From f4d8d3d04625ae7220b5b2e5424d78fa49cc7ae6 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sat, 4 Nov 2023 10:16:51 -0700 Subject: [PATCH] ejabberd: Update old STUN URIs to remove 'transport' parameter - If the STUN/TURN configuration is not managed by FreedomBox, the parameters are left unchanged. Tests: - Install app after installing Coturn. Notice that STUN URI doesn't have a 'transport' parameter. - Install app without the patches after installing Coturn. Notice that STUN URIs have 'transport' parameter. Then apply patches. Setup is run. 'transport' parameter is removed and only one STUN URI is present. matrix-synapse server restarts successfully and don't have show any errors/warnings regarding STUN/TURN configuration during startup. - Install app without patches and set custom STUN/TURN URIs. Apply patches. Setup is run. The URIs are not changed. Reviewed-by: James Valleroy --- plinth/modules/ejabberd/__init__.py | 13 +++++++++---- plinth/modules/ejabberd/privileged.py | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/plinth/modules/ejabberd/__init__.py b/plinth/modules/ejabberd/__init__.py index 3ee7771b0..d4489c7a1 100644 --- a/plinth/modules/ejabberd/__init__.py +++ b/plinth/modules/ejabberd/__init__.py @@ -50,7 +50,7 @@ class EjabberdApp(app_module.App): app_id = 'ejabberd' - _version = 7 + _version = 8 def __init__(self): """Create components for the app.""" @@ -148,9 +148,14 @@ class EjabberdApp(app_module.App): if not old_version: self.enable() - # Configure STUN/TURN only if there's a valid TLS domain set for Coturn - configuration = self.get_component('turn-ejabberd').get_configuration() - update_turn_configuration(configuration, force=True) + if not old_version or get_turn_configuration()[1]: + # Configure STUN/TURN only if there's a valid TLS domain set for + # Coturn. Do this if app is being freshly installed or if it is + # previously installed and configured to use STUN/TURN + # auto-management. + configuration = self.get_component( + 'turn-ejabberd').get_configuration() + update_turn_configuration(configuration, force=True) class EjabberdTurnConsumer(TurnConsumer): diff --git a/plinth/modules/ejabberd/privileged.py b/plinth/modules/ejabberd/privileged.py index 7dd6e6498..11851c734 100644 --- a/plinth/modules/ejabberd/privileged.py +++ b/plinth/modules/ejabberd/privileged.py @@ -30,7 +30,7 @@ yaml = YAML() yaml.allow_duplicate_keys = True yaml.preserve_quotes = True # type: ignore [assignment] -TURN_URI_REGEX = r'(stun|turn):(.*):([0-9]{4})\?transport=(tcp|udp)' +TURN_URI_REGEX = r'(stun|turn):(.*):([0-9]{4})(?:\?transport=(tcp|udp))?' @privileged @@ -290,7 +290,7 @@ def _generate_service(uri: str) -> dict: if not match: raise ValueError('URL does not match TURN URI') - typ, domain, port, transport = match.groups() + typ, domain, port, transport = match.groups('udp') return { "host": domain, "port": int(port), @@ -301,10 +301,16 @@ def _generate_service(uri: str) -> dict: def _generate_uris(services: list[dict]) -> list[str]: """Generate STUN/TURN URIs from ejabberd mod_stun_disco service config.""" - return [ - f"{s['type']}:{s['host']}:{s['port']}?transport={s['transport']}" - for s in services - ] + uris = [] + for s in services: + uri = f"{s['type']}:{s['host']}:{s['port']}" + if s['type'] != 'stun': + uri += f"?transport={s['transport']}" + + if uri not in uris: + uris.append(uri) + + return uris @privileged