network: fix properly detect network type

This commit is contained in:
Alfredo Monclus 2024-05-26 12:15:44 -03:00
parent f07f77eaa6
commit 3c71f67f53
2 changed files with 47 additions and 26 deletions

View File

@ -160,9 +160,7 @@ class SdbusNm:
return any(net['SSID'] == ssid for net in self.get_known_networks())
def is_open(self, ssid):
for network in self.get_networks():
if network["SSID"] == ssid:
return network["security"] == "Open"
return self.get_security_type(ssid) == "Open"
def get_ip_address(self):
active_connection_path = self.nm.primary_connection
@ -181,7 +179,7 @@ class SdbusNm:
{
"SSID": ap.ssid.decode("utf-8"),
"known": self.is_known(ap.ssid.decode("utf-8")),
"security": get_encryption(ap.rsn_flags),
"security": get_encryption(ap.rsn_flags or ap.wpa_flags or ap.flags),
"frequency": WifiChannels(ap.frequency)[0],
"channel": WifiChannels(ap.frequency)[1],
"signal_level": ap.strength,
@ -205,10 +203,24 @@ class SdbusNm:
def get_connected_bssid(self):
return self.get_connected_ap().hw_address if self.get_connected_ap() is not None else None
def get_security_type(self, ssid):
return next(
(
network["security"]
for network in self.get_networks()
if network["SSID"] == ssid
),
None
)
def add_network(self, ssid, psk):
if self.insufficient_privileges:
return {"error": "insufficient_privileges", "message": _("Insufficient privileges")}
security_type = self.get_security_type(ssid)
if security_type is None:
return {"error": "network_not_found", "message": _("Network not found")}
existing_networks = NetworkManagerSettings().list_connections()
for connection_path in existing_networks:
connection_settings = NetworkConnectionSettings(connection_path).get_settings()
@ -234,13 +246,23 @@ class SdbusNm:
"ipv6": {"method": ("s", "auto")},
}
if psk:
if "WPA-PSK" in security_type:
properties["802-11-wireless"]["security"] = ("s", "802-11-wireless-security")
properties["802-11-wireless-security"] = {
"key-mgmt": ("s", "wpa-psk"),
"auth-alg": ("s", "open"),
"psk": ("s", psk),
}
elif "WEP" in security_type:
properties["802-11-wireless"]["security"] = ("s", "802-11-wireless-security")
properties["802-11-wireless-security"] = {
"key-mgmt": ("s", "none"),
"wep-key-type": ("s", "key"),
"wep-key0": ("s", psk),
"auth-alg": ("s", "open"),
}
elif security_type != "Open":
return {"error": "unknown_security_type", "message": _("Unknown security type")}
try:
NetworkManagerSettings().add_connection(properties)
@ -260,7 +282,7 @@ class SdbusNm:
self.wlan_device.disconnect()
def delete_network(self, ssid):
connection = NetworkManagerSettings().get_connections_by_id(ssid)
connection = self.get_connection_by_ssid(ssid)
for path in connection:
self.delete_connection_path(path)
@ -271,31 +293,32 @@ class SdbusNm:
def rescan(self):
return self.wlan_device.request_scan({})
def connect(self, ssid):
connections = NetworkManagerSettings().list_connections()
target_connection = None
for connection_path in connections:
def get_connection_by_ssid(self, ssid):
existing_networks = NetworkManagerSettings().list_connections()
for connection_path in existing_networks:
connection_settings = NetworkConnectionSettings(connection_path).get_settings()
if (
connection_settings.get('802-11-wireless') and
connection_settings['802-11-wireless'].get('ssid') and
connection_settings['802-11-wireless']['ssid'][1].decode() == ssid
):
target_connection = connection_path
break
return connection_path
return None
if target_connection:
self.popup(f"{ssid}\n{_('Starting WiFi Association')}", 1)
try:
active_connection = self.nm.activate_connection(target_connection)
self.monitor_connection_status(active_connection)
return target_connection
except Exception as e:
logging.exception("Unexpected error")
self.popup(f"Unexpected error: {e}")
else:
self.popup(f"SSID '{ssid}' not found among saved connections")
def connect(self, ssid):
connections = NetworkManagerSettings().list_connections()
if target_connection := self.get_connection_by_ssid(ssid):
self.popup(f"{ssid}\n{_('Starting WiFi Association')}", 1)
try:
active_connection = self.nm.activate_connection(target_connection)
self.monitor_connection_status(active_connection)
return target_connection
except Exception as e:
logging.exception("Unexpected error")
self.popup(f"Unexpected error: {e}")
else:
self.popup(f"SSID '{ssid}' not found among saved connections")
def toggle_wifi(self, enable):
self.nm.wireless_enabled = enable

View File

@ -222,8 +222,6 @@ class Panel(ScreenPanel):
bssid = self.sdbus_nm.get_bssid_from_ssid(ssid)
if bssid and bssid in self.network_rows:
self.remove_network_from_list(bssid)
msg = f"{ssid}\n" + _("Starting WiFi Association")
self._screen.show_popup_message(msg, 1)
result = self.sdbus_nm.connect(ssid)
logging.debug(result)
self.update_all_networks()