Adding gandi client and bbox modem client
This commit is contained in:
50
detector.py
50
detector.py
@@ -1,41 +1,13 @@
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
|
||||
import socket
|
||||
|
||||
import subprocess
|
||||
|
||||
from services.fast5330br1 import Fast5330bR1
|
||||
|
||||
MODEM_ADDRESS = 'mabbox.bytel.fr'
|
||||
ROUTER_ADDRESS = 'kynes.local:8000'
|
||||
|
||||
|
||||
class SecLevel1Adapter(HTTPAdapter):
|
||||
def init_poolmanager(self, *args, **kwargs):
|
||||
context = create_urllib3_context(ciphers='DEFAULT@SECLEVEL=1')
|
||||
kwargs['ssl_context'] = context
|
||||
return super(SecLevel1Adapter, self).init_poolmanager(*args, **kwargs)
|
||||
|
||||
def proxy_manager_for(self, *args, **kwargs):
|
||||
context = create_urllib3_context(ciphers='DEFAULT@SECLEVEL=1')
|
||||
kwargs['ssl_context'] = context
|
||||
return super(SecLevel1Adapter, self).proxy_manager_for(*args, **kwargs)
|
||||
|
||||
|
||||
class Fast5330bR1:
|
||||
def __init__(self, address):
|
||||
self.address = address
|
||||
|
||||
def check_internet_status(self):
|
||||
with requests.Session() as session:
|
||||
session.mount(f"https://{self.address}", SecLevel1Adapter())
|
||||
r = session.get(
|
||||
f"https://{self.address}/api/v1/summary",
|
||||
proxies={"https": "socks5h://localhost:1337"}
|
||||
)
|
||||
|
||||
result = r.json()
|
||||
|
||||
return result[0]['internet']['state'] == 2
|
||||
|
||||
|
||||
eth_modem = Fast5330bR1(MODEM_ADDRESS)
|
||||
eth_modem_is_up = eth_modem.check_internet_status()
|
||||
|
||||
@@ -50,3 +22,17 @@ elif not eth_modem_is_up and router_status == 'eth':
|
||||
|
||||
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'])
|
||||
|
||||
Reference in New Issue
Block a user