From af3f300786a6929ef2a31612f16bd0370efae7b0 Mon Sep 17 00:00:00 2001 From: rhmatzeka <155574253+rhmatzeka@users.noreply.github.com> Date: Mon, 2 Nov 2026 11:23:12 +0700 Subject: [PATCH] desktop: wifi-login opens hotspot login pages, and stays quiet while the internet works another way The watcher (started from hypr-user.lua, Super+Shift+W to check again) finds the captive portal of the WiFi you joined and opens its login page. It used to judge each WiFi on its own, so a campus WiFi that wanted a login kept opening its page and sending notifications even while USB tethering already had internet. Each default route is now checked through its own interface; while one is online, a WiFi that wants a login or does not answer is left alone with no page and no notification, and the login page opens once that connection goes away. --- .github/workflows/ci.yml | 2 + README.md | 2 +- bin/wifi-login | 302 +++++++++++++++++++++++++++++++++ config/caelestia/hypr-user.lua | 8 + linux/debian/desktop.sh | 2 + tests/wifi-login.py | 81 +++++++++ 6 files changed, 396 insertions(+), 1 deletion(-) create mode 100755 bin/wifi-login create mode 100644 tests/wifi-login.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42f1dbd..5466c9e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,8 @@ jobs: for f in config/nvim/dotfiles.lua config/caelestia/*.lua; do luac5.4 -p "$f" 2>&1 || lua5.4 -e "assert(loadfile('$f'))"; done - name: Menu (scripted answers) run: tests/menu.sh + - name: wifi-login (hotspot login watcher) + run: python3 tests/wifi-login.py - name: Dry run (everything except the desktop, which needs Debian 13) run: ./install.sh --dry-run --yes --only base,shell,tmux,nvim,terminal,browsers - name: Real run of the user-level parts in a throw-away HOME diff --git a/README.md b/README.md index 59b65cf..29b9ed2 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ irm https://raw.githubusercontent.com/rhmatzeka/dotfile/main/uninstall.ps1 | iex | `apps` | Microsoft 365 web apps (Word, Excel, PowerPoint, Outlook, OneDrive) in their own windows, plus **Open with Microsoft 365** for local files (converts PDFs to .docx, uploads the file to a private folder of your OneDrive through the Microsoft Graph API, using rclone only for the sign-in, then opens it on the web). Double-clicking Word/Excel/PowerPoint files does this; for PDFs (which stay in your PDF viewer unless you agree to change that during installation) use the Thunar right-click entry **Open with Microsoft 365**. A file that is already open in Word online is not uploaded again (OneDrive locks it); the open copy is shown instead, PDF viewer, VLC, GIMP. Pre-selected only when a graphical session is running | | `web` | Web development: PHP, Composer, MariaDB, Apache, phpMyAdmin. Services are **not** started unless you say yes (Apache listens on port 80); no passwords are set | | `rust` | Rust through the official rustup installer: stable toolchain, clippy, rustfmt, rust-analyzer | -| `desktop` | Hyprland + Caelestia shell with see-through, blurred panels and a few small patches: Bluetooth lists (bar panel and the pair-new-device page) hide devices that only show a MAC address and show the device type instead; the Bluetooth panel says so when no adapter is found instead of showing dead switches; WiFi password fields get an eye button to show what you typed; text on the transparent panels gets a thin halo so it stays readable on bright wallpapers; notifications that contain HTML, such as Instagram's, show clean text; hibernation at critical battery gets enough free memory to succeed (`/sys/power/image_size` = 0) and the laptop powers off if it still fails, instead of staying on with a black screen (AMD GPUs do not come back from a failed hibernation), plus Caelestia's btop, fastfetch, foot, micro, thunar, zed and fish configs (only where you have none). **Debian 13 only, beta.** Builds Qt 6.11 from source (1 to 2 hours) | +| `desktop` | Hyprland + Caelestia shell with see-through, blurred panels and a few small patches: Bluetooth lists (bar panel and the pair-new-device page) hide devices that only show a MAC address and show the device type instead; the Bluetooth panel says so when no adapter is found instead of showing dead switches; WiFi password fields get an eye button to show what you typed; hotspot login pages (campus or cafe WiFi) open by itself when you join, but not while the internet already works through USB tethering, LAN or another WiFi (`wifi-login`, Super+Shift+W checks again); text on the transparent panels gets a thin halo so it stays readable on bright wallpapers; notifications that contain HTML, such as Instagram's, show clean text; hibernation at critical battery gets enough free memory to succeed (`/sys/power/image_size` = 0) and the laptop powers off if it still fails, instead of staying on with a black screen (AMD GPUs do not come back from a failed hibernation), plus Caelestia's btop, fastfetch, foot, micro, thunar, zed and fish configs (only where you have none). **Debian 13 only, beta.** Builds Qt 6.11 from source (1 to 2 hours) | Checked by default: `base`, `shell`, `tmux`, `nvim` and `terminal` (plus `apps` on Linux when a graphical session is running). `web`, `rust`, `browsers` and `desktop` are only installed when you check them. diff --git a/bin/wifi-login b/bin/wifi-login new file mode 100755 index 0000000..386d8a4 --- /dev/null +++ b/bin/wifi-login @@ -0,0 +1,302 @@ +#!/usr/bin/env python3 +"""Find the captive portal (hotspot login page, MikroTik and the like) of the WiFi you joined and open it. + + wifi-login check now and open the login page if there is one (also bound to Super+Shift+W) + wifi-login --watch keep running in the background (started from hypr-user.lua): every time WiFi connects or the + network changes, check and open the login page by itself + +Each check is bound to the WiFi interface (DNS goes to the WiFi's own DHCP DNS server, HTTP through --interface), so it +stays right when another connection (USB tethering, LAN) is the default route or Tailscale has taken over DNS. +While the internet already works through another connection, the watcher leaves a WiFi that wants a login (or does +not answer) alone, without opening pages or showing notifications; it opens the login page once that connection goes. +""" +import html +import random +import re +import select +import socket +import struct +import subprocess +import sys +import time +from urllib.parse import urljoin, urlsplit, urlunsplit + +PROBES = [ + ("http://nmcheck.gnome.org/check_network_status.txt", "NetworkManager is online"), + ("http://detectportal.firefox.com/success.txt", "success"), +] +WATCH_INTERVAL = 60 # seconds between checks in --watch once everything is settled +SO_BINDTODEVICE = 25 + + +def run(*cmd): + return subprocess.run(cmd, capture_output=True, text=True).stdout + + +def notify(title, body, icon="network-wireless", urgency="normal"): + subprocess.Popen(["notify-send", "-a", "WiFi", "-u", urgency, "-i", icon, title, body], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + +def wifi_devices(): + """[(device, connection name, ip, dns server)] for every WiFi that is connected.""" + out = [] + for line in run("nmcli", "-t", "-f", "DEVICE,TYPE,STATE,CONNECTION", "dev").splitlines(): + parts = re.split(r"(?HHHHHH", tid, 0x0100, 1, 0, 0, 0) + qname + struct.pack(">HH", 1, 1) + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + s.setsockopt(socket.SOL_SOCKET, SO_BINDTODEVICE, dev.encode()) + s.settimeout(3) + for _ in range(2): + s.sendto(pkt, (server, 53)) + try: + data, _ = s.recvfrom(4096) + except socket.timeout: + continue + if struct.unpack(">H", data[:2])[0] != tid: + continue + ancount = struct.unpack(">H", data[6:8])[0] + i = 12 + len(qname) + 4 + for _ in range(ancount): + if data[i] & 0xC0 == 0xC0: + i += 2 + else: + while data[i]: + i += data[i] + 1 + i += 1 + rtype, _, _, rdlen = struct.unpack(">HHIH", data[i:i + 10]) + i += 10 + if rtype == 1 and rdlen == 4: + return socket.inet_ntoa(data[i:i + 4]) + i += rdlen + return None + except OSError: + return None + finally: + s.close() + return None + + +def system_resolve(name): + try: + return socket.gethostbyname(name) + except OSError: + return None + + +def http_get(url, dev, ip): + """GET without following redirects, through one interface. (status, location, body) or None.""" + host = urlsplit(url).hostname + port = urlsplit(url).port or 80 + cmd = ["curl", "-s", "-m", "8", "--interface", dev, "-A", "Mozilla/5.0 (captive-portal-check)", + "-o", "-", "-w", "\n%{http_code} %{redirect_url}", url] + if ip: + cmd[1:1] = ["--resolve", f"{host}:{port}:{ip}"] + r = subprocess.run(cmd, capture_output=True, text=True, errors="replace") + body, _, tail = r.stdout.rpartition("\n") + code, _, location = tail.partition(" ") + if r.returncode != 0 or code == "000": + return None + return int(code), location.strip(), body + + +def login_url_from(probe_url, status, location, body): + if 300 <= status < 400 and location: + return urljoin(probe_url, location) + patterns = [ + r'http-equiv=["\']?refresh["\']?[^>]*content=["\'][^"\']*url=([^"\'>]+)', + r'(?:window\.)?location(?:\.href)?\s*=\s*["\']([^"\']+)', + r'