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