Files
network-roaming/detector.py

40 lines
1.2 KiB
Python

import requests
import socket
import subprocess
from services.fast5330br1 import Fast5330bR1
from services.gandi import GandiClient
MODEM_ADDRESS = 'mabbox.bytel.fr'
ROUTER_ADDRESS = 'kynes.local:8000'
eth_modem = Fast5330bR1(MODEM_ADDRESS)
eth_modem_is_up = eth_modem.check_internet_status()
r = requests.get(f"http://{ROUTER_ADDRESS}/status")
router_status = r.json()['status']
new_router_status = None
if eth_modem_is_up and router_status == 'usb':
new_router_status = 'eth'
elif not eth_modem_is_up and router_status == 'eth':
new_router_status = 'usb'
if new_router_status:
requests.post(f"http://{ROUTER_ADDRESS}/status/{new_router_status}", auth=(router_user, router_pass))
router_status = new_router_status
current_dns_host = socket.gethostbyname_ex('dorfsvald.net')
expected_host = ETH_HOST if router_status == "eth" else USB_HOST
if current_dns_host != expected_host:
client = GandiClient(domain, gandi_api_key, '@', 'A')
dns_updating = client.get_host() == expected_host
if not dns_updating:
client.set_host(expected_host)
else:
if expected_host == ETH_HOST:
subprocess.run(['systemctl', 'start', 'wg-quick@wg0'])
else:
subprocess.run(['systemctl', 'stop', 'wg-quick@wg0'])