Skip to content

Commit

Permalink
removed iwconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Dec 12, 2024
1 parent 6d87f75 commit 286e8f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 82 deletions.
16 changes: 1 addition & 15 deletions wap/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -550,22 +549,9 @@ func getIPFromSpecificNetwork(ctx context.Context, ssid string) (string, error)
return "", err
}

// Check which command is available
useIw := false
if _, err := exec.LookPath("iwconfig"); err != nil {
if _, err := exec.LookPath("iw"); err != nil {
return "", fmt.Errorf("neither iwconfig nor iw commands found")
}
useIw = true
}

for _, iface := range ifaces {
var cmdString string
if useIw {
cmdString = "iw dev " + iface.Name + " link"
} else {
cmdString = "iwconfig " + iface.Name
}
cmdString = "iw dev " + iface.Name + " link"

out, _, err := wifi.RunCommand(ctx, cmdString)

Expand Down
85 changes: 18 additions & 67 deletions wap/pkg/wifi/wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,6 @@ func activateHotspot(ctx context.Context) {

func checkIfIsConnectedLinux(ctx context.Context, interfaceName string) error {
var interfaces []string
var useIw bool

// Check which command is available
if _, err := exec.LookPath("iwconfig"); err != nil {
if _, err := exec.LookPath("iw"); err != nil {
return fmt.Errorf("neither iwconfig nor iw commands found")
}
useIw = true
}

if interfaceName == "" {
// Get all available wireless network interfaces
Expand All @@ -537,46 +528,20 @@ func checkIfIsConnectedLinux(ctx context.Context, interfaceName string) error {
var stdout string
var err error

if useIw {
// Get interfaces using iw
stdout, _, err = runCommand(ctx, "iw dev")
if err != nil {
return err
}

// Parse iw output for interfaces
scanner := bufio.NewScanner(strings.NewReader(stdout))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Interface") {
fields := strings.Fields(line)
if len(fields) > 1 {
interfaces = append(interfaces, fields[1])
}
}
}
} else {
// Use existing logic for iwconfig
stdout, _, err = runCommand(ctx, "iwconfig")
if err != nil {
return err
}

// 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)
}
}
// Get interfaces using iw
stdout, _, err = runCommand(ctx, "iw dev")
if err != nil {
return err
}

// Run awk-like functionality to print the first field of each line
for _, line := range filteredLines {
// Parse iw output for interfaces
scanner := bufio.NewScanner(strings.NewReader(stdout))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Interface") {
fields := strings.Fields(line)
if len(fields) > 0 {
interfaces = append(interfaces, fields[0])
if len(fields) > 1 {
interfaces = append(interfaces, fields[1])
}
}
}
Expand All @@ -589,32 +554,18 @@ func checkIfIsConnectedLinux(ctx context.Context, interfaceName string) error {
var stdout, stderr string
var err error

if useIw {
stdout, stderr, err = runCommand(ctx, fmt.Sprintf("iw dev %s link", iface))
} else {
stdout, stderr, err = runCommand(ctx, fmt.Sprintf("iwconfig %s", iface))
}
stdout, stderr, err = runCommand(ctx, fmt.Sprintf("iw dev %s link", iface))

if err != nil {
continue // Try next interface if this one fails
}

// Check connection status based on the tool being used
if useIw {
// iw shows "connected" when connected
if !strings.Contains(stdout, "FxBlox") &&
strings.Contains(stdout, "connected") &&
!strings.Contains(stderr, "Not connected") {
return nil
}
} else {
// Use existing logic for iwconfig
if !strings.Contains(stdout, "FxBlox") &&
!strings.Contains(stdout, "ESSID:\"\"") &&
!strings.Contains(stdout, "Not connected") &&
!strings.Contains(stderr, "Not connected") {
return nil
}
// iw shows "connected" when connected
if !strings.Contains(stdout, "FxBlox") &&
strings.Contains(stdout, "connected") &&
!strings.Contains(stderr, "Not connected") {
return nil
}
}

Expand Down

0 comments on commit 286e8f5

Please sign in to comment.