diff --git a/config/helper.py b/config/helper.py new file mode 100644 index 0000000..517b768 --- /dev/null +++ b/config/helper.py @@ -0,0 +1,63 @@ +import subprocess, sys + +def get_ssid_linux(): + out = subprocess.check_output(["nmcli", "-t", "-f", "active,ssid", "dev", "wifi"], text=True) + for line in out.splitlines(): + active, ssid = line.split(":",1) + if active == "yes": + return ssid + return None + +def get_ssid_mac(): + out = subprocess.check_output(["/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport", "-I"], text=True) + for line in out.splitlines(): + if "SSID:" in line: + return line.split("SSID:")[1].strip() + return None + +def get_ssid_windows(): + out = subprocess.check_output(["netsh", "wlan", "show", "interfaces"], text=True, shell=True) + for line in out.splitlines(): + if "SSID" in line and "BSSID" not in line: + return line.split(":",1)[1].strip() + return None + +def detect_ssid(): + import platform + plat = platform.system() + try: + if plat == "Linux": return get_ssid_linux() + if plat == "Darwin": return get_ssid_mac() + if plat == "Windows": return get_ssid_windows() + except Exception: + return None +import yaml, keyring, requests, sqlite3, argparse, platform, subprocess + +def load_config(path): + return yaml.safe_load(open(path)) + +def choose_profile(config, ssid, override): + if override: + return config['profiles'].get(override) + for name, p in config['profiles'].items(): + if ssid in p.get('ssids', []): + return name, p + return config.get('default_profile'), config['profiles'][config.get('default_profile')] + +def get_creds(key): + return keyring.get_password('wifi_login', key) + +def portal_login(profile, creds): + # very simplified: POST to login_url with username/password fields + resp = requests.get("http://clients3.google.com/generate_204", allow_redirects=True, timeout=5) + if resp.status_code == 204: + return True, "No portal" + # redirect -> parse and post + # ... form parsing and submission ... + return False, "Portal flow not implemented" + +def log(db, profile, ssid, status, msg): + conn = sqlite3.connect(db); c = conn.cursor() + c.execute("INSERT INTO login_attempts(profile, ssid, status, msg) VALUES(?,?,?,?)", + (profile, ssid, status, msg)) + conn.commit(); conn.close() diff --git a/config/multi.yaml b/config/multi.yaml new file mode 100644 index 0000000..d7ccaec --- /dev/null +++ b/config/multi.yaml @@ -0,0 +1,29 @@ +# ~/.config/wifi-login/config.yaml +default_profile: home + +profiles: + home: + ssids: ["MyHomeSSID", "MyHomeSSID_Guest"] + auth: + type: portal + login_url: "https://captive.example/login" + username_key: "home_username" # lookup in keyring + password_key: "home_password" # lookup in keyring + form: + username_field: "user" + password_field: "pass" + submit_selector: "button[type=submit]" + + work: + ssids: ["ACME_WiFi"] + auth: + type: radius + username_key: "work_username" + password_key: "work_password" + eap: "PEAP" + phase2: "MSCHAPV2" + + cafe: + ssids: ["StarCafe"] + auth: + type: none # just a normal open/psk WiFi; maybe no additional login required