Skip to content

Commit

Permalink
gofmt-ed
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Jun 27, 2023
1 parent 0465952 commit 65968c1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 122 deletions.
41 changes: 20 additions & 21 deletions wap/pkg/wifi/hotspot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
102 changes: 51 additions & 51 deletions wap/pkg/wifi/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
// 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
}
96 changes: 46 additions & 50 deletions wap/pkg/wifi/wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 65968c1

Please sign in to comment.