|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
## imports
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
import logging.handlers
|
|
|
|
@ -33,38 +32,107 @@ client = ovh.Client(
|
|
|
|
|
consumer_key=os.getenv('CONSUMER_KEY'), # Consumer Key
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
api_token=os.getenv('API_TOKEN')
|
|
|
|
|
provider = 'hetzner'
|
|
|
|
|
TMPFILE = '/tmp/lastIP'
|
|
|
|
|
|
|
|
|
|
rid=None
|
|
|
|
|
zid=None
|
|
|
|
|
|
|
|
|
|
WEBRTCCONFIGFILE = '/etc/bigbluebutton/bbb-webrtc-sfu/production.yml'
|
|
|
|
|
# WEBRTCCONFIGFILE = 'test.yml'
|
|
|
|
|
|
|
|
|
|
def get_ip():
|
|
|
|
|
r = requests.get('http://ipinfo.io')
|
|
|
|
|
return (r.json()['ip'])
|
|
|
|
|
r = requests.get('http://ipinfo.io').json()
|
|
|
|
|
return (r['ip'])
|
|
|
|
|
|
|
|
|
|
def get_zone_id(zone):
|
|
|
|
|
if zid != None: return zid
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = requests.get(
|
|
|
|
|
url="https://dns.hetzner.com/api/v1/zones",
|
|
|
|
|
headers={
|
|
|
|
|
"Auth-API-Token": api_token,
|
|
|
|
|
}
|
|
|
|
|
).json()
|
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
|
my_logger.critical('HTTP Request failed')
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
id = [z.get('id') for z in response.get('zones') if z.get('name')==zone]
|
|
|
|
|
|
|
|
|
|
rid=id[0]
|
|
|
|
|
return rid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_record_id(zone,subdomain):
|
|
|
|
|
result = client.get('/domain/zone/' + zone + '/record', fieldType='A', subDomain=subdomain )
|
|
|
|
|
#print (json.dumps(result, indent=4))
|
|
|
|
|
if rid != None: return rid
|
|
|
|
|
|
|
|
|
|
if provider == 'ovh':
|
|
|
|
|
id = client.get('/domain/zone/' + zone + '/record', fieldType='A', subDomain=subdomain )
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
response = requests.get(
|
|
|
|
|
url="https://dns.hetzner.com/api/v1/records",
|
|
|
|
|
params={
|
|
|
|
|
"zone_id": get_zone_id(zone),
|
|
|
|
|
},
|
|
|
|
|
headers={
|
|
|
|
|
"Auth-API-Token": api_token,
|
|
|
|
|
}
|
|
|
|
|
).json()
|
|
|
|
|
|
|
|
|
|
id = [rec.get('id') for rec in response.get('records') if rec.get('name')== subdomain]
|
|
|
|
|
|
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
|
my_logger.critical('HTTP Request failed')
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
zid = id[0]
|
|
|
|
|
return zid
|
|
|
|
|
|
|
|
|
|
return result[0]
|
|
|
|
|
|
|
|
|
|
def set_record(zone, subdomain, ip):
|
|
|
|
|
id=get_record_id(zone,subdomain)
|
|
|
|
|
if provider == 'ovh':
|
|
|
|
|
rid=get_record_id(zone,subdomain)
|
|
|
|
|
result = client.put('/domain/zone/' + zone + '/record/' + str(rid), subDomain=subdomain, target=rip, ttl=60)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
if result != None:
|
|
|
|
|
my_logger.critical('couldn\'t change record')
|
|
|
|
|
exit(1)
|
|
|
|
|
result = client.post('/domain/zone/' + zone + '/refresh')
|
|
|
|
|
return result
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
response = requests.put(
|
|
|
|
|
url="https://dns.hetzner.com/api/v1/records/" + get_record_id(zone,subdomain),
|
|
|
|
|
headers={
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"Auth-API-Token": api_token,
|
|
|
|
|
},
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"value": ip,
|
|
|
|
|
"ttl": 60,
|
|
|
|
|
"type": "A",
|
|
|
|
|
"name": subdomain,
|
|
|
|
|
"zone_id": get_zone_id(zone)
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
return (None if response.status_code == requests.codes.ok else False)
|
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
|
my_logger.critical('HTTP Request failed')
|
|
|
|
|
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
|
|
|
|
@ -78,7 +146,6 @@ if len(sys.argv) > 2:
|
|
|
|
|
webrtc_config = read_config_file()
|
|
|
|
|
|
|
|
|
|
lastIP = webrtc_config['freeswitch']['ip']
|
|
|
|
|
|
|
|
|
|
currentIP = get_ip()
|
|
|
|
|
|
|
|
|
|
if lastIP == currentIP:
|
|
|
|
@ -87,7 +154,7 @@ if len(sys.argv) > 2:
|
|
|
|
|
|
|
|
|
|
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"])
|
|
|
|
|
|
|
|
|
|