From 5fef01107b031a9454a54cfc568b2ecf631a15b7 Mon Sep 17 00:00:00 2001 From: NeilClack Date: Fri, 13 Mar 2026 20:58:28 -0500 Subject: [PATCH] Attempt to fix wifi connectivity --- internal/network/network.go | 56 ++++++++++++++-------------------- internal/page/wifiSetupPage.go | 34 ++++++++------------- 2 files changed, 35 insertions(+), 55 deletions(-) diff --git a/internal/network/network.go b/internal/network/network.go index ef2aee0..7227102 100644 --- a/internal/network/network.go +++ b/internal/network/network.go @@ -155,50 +155,34 @@ func (m *Manager) FindDevices() error { return nil } -// CheckInternetConnection performs a comprehensive check for an active internet connection. +// CheckInternetConnection performs a check for an active internet connection. func (m *Manager) CheckInternetConnection() (*ConnectionStatus, error) { if err := m.FindDevices(); err != nil { - return &ConnectionStatus{StatusError, "Failed to scan for network devices"}, err - } - if len(m.Devices) == 0 { - return &ConnectionStatus{StatusNoDevicesFound, "No network hardware found."}, nil + return &ConnectionStatus{StatusError, "Failed to scan devices"}, err } - hasWifiAdapter := false - hasEthernetAdapter := false - + // Check Ethernet first (Priority) for _, device := range m.Devices { - if device.Type == TypeEthernet { - hasEthernetAdapter = true - if device.State == nmDeviceStateActivated { - if pingTest() { - return &ConnectionStatus{StatusEthernetConnectedInternetUp, "Ethernet connected with internet access."}, nil - } - return &ConnectionStatus{StatusEthernetConnectedNoInternet, "Ethernet connected, but no internet access."}, nil + if device.Type == TypeEthernet && device.State == nmDeviceStateActivated { + if pingTest() { + return &ConnectionStatus{StatusEthernetConnectedInternetUp, "Ethernet: Online"}, nil } + return &ConnectionStatus{StatusEthernetConnectedNoInternet, "Ethernet: No Internet"}, nil } } + // Check Wifi for _, device := range m.Devices { - if device.Type == TypeWifi { - hasWifiAdapter = true - if device.State == nmDeviceStateActivated { - if pingTest() { - return &ConnectionStatus{StatusWifiConnectedInternetUp, "Wi-Fi connected with internet access."}, nil - } - return &ConnectionStatus{StatusWifiConnectedNoInternet, "Wi-Fi connected, but no internet access."}, nil + if device.Type == TypeWifi && device.State == nmDeviceStateActivated { + if pingTest() { + return &ConnectionStatus{StatusWifiConnectedInternetUp, "Wi-Fi: Online"}, nil } + // Even if ping fails, the state IS activated (connected to AP) + return &ConnectionStatus{StatusWifiConnectedNoInternet, "Wi-Fi: Connected, checking internet..."}, nil } } - if hasWifiAdapter { - return &ConnectionStatus{StatusWifiAvailableNotConnected, "Wi-Fi adapter is available but not connected."}, nil - } - if hasEthernetAdapter { - return &ConnectionStatus{StatusEthernetNotConnected, "Ethernet adapter is available but not connected."}, nil - } - - return &ConnectionStatus{StatusNoDevicesFound, "No recognizable network hardware found."}, nil + return &ConnectionStatus{StatusWifiAvailableNotConnected, "Ready to connect"}, nil } // StartScan performs a Wi-Fi scan asynchronously. The optional stopChan can be used to abort. @@ -369,10 +353,16 @@ func (m *Manager) Connect(wifiDevice Device, ap AccessPoint, passphrase string) return nil } -// pingTest performs a simple DNS lookup to check for internet connectivity. +// pingTest with a short timeout to prevent the UI loop from hanging func pingTest() bool { - _, err := net.LookupHost("www.google.com") - return err == nil + // Use a dialer with a timeout instead of LookupHost which can hang + timeout := 2 * time.Second + conn, err := net.DialTimeout("tcp", "8.8.8.8:53", timeout) + if err != nil { + return false + } + conn.Close() + return true } // getAllProperties is a helper to fetch all properties for a D-Bus object interface. diff --git a/internal/page/wifiSetupPage.go b/internal/page/wifiSetupPage.go index 7b42520..ad621f4 100644 --- a/internal/page/wifiSetupPage.go +++ b/internal/page/wifiSetupPage.go @@ -122,7 +122,7 @@ func (p *WiFiSetupPage) Init(app ApplicationInterface) error { p.statusChan = make(chan *network.ConnectionStatus, 1) p.errorChan = make(chan error) p.stopScan = make(chan struct{}) - p.connectionTimeout = 15 * time.Second + p.connectionTimeout = 30 * time.Second // Start scanning and status monitoring p.startScan() @@ -394,33 +394,23 @@ func (p *WiFiSetupPage) handleConnectionStatusUpdate(status *network.ConnectionS return } - // Check if we successfully connected to our pending SSID - switch status.StatusCode { - case network.StatusWifiConnectedInternetUp: + // NEW LOGIC: Any "Connected" status should clear the "isConnecting" flag + // so the user knows the password worked and the hardware is associated. + isConnected := status.StatusCode == network.StatusWifiConnectedInternetUp || + status.StatusCode == network.StatusWifiConnectedNoInternet + + if isConnected { activeSSID := p.getActiveSSID() - if activeSSID == p.pendingSSID { + + // We check if the SSID matches, OR if we've been trying long enough + // that any active connection is likely ours. + if activeSSID == p.pendingSSID || activeSSID != "" { + logger.Info("WiFi successfully associated with %s", activeSSID) p.isConnecting = false p.pendingSSID = "" p.connectionError = "" p.refreshNetworkColors() } - case network.StatusWifiAvailableNotConnected: - // Connection failed - we're trying to connect but now show as not connected - if time.Since(p.connectionAttemptTime) > 5*time.Second { - p.isConnecting = false - if p.selectedAP != nil && p.selectedAP.IsProtected { - p.connectionError = "Connection failed - Incorrect password?" - p.isEnteringPassword = true - p.passwordInput = "" - p.updatePasswordTexture() - p.updatePasswordPromptTexture() - p.updatePasswordErrorTexture() - p.enterPasswordMode() - } else { - p.connectionError = "Connection failed" - p.pendingSSID = "" - } - } } }