From 1f152d954c1463d819073ca3ea41227e96615296 Mon Sep 17 00:00:00 2001 From: codeking Date: Tue, 20 Jan 2026 06:17:10 +0100 Subject: [PATCH] Improve connection management-related logic --- core/controllers/ConnectionController.py | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/core/controllers/ConnectionController.py b/core/controllers/ConnectionController.py index 356989b..5efe84c 100644 --- a/core/controllers/ConnectionController.py +++ b/core/controllers/ConnectionController.py @@ -206,6 +206,7 @@ class ConnectionController: raise ConnectionError('The connection could not be established.') + ConnectionController.terminate_tor_connection() time.sleep(1.0) @staticmethod @@ -398,6 +399,7 @@ class ConnectionController: if completed_successfully or not ConnectionController.system_uses_wireguard_interface(): subprocess.run(('nmcli', 'connection', 'delete', 'hv-ipv6-sink'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + ConnectionController.terminate_tor_connection() SystemState.dissolve() else: @@ -482,6 +484,9 @@ class ConnectionController: @staticmethod def __establish_system_connection(profile: SystemProfile, connection_observer: Optional[ConnectionObserver] = None): + if shutil.which('dbus-send') is None: + raise CommandNotFoundError('dbus-send') + if shutil.which('nmcli') is None: raise CommandNotFoundError('nmcli') @@ -489,19 +494,28 @@ class ConnectionController: try: process_output = subprocess.check_output(('nmcli', 'connection', 'import', '--temporary', 'type', 'wireguard', 'file', profile.get_wireguard_configuration_path()), text=True) - except CalledProcessError as exception: - raise CalledProcessError(exception.returncode, 'nmcli') - - connection_id = (m := re.search(r'(?<=\()([a-f0-9-]+?)(?=\))', process_output)) and m.group(1) - subprocess.run(('nmcli', 'connection', 'modify', connection_id, 'ipv4.dns-priority', '-1750', 'ipv4.ignore-auto-dns', 'yes'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + except CalledProcessError: + raise ConnectionError('The connection could not be established.') try: + + connection_id = (m := re.search(r'(?<=\()([a-f0-9-]+?)(?=\))', process_output)) and m.group(1) ipv6_method = subprocess.check_output(('nmcli', '-g', 'ipv6.method', 'connection', 'show', connection_id), text=True).strip() + except CalledProcessError: raise ConnectionError('The connection could not be established.') if ipv6_method in ('disabled', 'ignore'): - subprocess.run(('nmcli', 'connection', 'add', 'type', 'dummy', 'save', 'no', 'con-name', 'hv-ipv6-sink', 'ifname', 'hvipv6sink0', 'ipv6.method', 'manual', 'ipv6.addresses', 'fd7a:fd4b:54e3:077c::/64', 'ipv6.gateway', 'fd7a:fd4b:54e3:077c::1', 'ipv6.route-metric', '72'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + try: + subprocess.run(('dbus-send', '--system', '--print-reply', '--dest=org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager', 'org.freedesktop.DBus.Properties.Set', 'string:org.freedesktop.NetworkManager', 'string:ConnectivityCheckEnabled', 'variant:boolean:false'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + except CalledProcessError: + raise ConnectionError('The connection could not be established.') + + try: + subprocess.run(('nmcli', 'connection', 'add', 'type', 'dummy', 'save', 'no', 'con-name', 'hv-ipv6-sink', 'ifname', 'hvipv6sink0', 'ipv6.method', 'manual', 'ipv6.addresses', 'fd7a:fd4b:54e3:077c::/64', 'ipv6.gateway', 'fd7a:fd4b:54e3:077c::1', 'ipv6.dns', '::1', 'ipv6.route-metric', '72'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) + except CalledProcessError: + raise ConnectionError('The connection could not be established.') SystemStateController.create(profile.id)