#!/usr/bin/env python3
"""Re-patch Nkoabang hotspot HTML via remove+write (MikroTik SFTP putfo is flaky)."""
from __future__ import annotations
import re
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import paramiko
from mk_rb4011_exec import JUMP, MK
FONT_RE = re.compile(
r'[ \t]*]+(?:fonts\.googleapis\.com|fonts\.gstatic\.com)[^>]*>\s*',
re.I,
)
FAST_MARK = "wo-cna-fast"
FAST_CSS = (
'"
)
FILES = [
"hotspot/login.html",
"hotspot/alogin.html",
"hotspot/status.html",
"hotspot/ticket.html",
"hotspot/pay.html",
"hotspot/code.html",
"hotspot/error.html",
"hotspot/logout.html",
"hotspot/apropos.html",
"hotspot/services.html",
"hotspot/chat.html",
]
def patch_html(raw: str) -> str:
html = FONT_RE.sub("", raw)
if FAST_MARK not in html:
if re.search(r"", html, re.I):
html = re.sub(r"", FAST_CSS + "", html, count=1, flags=re.I)
else:
html = FAST_CSS + html
return html
def main() -> None:
jump = paramiko.SSHClient()
jump.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jump.connect(JUMP[0], username=JUMP[1], password=JUMP[2], timeout=25, allow_agent=False, look_for_keys=False)
chan = jump.get_transport().open_channel("direct-tcpip", (MK[0], 22), ("127.0.0.1", 0))
mk = paramiko.SSHClient()
mk.set_missing_host_key_policy(paramiko.AutoAddPolicy())
mk.connect(MK[0], username=MK[1], password=MK[2], sock=chan, timeout=25, allow_agent=False, look_for_keys=False)
try:
sftp = mk.open_sftp()
for remote in FILES:
try:
with sftp.file(remote, "r") as f:
raw = f.read()
text = raw.decode("utf-8", "replace") if isinstance(raw, bytes) else str(raw)
except Exception as e:
print("SKIP read", remote, type(e).__name__)
continue
new = patch_html(text)
stdin, stdout, stderr = mk.exec_command('/file remove "' + remote + '"', timeout=20)
stdout.read()
with sftp.file(remote, "w") as f:
f.write(new)
print("WROTE", remote, len(text), "->", len(new), "fonts", "fonts.googleapis.com" in new, "fast", FAST_MARK in new)
sftp.close()
sftp = mk.open_sftp()
with sftp.file("hotspot/login.html", "r") as f:
chk = f.read()
chk = chk.decode("utf-8", "replace") if isinstance(chk, bytes) else str(chk)
print("VERIFY login fonts", "fonts.googleapis.com" in chk, "fast", FAST_MARK in chk, "overlay", "wo-pay-scrim" in chk, "bytes", len(chk))
sftp.close()
finally:
mk.close()
jump.close()
if __name__ == "__main__":
main()