diff --git a/api/api.go b/api/api.go index cdb24ac..32d39f6 100644 --- a/api/api.go +++ b/api/api.go @@ -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() @@ -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" diff --git a/management-interface.go b/management-interface.go index bd5b796..8d708ab 100644 --- a/management-interface.go +++ b/management-interface.go @@ -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 { @@ -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)