You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.9 KiB

#!/usr/bin/env python
## imports
import json
import logging
import logging.handlers
import os, sys
import ovh
import requests
import subprocess
import time
import yaml
from dotenv import load_dotenv
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
formatter = logging.Formatter("dyndns-update: %(message)s")
handler.setFormatter(formatter)
my_logger.addHandler(handler)
env_path = os.path.abspath ( os.path.join ( os.curdir, os.path.relpath('.env') ) )
load_dotenv(verbose=True, dotenv_path=env_path)
## constants
client = ovh.Client(
endpoint=os.getenv('ENDPOINT'), # Endpoint of API OVH Europe (List of available endpoints)
application_key=os.getenv('APP_KEY'), # Application Key
application_secret=os.getenv('SECRET'), # Application Secret
consumer_key=os.getenv('CONSUMER_KEY'), # Consumer Key
)
TMPFILE = '/tmp/lastIP'
WEBRTCCONFIGFILE = '/etc/bigbluebutton/bbb-webrtc-sfu/production.yml'
# WEBRTCCONFIGFILE = 'test.yml'
def get_ip():
r = requests.get('http://ipinfo.io')
return (r.json()['ip'])
def get_record_id(zone,subdomain):
result = client.get('/domain/zone/' + zone + '/record', fieldType='A', subDomain=subdomain )
#print (json.dumps(result, indent=4))
return result[0]
def set_record(zone, subdomain, ip):
id=get_record_id(zone,subdomain)
result = client.put('/domain/zone/' + zone + '/record/' + str(id), subDomain=subdomain, target=ip, ttl=60)
if result != None:
my_logger.critical('couldn\'t change record')
exit(1)
result = client.post('/domain/zone/' + zone + '/refresh')
return result
def read_config_file():
with open(WEBRTCCONFIGFILE,'r') as file:
webrtc_config = yaml.safe_load(file)
return webrtc_config
def write_config_file(ip):
webrtc_config['mediasoup']['webrtc']['listenIps'][0]['announcedIp']=ip
webrtc_config['mediasoup']['plainRtp']['listenIp']['announcedIp']=ip
webrtc_config['freeswitch']['ip']=ip
with open(WEBRTCCONFIGFILE, 'w') as file:
yaml.dump(webrtc_config, file)
if len(sys.argv) > 2:
webrtc_config = read_config_file()
lastIP = webrtc_config['freeswitch']['ip']
currentIP = get_ip()
if lastIP == currentIP:
my_logger.debug ('IP has not changed since last call')
exit(1)
if (set_record (sys.argv[1], sys.argv[2], currentIP) == None ):
write_config_file(currentIP)
subprocess.run(["ip","a","del",lastIP + '/32',"dev","lo"])
subprocess.run(["ip","a","add",currentIP,"dev","lo"])
my_logger.debug('IP of ' + sys.argv[2] + '.' + sys.argv[1] + ' successfully changed to ' + currentIP)
else:
my_logger.critical('Couldn\'t change IP')
else:
print('Updates IP record of SUBDOMAIN in ZONE to current IP from ipinfo.io\n\nUsage:\n' + sys.argv[0] + ' ZONE SUBDOMAIN')