From 49acf4fb5cc532f25cd923aac1321e16b03954bd Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 4 Oct 2026 08:30:38 -0700 Subject: [PATCH] wireguard: Don't setup server implicitly - When waiting after a auto-client-add operation, sometimes, it is not detected that server connection exists. Then a new server setup is triggered and this causes a duplicate wireguard server connection created. Fix this by making wait operations strictly read-only. - Also add more safety checks during waiting based one of the seen failures. Tests: - Functional tests work. - Auto add client works much more reliably. [jvalleroy: Update docstring] Reviewed-by: James Valleroy --- freedombox/modules/wireguard/utils.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/freedombox/modules/wireguard/utils.py b/freedombox/modules/wireguard/utils.py index 9032e73ed..29cfe5d1f 100644 --- a/freedombox/modules/wireguard/utils.py +++ b/freedombox/modules/wireguard/utils.py @@ -218,7 +218,8 @@ def add_server(settings): found = False for _, server in info['my_client']['servers'].items(): for _, peer in server['peers'].items(): - if peer['public_key'] == public_key: + if 'public_key' in peer and peer[ + 'public_key'] == public_key: found = True break @@ -320,22 +321,17 @@ def _get_next_available_ip_address(settings): def _server_connection(): - """Return a server connection. Create one if necessary.""" + """Return a server connection.""" setting_name = nm.SETTING_WIREGUARD_SETTING_NAME - connection = network.get_connection_by_interface_name('wg0') - if not connection: - setup_server() - for _ in range(10): - # XXX: Improve this waiting by doing a synchronous D-Bus operation to - # add network manager connection instead. - time.sleep(1) connection = network.get_connection_by_interface_name('wg0') if connection: break + time.sleep(1) + if not connection: - raise RuntimeError('Unable to create a server connection.') + raise RuntimeError('Unable to get a server connection.') # Retrieve secrets so that when the connection is changed, secrets are # preserved properly. @@ -368,7 +364,7 @@ def add_client(public_key): if info['my_server'] and info['my_server']['peers']: found = False for _, peer in info['my_server']['peers'].items(): - if peer['public_key'] == public_key: + if 'public_key' in peer and peer['public_key'] == public_key: found = True break @@ -399,7 +395,7 @@ def remove_client(public_key): if info['my_server']: found = False for _, peer in info['my_server']['peers'].items(): - if peer['public_key'] == public_key: + if 'public_key' in peer and peer['public_key'] == public_key: found = True break