diff --git a/wap/pkg/wifi/hotspot.go b/wap/pkg/wifi/hotspot.go index f1f7fa83..b66b3766 100644 --- a/wap/pkg/wifi/hotspot.go +++ b/wap/pkg/wifi/hotspot.go @@ -71,28 +71,27 @@ func parseHotspotLinux(output string) (supported bool, err error) { } func CheckHotspotSupported(ctx context.Context) (supported bool, err error) { - command := "" - os := "" - switch runtime.GOOS { - case "windows": - os = "windows" - command = "netsh wlan show drivers" - case "darwin": - os = "darwin" - command = "networksetup -listallhardwareports" - default: - os = "linux" - command = "iw list" - } - stdout, stderr, err := runCommand(ctx, command) - if err != nil { - log.Errorw("failed to check hotspot support", "command", command, "err", err, "stderr", stderr) - return - } - - return parseHotspot(stdout, os) -} + command := "" + os := "" + switch runtime.GOOS { + case "windows": + os = "windows" + command = "netsh wlan show drivers" + case "darwin": + os = "darwin" + command = "networksetup -listallhardwareports" + default: + os = "linux" + command = "iw list" + } + stdout, stderr, err := runCommand(ctx, command) + if err != nil { + log.Errorw("failed to check hotspot support", "command", command, "err", err, "stderr", stderr) + return + } + return parseHotspot(stdout, os) +} // startHotspot can be used to get the list of available wifis and their strength // If forceReload is set to true it resets the network adapter to make sure it fetches the latest list, otherwise it reads from cache diff --git a/wap/pkg/wifi/scan.go b/wap/pkg/wifi/scan.go index 74f5644f..f0c5fbad 100644 --- a/wap/pkg/wifi/scan.go +++ b/wap/pkg/wifi/scan.go @@ -129,55 +129,55 @@ func parseLinux(output string) (wifis []Wifi, err error) { // If forceReload is set to true it resets the network adapter to make sure it fetches the latest list, otherwise it reads from cache // wifiInterface is the name of interface that it should look for in Linux. func Scan(forceReload bool, wifiInterface ...string) (wifilist []Wifi, err error) { - var command, stdout, stderr string - switch runtime.GOOS { - case "windows": - // Your windows related code - case "darwin": - // Your darwin related code - default: - // Get all available wireless network interfaces - ctx, cl := context.WithTimeout(context.Background(), TimeLimit) - defer cl() + var command, stdout, stderr string + switch runtime.GOOS { + case "windows": + // Your windows related code + case "darwin": + // Your darwin related code + default: + // Get all available wireless network interfaces + ctx, cl := context.WithTimeout(context.Background(), TimeLimit) + defer cl() + + // Stage 1: Run iwconfig + stdout, stderr, err = runCommand(ctx, "iwconfig") + if err != nil { + log.Errorw("failed to run iwconfig", "err", err, "stderr", stderr) + return nil, err // Here we return nil for wifilist + } - // Stage 1: Run iwconfig - stdout, stderr, err = runCommand(ctx, "iwconfig") - if err != nil { - log.Errorw("failed to run iwconfig", "err", err, "stderr", stderr) - return nil, err // Here we return nil for wifilist - } - - // Stage 2: Filter output with grep-like functionality - var filteredLines []string - scanner := bufio.NewScanner(strings.NewReader(stdout)) - for scanner.Scan() { - line := scanner.Text() - if len(line) > 0 && (line[0] >= 'a' && line[0] <= 'z' || line[0] >= 'A' && line[0] <= 'Z') { - filteredLines = append(filteredLines, line) - } - } - - // Stage 3: Run awk-like functionality to print the first field of each line - var interfaces []string - for _, line := range filteredLines { - fields := strings.Fields(line) - if len(fields) > 0 { - interfaces = append(interfaces, fields[0]) - } - } - - // Loop over interfaces - for _, iface := range interfaces { - command = fmt.Sprintf("iwlist %s scan", iface) - stdout, _, err = runCommand(ctx, command) - if err == nil { - // Break the loop when the scan command is successful - wifilist, err = parse(stdout, "linux") - if err == nil { - break - } - } - } - } - return -} \ No newline at end of file + // Stage 2: Filter output with grep-like functionality + var filteredLines []string + scanner := bufio.NewScanner(strings.NewReader(stdout)) + for scanner.Scan() { + line := scanner.Text() + if len(line) > 0 && (line[0] >= 'a' && line[0] <= 'z' || line[0] >= 'A' && line[0] <= 'Z') { + filteredLines = append(filteredLines, line) + } + } + + // Stage 3: Run awk-like functionality to print the first field of each line + var interfaces []string + for _, line := range filteredLines { + fields := strings.Fields(line) + if len(fields) > 0 { + interfaces = append(interfaces, fields[0]) + } + } + + // Loop over interfaces + for _, iface := range interfaces { + command = fmt.Sprintf("iwlist %s scan", iface) + stdout, _, err = runCommand(ctx, command) + if err == nil { + // Break the loop when the scan command is successful + wifilist, err = parse(stdout, "linux") + if err == nil { + break + } + } + } + } + return +} diff --git a/wap/pkg/wifi/wifi.go b/wap/pkg/wifi/wifi.go index 7aed84d9..65114277 100644 --- a/wap/pkg/wifi/wifi.go +++ b/wap/pkg/wifi/wifi.go @@ -326,60 +326,56 @@ func activateHotspot(ctx context.Context) { } func checkIfIsConnectedLinux(ctx context.Context, interfaceName string) error { - var interfaces []string - - if interfaceName == "" { - // Get all available wireless network interfaces - ctx, cl := context.WithTimeout(context.Background(), TimeLimit) - defer cl() - - // Stage 1: Run iwconfig - stdout, _, err := runCommand(ctx, "iwconfig") - if err != nil { - return err - } - - // Stage 2: Filter output with grep-like functionality - var filteredLines []string - scanner := bufio.NewScanner(strings.NewReader(stdout)) - for scanner.Scan() { - line := scanner.Text() - if len(line) > 0 && (line[0] >= 'a' && line[0] <= 'z' || line[0] >= 'A' && line[0] <= 'Z') { - filteredLines = append(filteredLines, line) - } - } - - // Stage 3: Run awk-like functionality to print the first field of each line - for _, line := range filteredLines { - fields := strings.Fields(line) - if len(fields) > 0 { - interfaces = append(interfaces, fields[0]) - } - } - } else { - interfaces = []string{interfaceName} - } - - // Iterate over interfaces and check the connection - for _, iface := range interfaces { - stdout, stderr, err := runCommand(ctx, fmt.Sprintf("iw %s link", iface)) - if err != nil { - return err - } - // If connection is not "FxBlox" and is connected, return nil (no error) - if !strings.Contains(string(stdout), "FxBlox") && - !strings.Contains(string(stdout), "Not connected") && - !strings.Contains(string(stderr), "Not connected") { - return nil - } - } - // If no connected interface is found, return error - return errors.New("Wifi not connected on any interface") -} + var interfaces []string + if interfaceName == "" { + // Get all available wireless network interfaces + ctx, cl := context.WithTimeout(context.Background(), TimeLimit) + defer cl() + + // Stage 1: Run iwconfig + stdout, _, err := runCommand(ctx, "iwconfig") + if err != nil { + return err + } + // Stage 2: Filter output with grep-like functionality + var filteredLines []string + scanner := bufio.NewScanner(strings.NewReader(stdout)) + for scanner.Scan() { + line := scanner.Text() + if len(line) > 0 && (line[0] >= 'a' && line[0] <= 'z' || line[0] >= 'A' && line[0] <= 'Z') { + filteredLines = append(filteredLines, line) + } + } + // Stage 3: Run awk-like functionality to print the first field of each line + for _, line := range filteredLines { + fields := strings.Fields(line) + if len(fields) > 0 { + interfaces = append(interfaces, fields[0]) + } + } + } else { + interfaces = []string{interfaceName} + } + // Iterate over interfaces and check the connection + for _, iface := range interfaces { + stdout, stderr, err := runCommand(ctx, fmt.Sprintf("iw %s link", iface)) + if err != nil { + return err + } + // If connection is not "FxBlox" and is connected, return nil (no error) + if !strings.Contains(string(stdout), "FxBlox") && + !strings.Contains(string(stdout), "Not connected") && + !strings.Contains(string(stderr), "Not connected") { + return nil + } + } + // If no connected interface is found, return error + return errors.New("Wifi not connected on any interface") +} // TODO: unused, complete the c1 command func disconnectLinux(ctx context.Context) error {