Compare commits
3 Commits
5e46e7e14e
...
21d13bf1eb
| Author | SHA1 | Date | |
|---|---|---|---|
| 21d13bf1eb | |||
| d25e6d9de6 | |||
| 827c90913a |
38
detector.py
Normal file
38
detector.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import requests
|
||||
import socket
|
||||
|
||||
import subprocess
|
||||
|
||||
from services.fast5330br1 import Fast5330bR1
|
||||
|
||||
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'])
|
||||
@@ -12,7 +12,7 @@ import configparser
|
||||
from fastapi import FastAPI, Depends, HTTPException, status
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
|
||||
from mw40v import Mw40V
|
||||
from services.mw40v import Mw40V
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@@ -105,11 +105,11 @@ def is_usb_on():
|
||||
|
||||
def switch_status(turn_on):
|
||||
if turn_on:
|
||||
update_usb_modem(turn_on)
|
||||
update_dhcp_conf(turn_on)
|
||||
update_usb_modem(True)
|
||||
update_dhcp_conf(True)
|
||||
else:
|
||||
update_dhcp_conf(turn_on)
|
||||
update_usb_modem(turn_on)
|
||||
update_dhcp_conf(False)
|
||||
update_usb_modem(False)
|
||||
|
||||
subprocess.run(['/usr/bin/sudo', '/usr/sbin/service', 'dhcpcd', 'restart'])
|
||||
subprocess.run(['/usr/bin/sudo', '/usr/sbin/service', 'networking', 'restart'])
|
||||
0
services/__init__.py
Normal file
0
services/__init__.py
Normal file
33
services/fast5330br1.py
Normal file
33
services/fast5330br1.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import requests
|
||||
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
|
||||
|
||||
|
||||
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
|
||||
34
services/gandi.py
Normal file
34
services/gandi.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
|
||||
class GandiClient:
|
||||
base_url = "https://api.gandi.net/v5/livedns/domains"
|
||||
|
||||
def __init__(self, domain, api_key, record_name, record_type):
|
||||
self.domain = domain
|
||||
self.api_key = api_key
|
||||
self.record_name = record_name
|
||||
self.record_type = record_type
|
||||
|
||||
def _get_headers(self):
|
||||
return {
|
||||
'Authorization': f'Bearer {self.api_key}',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def _get_url(self):
|
||||
return f"{self.base_url}/{self.domain}/records/{self.record_name}/{self.record_type}"
|
||||
|
||||
def get_host(self):
|
||||
response = requests.request("PUT", self._get_url(), headers=self._get_headers())
|
||||
|
||||
return response.json()['rrset_values'][0]
|
||||
|
||||
def set_host(self, host, tls=3600):
|
||||
payload = json.dumps({
|
||||
"rrset_ttl": tls,
|
||||
"rrset_values": [host]
|
||||
})
|
||||
|
||||
requests.request("PUT", self._get_url(), headers=self._get_headers(), data=payload)
|
||||
Reference in New Issue
Block a user