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

View File

@ -222,8 +222,6 @@ class Panel(ScreenPanel):
bssid = self.sdbus_nm.get_bssid_from_ssid(ssid) bssid = self.sdbus_nm.get_bssid_from_ssid(ssid)
if bssid and bssid in self.network_rows: if bssid and bssid in self.network_rows:
self.remove_network_from_list(bssid) 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) result = self.sdbus_nm.connect(ssid)
logging.debug(result) logging.debug(result)
self.update_all_networks() self.update_all_networks()