Update public source
This commit is contained in:
+107
-16
@@ -4,6 +4,7 @@ import secrets
|
|||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import time
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -63,7 +64,9 @@ class WiFiConnectionService:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
command_runner=None,
|
command_runner=None,
|
||||||
command_timeout=30
|
command_timeout=30,
|
||||||
|
scan_timeout=15,
|
||||||
|
scan_poll_interval=0.25
|
||||||
):
|
):
|
||||||
self.command_runner = (
|
self.command_runner = (
|
||||||
command_runner
|
command_runner
|
||||||
@@ -72,6 +75,8 @@ class WiFiConnectionService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.command_timeout = command_timeout
|
self.command_timeout = command_timeout
|
||||||
|
self.scan_timeout = scan_timeout
|
||||||
|
self.scan_poll_interval = scan_poll_interval
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_interface(interface):
|
def _validate_interface(interface):
|
||||||
@@ -216,6 +221,79 @@ class WiFiConnectionService:
|
|||||||
f"Cannot parse D-Bus property {property_name}."
|
f"Cannot parse D-Bus property {property_name}."
|
||||||
) from error
|
) from error
|
||||||
|
|
||||||
|
def _get_last_scan(self, device_path):
|
||||||
|
|
||||||
|
parts = self._get_property(
|
||||||
|
device_path,
|
||||||
|
NM_DEVICE_WIRELESS,
|
||||||
|
"LastScan"
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(parts) != 2 or parts[0] != "x":
|
||||||
|
raise WiFiConnectionError(
|
||||||
|
"Unexpected D-Bus type for LastScan."
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return int(parts[1])
|
||||||
|
|
||||||
|
except ValueError as error:
|
||||||
|
|
||||||
|
raise WiFiConnectionError(
|
||||||
|
"Invalid LastScan value from NetworkManager."
|
||||||
|
) from error
|
||||||
|
|
||||||
|
def _request_fresh_scan(self, device_path):
|
||||||
|
|
||||||
|
previous_scan = self._get_last_scan(
|
||||||
|
device_path
|
||||||
|
)
|
||||||
|
|
||||||
|
self._busctl(
|
||||||
|
"call",
|
||||||
|
NM_SERVICE,
|
||||||
|
device_path,
|
||||||
|
NM_DEVICE_WIRELESS,
|
||||||
|
"RequestScan",
|
||||||
|
"a{sv}",
|
||||||
|
"0"
|
||||||
|
)
|
||||||
|
|
||||||
|
deadline = (
|
||||||
|
time.monotonic()
|
||||||
|
+
|
||||||
|
self.scan_timeout
|
||||||
|
)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
current_scan = self._get_last_scan(
|
||||||
|
device_path
|
||||||
|
)
|
||||||
|
|
||||||
|
if current_scan > previous_scan:
|
||||||
|
return
|
||||||
|
|
||||||
|
remaining = (
|
||||||
|
deadline
|
||||||
|
-
|
||||||
|
time.monotonic()
|
||||||
|
)
|
||||||
|
|
||||||
|
if remaining <= 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
time.sleep(
|
||||||
|
min(
|
||||||
|
self.scan_poll_interval,
|
||||||
|
remaining
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
raise WiFiConnectionError(
|
||||||
|
"NetworkManager did not complete a fresh Wi-Fi scan in time."
|
||||||
|
)
|
||||||
|
|
||||||
def _get_integer_property(
|
def _get_integer_property(
|
||||||
self,
|
self,
|
||||||
object_path,
|
object_path,
|
||||||
@@ -346,6 +424,10 @@ class WiFiConnectionService:
|
|||||||
interface
|
interface
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self._request_fresh_scan(
|
||||||
|
device_path
|
||||||
|
)
|
||||||
|
|
||||||
output = self._busctl(
|
output = self._busctl(
|
||||||
"call",
|
"call",
|
||||||
NM_SERVICE,
|
NM_SERVICE,
|
||||||
@@ -757,6 +839,7 @@ class WiFiConnectionService:
|
|||||||
|
|
||||||
profile_uuid = None
|
profile_uuid = None
|
||||||
success = False
|
success = False
|
||||||
|
operation_error = None
|
||||||
error_message = None
|
error_message = None
|
||||||
restore_error = None
|
restore_error = None
|
||||||
cleanup_error = None
|
cleanup_error = None
|
||||||
@@ -777,8 +860,9 @@ class WiFiConnectionService:
|
|||||||
|
|
||||||
success = True
|
success = True
|
||||||
|
|
||||||
except NetworkCommandError as error:
|
except Exception as error:
|
||||||
|
|
||||||
|
operation_error = error
|
||||||
error_message = str(
|
error_message = str(
|
||||||
error
|
error
|
||||||
)
|
)
|
||||||
@@ -837,34 +921,41 @@ class WiFiConnectionService:
|
|||||||
error
|
error
|
||||||
)
|
)
|
||||||
|
|
||||||
if restore_error and cleanup_error:
|
verification_error = (
|
||||||
|
f"Password verification failed: {operation_error}. "
|
||||||
raise NetworkRestoreError(
|
if operation_error else ""
|
||||||
"The Wi-Fi verification attempt finished, but the previous connection "
|
)
|
||||||
f"could not be restored: {restore_error}. "
|
|
||||||
f"The temporary profile could not be deleted: {cleanup_error}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if restore_error:
|
if restore_error:
|
||||||
|
|
||||||
raise NetworkRestoreError(
|
raise NetworkRestoreError(
|
||||||
"The Wi-Fi verification attempt finished, but the previous connection "
|
f"{verification_error}"
|
||||||
f"could not be restored: {restore_error}"
|
"The previous Wi-Fi connection could not be restored: "
|
||||||
|
f"{restore_error}. "
|
||||||
|
+ (
|
||||||
|
f"The temporary profile could not be deleted: {cleanup_error}"
|
||||||
|
if cleanup_error else ""
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if cleanup_error:
|
if cleanup_error:
|
||||||
|
|
||||||
verification_error = (
|
|
||||||
f"Password verification failed: {error_message}. "
|
|
||||||
if error_message else ""
|
|
||||||
)
|
|
||||||
|
|
||||||
raise NetworkCleanupError(
|
raise NetworkCleanupError(
|
||||||
f"{verification_error}"
|
f"{verification_error}"
|
||||||
"The temporary Wi-Fi profile could not be deleted: "
|
"The temporary Wi-Fi profile could not be deleted: "
|
||||||
f"{cleanup_error}"
|
f"{cleanup_error}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
operation_error
|
||||||
|
and
|
||||||
|
not isinstance(
|
||||||
|
operation_error,
|
||||||
|
NetworkCommandError
|
||||||
|
)
|
||||||
|
):
|
||||||
|
raise operation_error
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"success": success,
|
"success": success,
|
||||||
"interface": interface,
|
"interface": interface,
|
||||||
|
|||||||
Reference in New Issue
Block a user