Skip to content

Commit dc9ff4e

Browse files
committed
Add support for external-IP-address-autodetection to Coturn
1 parent ccbff15 commit dc9ff4e

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

docs/configuring-playbook-turn.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ matrix_coturn_enabled: false
1616
In that case, Synapse would not point to any Coturn servers and audio/video call functionality may fail.
1717
1818
## Manually defining your public IP
19+
1920
In the `hosts` file we explicitly ask for your server's external IP address when defining `ansible_host`, because the same value is used for configuring Coturn.
21+
2022
If you'd rather use a local IP for `ansible_host`, make sure to set up `matrix_coturn_turn_external_ip_address` replacing `YOUR_PUBLIC_IP` with the pubic IP used by the server.
2123

2224
```yaml
2325
matrix_coturn_turn_external_ip_address: "YOUR_PUBLIC_IP"
2426
```
2527

28+
If you'd like to rely on external IP address auto-detection (not recommended unless you need it), set `matrix_coturn_turn_external_ip_address` to an empty value. The playbook will automatically contact an [EchoIP](https://github.com/mpolden/echoip)-compatible service (`https://ifconfig.co/json` by default) to determine your server's IP address. This API endpoint is configurable via the `matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url` variable.
29+
30+
If your server has multiple external IP addresses, the Coturn role offers a different variable for specifying them:
31+
32+
```yaml
33+
# Note: matrix_coturn_turn_external_ip_addresses is different than matrix_coturn_turn_external_ip_address
34+
matrix_coturn_turn_external_ip_addresses: ['1.2.3.4', '4.5.6.7']
35+
```
36+
2637
## Using your own external Coturn server
2738

2839
If you'd like to use another TURN server (be it Coturn or some other one), you can configure the playbook like this:
@@ -49,4 +60,4 @@ jitsi_web_stun_servers:
4960
You can put multiple host/port combinations if you like.
5061

5162
## Further variables and configuration options
52-
To see all the available configuration options, check roles/custom/matrix-coturn/defaults/main.yml
63+
To see all the available configuration options, check roles/custom/matrix-coturn/defaults/main.yml

examples/vars.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ devture_traefik_config_certificatesResolvers_acme_email: ''
4141
# The playbook creates additional Postgres users and databases (one for each enabled service)
4242
# using this superuser account.
4343
devture_postgres_connection_password: ''
44+
45+
# By default, we configure Coturn's external IP address using the value specified for `ansible_host` in your `inventory/hosts` file.
46+
# If this value is an external IP address, you can skip this section.
47+
#
48+
# If `ansible_host` is not the server's external IP address, you have 2 choices:
49+
# 1. Uncomment the line below, to allow IP address auto-detection to happen (more on this below)
50+
# 2. Uncomment and adjust the line below to specify an IP address manually
51+
#
52+
# By default, auto-detection will be attempted using the `https://ifconfig.co/json` API.
53+
# Default values for this are specified in `matrix_coturn_turn_external_ip_address_auto_detection_*` variables in the Coturn role
54+
# (see `roles/custom/matrix-coturn/defaults/main.yml`).
55+
#
56+
# If your server has multiple IP addresses, you may define them in another variable which allows a list of addresses.
57+
# Example: `matrix_coturn_turn_external_ip_addresses: ['1.2.3.4', '4.5.6.7']`
58+
#
59+
# matrix_coturn_turn_external_ip_address: ''

group_vars/matrix_servers

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,9 @@ matrix_coturn_enabled: true
22452245

22462246
matrix_coturn_container_image_self_build: "{{ matrix_architecture not in ['amd64', 'arm32', 'arm64'] }}"
22472247

2248+
# We make the assumption that `ansible_host` points to an external IP address, which may not always be the case.
2249+
# Users are free to set `matrix_coturn_turn_external_ip_address` to an empty string
2250+
# to allow auto-detection (via an EchoIP service) to happen at runtime.
22482251
matrix_coturn_turn_external_ip_address: "{{ ansible_host }}"
22492252

22502253
matrix_coturn_turn_static_auth_secret: "{{ '%s' | format(matrix_homeserver_generic_secret_key) | password_hash('sha512', 'coturn.sas', rounds=655555) | to_uuid }}"

roles/custom/matrix-coturn/defaults/main.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,25 @@ matrix_coturn_turn_udp_max_port: 49172
7373
matrix_coturn_turn_static_auth_secret: ""
7474

7575
# The external IP address of the machine where Coturn is.
76+
# If do not define an IP address here or in `matrix_coturn_turn_external_ip_addresses`, auto-detection via an EchoIP service will be done.
77+
# See `matrix_coturn_turn_external_ip_address_auto_detection_enabled`
7678
matrix_coturn_turn_external_ip_address: ''
77-
matrix_coturn_turn_external_ip_addresses: ["{{ matrix_coturn_turn_external_ip_address }}"]
79+
matrix_coturn_turn_external_ip_addresses: "{{ [matrix_coturn_turn_external_ip_address] if matrix_coturn_turn_external_ip_address != '' else [] }}"
80+
81+
# Controls whether external IP address auto-detection should be attempted.
82+
# We try to do this if there is no external IP address explicitly configured and if an EchoIP service URL is specified.
83+
# See matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url
84+
matrix_coturn_turn_external_ip_address_auto_detection_enabled: "{{ matrix_coturn_turn_external_ip_addresses | length == 0 and matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url != '' }}"
85+
86+
# Specifies the address of the EchoIP service (https://github.com/mpolden/echoip) to use for detecting the external IP address.
87+
# By default, we use the official public instance.
88+
matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url: https://ifconfig.co/json
89+
90+
# Controls whether SSL certificates will be validated when contacting the EchoIP service (matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url)
91+
matrix_coturn_turn_external_ip_address_auto_detection_echoip_validate_certs: true
92+
93+
matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_retries_count: "{{ devture_playbook_help_geturl_retries_count }}"
94+
matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_retries_delay: "{{ devture_playbook_help_geturl_retries_delay }}"
7895

7996
matrix_coturn_allowed_peer_ips: []
8097

roles/custom/matrix-coturn/tasks/setup_install.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
---
22

3+
- when: matrix_coturn_turn_external_ip_address_auto_detection_enabled | bool
4+
block:
5+
- when: matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url == ''
6+
name: Fail if enabled, but EchoIP service URL unset
7+
ansible.builtin.fail:
8+
msg: "To use the external IP address auto-detection feature, you need to set matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url"
9+
10+
# NOTE:
11+
# `ansible.builtin.uri` does not provide a way to configure whether IPv4 or IPv6 is used.
12+
# Luckily, the default instance we use does not define AAAA records for now, so it's always IPv4.
13+
- name: Fetch IP address information from EchoIP service
14+
ansible.builtin.uri:
15+
url: "{{ matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url }}"
16+
headers:
17+
Content-Type: application/json
18+
follow_redirects: none
19+
validate_certs: "{{ matrix_coturn_turn_external_ip_address_auto_detection_echoip_validate_certs }}"
20+
register: result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response
21+
ignore_errors: true
22+
check_mode: false
23+
retries: "{{ matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_retries_count }}"
24+
delay: "{{ matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_retries_delay }}"
25+
until: not result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response.failed
26+
27+
- when: "(result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response.failed or 'json' not in result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response)"
28+
name: Fail if EchoIP service failed
29+
ansible.builtin.fail:
30+
msg: "Failed contacting EchoIP service API at `{{ matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url }}` (controlled by `matrix_coturn_turn_external_ip_address_auto_detection_echoip_service_url`). Full error: {{ result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response }}"
31+
32+
- ansible.builtin.set_fact:
33+
matrix_coturn_turn_external_ip_address: "{{ result_matrix_coturn_turn_external_ip_address_auto_detection_echoip_response.json.ip }}"
34+
335
- name: Ensure Matrix Coturn path exists
436
ansible.builtin.file:
537
path: "{{ item.path }}"

roles/custom/matrix-coturn/templates/turnserver.conf.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ realm=turn.{{ matrix_server_fqn_matrix }}
55

66
min-port={{ matrix_coturn_turn_udp_min_port }}
77
max-port={{ matrix_coturn_turn_udp_max_port }}
8-
{% for ip in matrix_coturn_turn_external_ip_addresses|select('ne', '') %}
8+
{% for ip in matrix_coturn_turn_external_ip_addresses %}
99
external-ip={{ ip }}
1010
{% endfor %}
1111

0 commit comments

Comments
 (0)