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 <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2023-11-04 10:16:51 -07:00 committed by James Valleroy
parent e42740b589
commit f4d8d3d046
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 21 additions and 10 deletions

View File

@ -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):

View File

@ -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