This document explains how to enable VoIP relaying on your homeserver with TURN.
The palpo Matrix homeserver supports integration with TURN server via the TURN server REST API. This allows the homeserver to generate credentials that are valid for use on the TURN server through the use of a secret shared between the homeserver and the TURN server.
This documentation provides coturn server configuration.
For TURN relaying to work, the TURN service must be hosted on a server/endpoint with a public IP.
Hosting TURN behind NAT requires port forwarding and for the NAT gateway to have a public IP. However, even with appropriate configuration, NAT is known to cause issues and to often not work.
Afterwards, the homeserver needs some further configuration.
Your homeserver configuration file has section [turn]
for turn server configuation.
As an example, here is the relevant section of the config file for matrix.org
. The
turn_uris
are appropriate for TURN servers listening on the default ports, with no TLS.
After updating the homeserver configuration, you must restart palpo:
... and then reload any clients (or wait an hour for them to refresh their settings).
coturn is a free open source implementation of TURN and STUN Server. The TURN Server is a VoIP media traffic NAT traversal server and gateway.
The TURN daemon coturn
is available from a variety of sources such as native package managers, or installation from source.
Just install the debian package:
This will install and start a systemd service called coturn
.
Download the latest release from github. Unpack it and cd
into the directory.
Configure it:
You may need to install libevent2
: if so, you should do so in
the way recommended by your operating system. You can ignore
warnings about lack of database support: a database is unnecessary
for this purpose.
Build and install it:
Create a compose.yml
file, add coturn configuation:
then run
More install details and options can be found here.
Create or edit the config file in /etc/turnserver.conf
. The relevant
lines, with example values, are:
See turnserver.conf
for explanations of the options. One way to generate
the static-auth-secret
is with pwgen
:
A realm
must be specified, but its value is somewhat arbitrary. (It is
sent to clients as part of the authentication flow.) It is conventional to
set it to be your server name.
You will most likely want to configure coturn
to write logs somewhere. The
easiest way is normally to send them to the syslog:
(in which case, the logs will be available via journalctl -u coturn
on a
systemd system). Alternatively, coturn
can be configured to write to a
logfile - check the example config file supplied with coturn
.
Consider your security settings. TURN lets users request a relay which will connect to arbitrary IP addresses and ports. The following configuration is suggested as a minimum starting point:
Also consider supporting TLS/DTLS. To do this, add the following settings
to turnserver.conf
:
In this case, replace the turn:
schemes in the turn_uris
settings below
with turns:
.
We recommend that you only try to set up TLS/DTLS once you have set up a basic installation and got it working.
NB: If your TLS certificate was provided by Let's Encrypt, TLS/DTLS will not work with any Matrix client that uses Chromium's WebRTC library. This currently includes Element Android & iOS; for more details, see their respective issues as well as the underlying WebRTC issue. Consider using a ZeroSSL certificate for your TURN server as a working alternative.
Ensure your firewall allows traffic into the TURN server on the ports you've configured it to listen on (By default: 3478 and 5349 for TURN traffic (remember to allow both TCP and UDP traffic), and ports 49152-65535 for the UDP relay.)
If your TURN server is behind NAT, the NAT gateway must have an external,
publicly-reachable IP address. You must configure coturn
to advertise that
address to connecting clients:
You may optionally limit the TURN server to listen only on the local address that is mapped by NAT to the external address:
If your NAT gateway is reachable over both IPv4 and IPv6, you may
configure coturn
to advertise each available address:
When advertising an external IPv6 address, ensure that the firewall and network settings of the system running your TURN server are configured to accept IPv6 traffic, and that the TURN server is listening on the local IPv6 address that is mapped by NAT to the external IPv6 address.
(Re)start the turn server:
If you used the Debian package (or have set up a systemd unit yourself):
If you built from source:
The normal symptoms of a misconfigured TURN server are that calls between devices on different networks ring, but get stuck at "call connecting". Unfortunately, troubleshooting this can be tricky.
Here are a few things to try:
Check that you have opened your firewall to allow TCP and UDP traffic to the TURN ports (normally 3478 and 5349).
Check that you have opened your firewall to allow UDP traffic to the UDP relay ports (49152-65535 by default).
Try disabling TLS/DTLS listeners and enable only its (unencrypted) TCP/UDP listeners. (This will only leave signaling traffic unencrypted; voice & video WebRTC traffic is always encrypted.)
Some WebRTC implementations (notably, that of Google Chrome) appear to get
confused by TURN servers which are reachable over IPv6 (this appears to be
an unexpected side-effect of its handling of multiple IP addresses as
defined by
draft-ietf-rtcweb-ip-handling
).
Try removing any AAAA records for your TURN server, so that it is only reachable over IPv4.
If your TURN server is behind NAT:
double-check that your NAT gateway is correctly forwarding all TURN
ports (normally 3478 & 5349 for TCP & UDP TURN traffic, and 49152-65535 for the UDP
relay) to the NAT-internal address of your TURN server. If advertising
both IPv4 and IPv6 external addresses via the external-ip
option, ensure
that the NAT is forwarding both IPv4 and IPv6 traffic to the IPv4 and IPv6
internal addresses of your TURN server. When in doubt, remove AAAA records
for your TURN server and specify only an IPv4 address as your external-ip
.
ensure that your TURN server uses the NAT gateway as its default route.
Enable more verbose logging, in coturn
via the verbose
setting:
or with eturnal
with the shell command eturnalctl loglevel debug
or in the configuration file (the service needs to reload for it to become effective):
... and then see if there are any clues in its logs.
If you are using a browser-based client under Chrome, check
chrome://webrtc-internals/
for insights into the internals of the
negotiation. On Firefox, check the "Connection Log" on about:webrtc
.
(Understanding the output is beyond the scope of this document!)
You can test your Matrix homeserver TURN setup with https://test.voip.librepush.net/. Note that this test is not fully reliable yet, so don't be discouraged if the test fails. Here is the github repo of the source of the tester, where you can file bug reports.
There is a WebRTC test tool at https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/. To use it, you will need a username/password for your TURN server. You can either:
look for the GET /_matrix/client/r0/voip/turnServer
request made by a
matrix client to your homeserver in your browser's network inspector. In
the response you should see username
and password
. Or:
Use the following shell commands for coturn
:
or for eturnal
Or (coturn only): Temporarily configure coturn
to accept a static
username/password. To do this, comment out use-auth-secret
and
static-auth-secret
and add the following:
Note: these settings will not take effect unless use-auth-secret
and static-auth-secret
are disabled.
Restart coturn after changing the configuration file.
Remember to restore the original settings to go back to testing with Matrix clients!
If the TURN server is working correctly, you should see at least one relay
entry in the results.