Fix network connection timeouts and security detection
- Fixed detectSecurity() where WPA2 (0x100/PSK) and WPA3 (0x400/SAE) flags were swapped, causing NM to reject connections silently - Added missing AP path to AddAndActivateConnection call - Added critical connection settings: UUID, interface-name, autoconnect, permissions - Changed init() to fail fast with logger.Fatal instead of silent failure on NM connection error - Added auth-alg and psk-flags to WPA/WPA2 security settings
This commit is contained in:
+28
-11
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
gonm "github.com/Wifx/gonetworkmanager/v2"
|
||||
"github.com/google/uuid"
|
||||
"rhemawavetech.com/rhemawave/internal/logger"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,8 @@ func init() {
|
||||
var err error
|
||||
nm, err = gonm.NewNetworkManager()
|
||||
if err != nil {
|
||||
fmt.Printf("network: failed to connect to NetworkManager: %v\n", err)
|
||||
logger.Fatal("network: failed to connect to NetworkManager: %v\n", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +103,11 @@ func ConnectToNetwork(ssid, password string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ifaceName, err := dev.GetPropertyInterface()
|
||||
if err != nil {
|
||||
return fmt.Errorf("network: cannot get interface name: %w", err)
|
||||
}
|
||||
|
||||
ap, err := findAccessPoint(dev, ssid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("network: access point %q not found: %w", ssid, err)
|
||||
@@ -113,7 +120,7 @@ func ConnectToNetwork(ssid, password string) error {
|
||||
}
|
||||
|
||||
security := detectSecurity(ap)
|
||||
settings, err := buildConnectionSettings(ssid, password, security)
|
||||
settings, err := buildConnectionSettings(ssid, password, security, ifaceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -402,15 +409,19 @@ func detectSecurity(ap gonm.AccessPoint) SecurityType {
|
||||
wpaFlags, _ := ap.GetPropertyWPAFlags()
|
||||
rsnFlags, _ := ap.GetPropertyRSNFlags()
|
||||
|
||||
if rsnFlags != 0 {
|
||||
if rsnFlags&0x100 != 0 {
|
||||
return SecurityWPA3
|
||||
}
|
||||
// WPA3 uses SAE (0x400)
|
||||
if rsnFlags&0x400 != 0 {
|
||||
return SecurityWPA3
|
||||
}
|
||||
// WPA2 uses PSK (0x100)
|
||||
if rsnFlags&0x100 != 0 {
|
||||
return SecurityWPA2
|
||||
}
|
||||
// WPA uses WPA flags (not RSN)
|
||||
if wpaFlags != 0 {
|
||||
return SecurityWPA
|
||||
}
|
||||
// WEP uses privacy flag
|
||||
if flags&0x1 != 0 {
|
||||
return SecurityWEP
|
||||
}
|
||||
@@ -418,11 +429,15 @@ func detectSecurity(ap gonm.AccessPoint) SecurityType {
|
||||
return SecurityOpen
|
||||
}
|
||||
|
||||
func buildConnectionSettings(ssid, password string, security SecurityType) (map[string]map[string]interface{}, error) {
|
||||
func buildConnectionSettings(ssid, password string, security SecurityType, interfaceName string) (map[string]map[string]interface{}, error) {
|
||||
settings := map[string]map[string]interface{}{
|
||||
"connection": {
|
||||
"id": ssid,
|
||||
"type": "802-11-wireless",
|
||||
"id": ssid,
|
||||
"type": "802-11-wireless",
|
||||
"uuid": uuid.NewString(),
|
||||
"interface-name": interfaceName,
|
||||
"autoconnect": true,
|
||||
"permissions": []string{},
|
||||
},
|
||||
"802-11-wireless": {
|
||||
"ssid": []byte(ssid),
|
||||
@@ -451,8 +466,10 @@ func buildConnectionSettings(ssid, password string, security SecurityType) (map[
|
||||
return nil, fmt.Errorf("network: WPA/WPA2 network requires a password")
|
||||
}
|
||||
settings["802-11-wireless-security"] = map[string]interface{}{
|
||||
"key-mgmt": "wpa-psk",
|
||||
"psk": password,
|
||||
"key-mgmt": "wpa-psk",
|
||||
"auth-alg": "open",
|
||||
"psk": password,
|
||||
"psk-flags": uint32(0),
|
||||
}
|
||||
|
||||
case SecurityWPA3:
|
||||
|
||||
Reference in New Issue
Block a user