Building/configuring a TURN server

I scripted a quick and dirty fix for the dynamic IP problem in my case.
That might help others until there will be an official solution.

#!/usr/bin/python3                                                              

import socket, shutil

# Read the current coturn config                                                
#                                                                               
config = {}
with open('/etc/coturn/freedombox.conf') as f:
    for line in f:
        key, value = line.partition("=")[::2]
        config[key.strip()] = value.strip()


# Get current IP of TLD                                                         
#                                                                               
current_ip = socket.gethostbyname(config['realm'])

print('Old IP: %s' % config['external-ip'])
print('Current IP: %s' % current_ip)

# If the IP hasn't change, simply do nothing                                    
#                                                                               
if current_ip == config['external-ip']:                                         
    print("Still the same IP (%s)." % config['external-ip'])                    
    print("Doing nothing.")                                                     
                                                                                
# If the IP has actually changed                                                
#                                                                               
else:                                                                           
    print("The IP has changed.")

    # Set external-ip to new value and write it to a new file                   
    #                                                                           
    config['external-ip'] = current_ip
    with open('/etc/coturn/freedombox.conf.new','w') as f:
        for key, value in config.items():
            if value is not "":
                f.write('%s = %s\n' % (key, value))
            else:
                f.write('%s' % key)
                continue
    print("Changed coturn IP to %s." % current_ip)

    try:
        shutil.copyfile('/etc/coturn/freedombox.conf', '/etc/coturn/freedombox.\
conf.bak')
        print("Backuped old config.")
    except:
	print("Could not backup old config!")

    try:
        shutil.copyfile('/etc/coturn/freedombox.conf.new', '/etc/coturn/freedom\
box.conf')
        print("Substituted old config with the new.")
    except:
	print("Could not substitute config file!")

# Restart the coturn service                                                
    #                                                                           
    try:
        os.system("systemctl restart coturn")
        print("Restarted coturn service.")
    except:
        print("Could not restart coturn service!")

With sudo crontab -e I let this script fire every 8 hours.

0 4,12,20 * * *	     python3 /root/update_coturn_external_ip.py

Until now looks like it works smoothely.

1 Like