Skip to content

Commit

Permalink
select_network on connection, add additional log info
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainrax committed Jan 31, 2024
1 parent a9810d2 commit 2d432a1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
48 changes: 48 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,32 @@ func connectToWifi(ssid string, passkey string) error {
return err
}

if ssids, err := getSSIDIds(); err == nil {
if id, ok := ssids[ssid]; ok {
// Connect to the network
if err := exec.Command("wpa_cli", "-i", "wlan0", "select_network", id).Run(); err != nil {
log.Printf("Error selecting Wi-Fi network: %v", err)
// Remove the network from the config
if err := removeNetworkFromWPAConfig(ssid); err != nil {
log.Printf("Error removing Wi-Fi network: %v", err)
return err
}
return err
} else {
// reassociate
if err := exec.Command("wpa_cli", "-i", "wlan0", "reassociate").Run(); err != nil {
log.Printf("Error reassociating Wi-Fi network: %v", err)
// Remove the network from the config
if err := removeNetworkFromWPAConfig(ssid); err != nil {
log.Printf("Error removing Wi-Fi network: %v", err)
return err
}
return err
}
}
}
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Expand All @@ -1061,6 +1087,28 @@ func connectToWifi(ssid string, passkey string) error {
return nil
}

func getSSIDIds() (map[string]string, error) {
// Get the list of Wi-Fi networks
cmd := exec.Command("wpa_cli", "-i", "wlan0", "list_networks")
output, err := cmd.Output()
if err != nil {
return nil, err
}

// Parse the output
ssidIds := make(map[string]string)
lines := strings.Split(string(output), "\n")
for _, line := range lines[1:] {
if line == "" {
continue
}
fields := strings.Fields(line)
ssidIds[fields[1]] = fields[0]
}

return ssidIds, nil
}

func addNetworkToWPAConfig(ssid string, passkey string) error {
configFile := "/etc/wpa_supplicant/wpa_supplicant.conf"

Expand Down
6 changes: 3 additions & 3 deletions management-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func WifiNetworkHandler(w http.ResponseWriter, r *http.Request) {
wifiNetworks, err := netmanagerclient.ListSavedWifiNetworks()
if err != nil {
log.Println(err)
wifiProps.Error = err.Error()
wifiProps.Error = "Error while getting saved networks: " + err.Error()
}
wifiProps.Networks = []wifiNetwork{}
for _, network := range wifiNetworks {
Expand All @@ -634,14 +634,14 @@ func WifiNetworkHandler(w http.ResponseWriter, r *http.Request) {
wifiProps.AvailableNetworks = []string{}
if err != nil {
log.Println(err)
wifiProps.Error = err.Error()
wifiProps.Error = "Error while getting available networks: " + err.Error()
}
for _, network := range availableWifiNetworks {
wifiProps.AvailableNetworks = append(wifiProps.AvailableNetworks, network.SSID)
}

if wifiProps.Error == "" && err != nil {
wifiProps.Error = err.Error()
wifiProps.Error = "Error while getting available networks: " + err.Error()
}

tmpl.ExecuteTemplate(w, "wifi-networks.html", wifiProps)
Expand Down

0 comments on commit 2d432a1

Please sign in to comment.