#!/usr/bin/env python3 """Rewrite leftover otantik-hub slug on RB4011 hotspot HTML/JS. Does not touch RB2011.""" from __future__ import annotations import re import sys from mk_nko_wg_exec import connect_mk TEXT = ( "login.html", "alogin.html", "status.html", "error.html", "logout.html", "ticket.html", "chat.html", "apropos.html", "services.html", "code.html", "pay.html", "md5.js", ) def rewrite(data: bytes) -> bytes: t = data.decode("utf-8", "replace") n = re.sub(r"otantik-hub(?!-nkoabang)", "otantik-hub-nkoabang", t) n = n.replace("Otantik Hub Nkoabang Nkoabang", "Otantik Hub Nkoabang") return n.encode("utf-8") def main() -> None: print("connecting 4011", flush=True) jump, mk = connect_mk() try: stdin, stdout, stderr = mk.exec_command("/system identity print", timeout=30) idn = stdout.read().decode("utf-8", "replace") print("identity", idn.strip(), flush=True) if "Nkoabang" not in idn: raise SystemExit("refusing: not RB4011 Nkoabang") sftp = mk.open_sftp() extra = [] try: extra = ["scripts/" + fn for fn in sftp.listdir("hotspot/scripts") if fn.lower().endswith(".js")] except OSError: pass changed = 0 for rel in list(TEXT) + extra: path = "hotspot/" + rel try: with sftp.file(path, "rb") as f: raw = f.read() except OSError as e: print("skip", path, e, flush=True) continue new = rewrite(raw) if new == raw: print("ok unchanged", rel, flush=True) continue with sftp.file(path, "wb") as f: f.write(new) changed += 1 print("patched", rel, flush=True) with sftp.file("hotspot/pay.html", "rb") as f: pay = f.read().decode("utf-8", "replace") print('pay z nkoabang', 'z="otantik-hub-nkoabang"' in pay, flush=True) print("pay leftover hub buy", "/wifi/otantik-hub/buy" in pay, flush=True) print("pay nkoabang buy", "/wifi/otantik-hub-nkoabang/buy" in pay, flush=True) print("files_changed", changed, flush=True) sftp.close() finally: mk.close() jump.close() print("DONE 4011 zone slug", flush=True) if __name__ == "__main__": sys.stdout.reconfigure(line_buffering=True) main()