diff options
-rw-r--r-- | .gitmodules | 6 | ||||
m--------- | lass/5pkgs/autowifi | 0 | ||||
-rw-r--r-- | lass/5pkgs/autowifi/autowifi.py | 228 | ||||
-rw-r--r-- | lass/5pkgs/autowifi/default.nix | 1 |
4 files changed, 6 insertions, 229 deletions
diff --git a/.gitmodules b/.gitmodules index f35a9250d..15d1b41de 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,9 @@ [submodule "submodules/krops"] path = submodules/krops url = https://cgit.krebsco.de/krops +[submodule "lass/5pkgs/autowifi"] + path = lass/5pkgs/autowifi + url = https://github.com/Lassulus/autowifi +[submodule "lass/5pkgs/--force"] + path = lass/5pkgs/--force + url = https://github.com/Lassulus/autowifi diff --git a/lass/5pkgs/autowifi b/lass/5pkgs/autowifi new file mode 160000 +Subproject cf3ae8f6fe285eab67db4f36f9a3da3762c3531 diff --git a/lass/5pkgs/autowifi/autowifi.py b/lass/5pkgs/autowifi/autowifi.py deleted file mode 100644 index fa3d007e7..000000000 --- a/lass/5pkgs/autowifi/autowifi.py +++ /dev/null @@ -1,228 +0,0 @@ -import subprocess -import time -import urllib.request -import logging -import argparse -import socket -import struct -import signal -import os - -wifiDB = '' -logger = logging.getLogger() -got_signal = False - - -def signal_handler(signum, frame): - global got_signal - got_signal = True - - -def get_default_gateway() -> str: - """Read the default gateway directly from /proc.""" - with open("/proc/net/route") as fh: - for line in fh: - fields = line.strip().split() - if fields[1] != '00000000' or not int(fields[3], 16) & 2: - continue - - return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16))) - - -def connect(ssid, psk=None): - subprocess.run( - ["nmcli", "connection", "delete", "autowifi"], - stdout=subprocess.PIPE, - ) - logging.info('connecting to %s', ssid) - if psk is None: - subprocess.run( - [ - "nmcli", - "device", - "wifi", - "connect", - ssid, - "name", - "autowifi", - ], - stdout=subprocess.PIPE, - ) - else: - subprocess.run( - [ - "nmcli", - "device", - "wifi", - "connect", - ssid, - "name", - "autowifi", - "password", - psk, - ], - stdout=subprocess.PIPE, - ) - time.sleep(5) - - -def scan(): - logging.debug('scanning wifis') - wifis_raw = subprocess.check_output([ - "nmcli", - "-t", - "device", - "wifi", - "list", - "--rescan", - "yes", - ]) - wifis_list = wifis_raw.split(b'\n') - logging.debug('scanning wifis finished') - wifis = [] - for line in wifis_list: - logging.debug(line) - ls = line.split(b':') - if len(ls) == 8: - wifis.append({ - "ssid": ls[1], - "signal": int(ls[5]), - "crypto": ls[7] - }) - return wifis - - -def get_known_wifis(): - wifis_lines = [] - with open(wifiDB) as f: - wifis_lines = f.read().splitlines() - wifis = [] - for line in wifis_lines: - ls = line.split('/') - wifis.append({"ssid": ls[0].encode(), "psk": ls[1].encode()}) - return wifis - - -def check_network(): - logging.debug('checking network') - - global got_signal - if got_signal: - logging.info('got disconnect signal') - got_signal = False - return False - else: - gateway = get_default_gateway() - if gateway: - response = subprocess.run( - [ - 'ping', - '-q', - '-c', - '1', - gateway, - ], - stdout=subprocess.PIPE, - ) - if response.returncode == 0: - logging.debug('host %s is up', gateway) - return True - else: - logging.debug('host %s is down', gateway) - return False - else: - logging.debug('no gateway') - return False - - -def check_internet(): - logging.debug('checking internet') - - try: - with open('./dummy_internet') as f: - dummy_content = f.read() - if dummy_content == 'xxx\n': - return True - beacon = urllib.request.urlopen('http://krebsco.de/secret') - except Exception as e: # noqa - logging.debug(e) - logging.info('no internet exc') - return False - if beacon.read() == b'1337\n': - return True - logging.info('no internet oh') - return False - - -def is_wifi_open(wifi): - if wifi['crypto'] == b'': - return True - else: - return False - - -def is_wifi_seen(wifi, seen_wifis): - for seen_wifi in seen_wifis: - if seen_wifi["ssid"] == wifi["ssid"]: - return True - return False - - -def main(): - parser = argparse.ArgumentParser() - - parser.add_argument( - '-c', '--config', - dest='config', - help='wifi config file to use', - default='/etc/wifis', - ) - - parser.add_argument( - '-l', '--loglevel', - dest='loglevel', - help='loglevel to use', - default=logging.INFO, - ) - - parser.add_argument( - '-p', '--pidfile', - dest='pidfile', - help='file to write the pid to', - default=None, - ) - - args = parser.parse_args() - - global wifiDB - wifiDB = args.config - logger.setLevel(args.loglevel) - - signal.signal(signal.SIGUSR1, signal_handler) - - if args.pidfile: - with open(args.pidfile, 'w+') as f: - f.write(str(os.getpid())) - - while True: - if not check_network(): - wifis = scan() - known_wifis = get_known_wifis() - known_seen_wifis = [ - wifi for wifi in known_wifis if is_wifi_seen(wifi, wifis) - ] - for wifi in known_seen_wifis: - connect(wifi['ssid'], wifi['psk']) - if check_network(): - break - open_wifis = filter(is_wifi_open, wifis) - for wifi in open_wifis: - connect(wifi['ssid']) - - if check_network(): - break - time.sleep(10) - - -if __name__ == '__main__': - main() diff --git a/lass/5pkgs/autowifi/default.nix b/lass/5pkgs/autowifi/default.nix deleted file mode 100644 index d565a6bb6..000000000 --- a/lass/5pkgs/autowifi/default.nix +++ /dev/null @@ -1 +0,0 @@ -pkgs.writers.writePython3Bin "autowifi" {} ./autowifi.py |