TURN (Traversal Using Relay NAT) and STUN (Session Traversal Utilities for NAT) servers are used to help establish and maintain real-time communications, such as VoIP calls, video conferencing, and online gaming, between devices on different networks.

When two devices attempt to communicate with each other, they first need to exchange information about their IP addresses and network settings. However, when devices are located behind a NAT (Network Address Translation) router, this information may not be directly available, making it difficult to establish a direct connection between the devices.

This is where TURN and STUN servers come in.

STUN servers provide a way for devices to discover their public IP address and port number, which can be used to establish a direct connection between the devices. When a device sends a request to a STUN server, the server responds with the device’s public IP address and port number. This information can then be used to establish a direct connection between the devices, if possible.

However, in some cases, a direct connection may not be possible due to firewalls or other network restrictions. This is where TURN servers come in. A TURN server acts as a relay, allowing devices to communicate with each other even if they are unable to establish a direct connection. When a direct connection is not possible, devices can send their data to the TURN server, which relays it to the other device.

In summary, STUN servers help devices discover their public IP addresses and port numbers, which can be used to establish a direct connection between devices when possible. TURN servers act as relays when a direct connection is not possible, allowing devices to communicate with each other even if they are located behind firewalls or other network restrictions.

Here is a basic configuration for a TURN server using the open-source software Coturn:

Install Coturn:

sudo apt-get install coturn

Configure Coturn by editing the turnserver.conf file:

sudo nano /etc/turnserver.conf

Set the listening IP address and port number for the TURN server:

listening-ip=
listening-port=

Configure authentication by setting a username and password:

user=:

Set the realm for the TURN server, which is used to identify the domain:

realm=

Set the type of relay to be used by the server:

relay-type=

Common values for include udp, tcp, and tls.

Set the maximum transmission unit (MTU) size for the relay packets:

mtu=

Enable verbose logging for debugging purposes:

verbose

Save and close the turnserver.conf file, and then start the TURN server:

sudo systemctl start coturn