#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Re-apply offline TM + deploy updated policies/seed/browser-launch to physical ops-neko.""" import base64 import json import sys from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] FILES = { "/opt/douyinWEB/backend/neko/policies/policies.json": ROOT / "backend/neko/policies/policies.json", "/opt/douyinWEB/backend/neko/browser-launch.sh": ROOT / "backend/neko/browser-launch.sh", "/opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Preferences": ROOT / "backend/neko/tampermonkey-seed/Default/Preferences", } INSIDE = r''' import json, shutil from pathlib import Path EXT = "dhdgffkkebhmkfjojejmpbldmpobfkfo" BASE = Path("/home/neko/.config/google-chrome/Default") SEED_EXT = Path("/etc/neko/tampermonkey-seed/Default/Extensions") / EXT SEED_PREF = Path("/etc/neko/tampermonkey-seed/Default/Preferences") def load(p): try: return json.loads(p.read_text(encoding="utf-8")) except Exception: return {} for p in ( BASE / "Extensions" / EXT, BASE / "Local Extension Settings" / EXT, BASE / "Managed Extension Settings" / EXT, ): if p.exists(): shutil.rmtree(p, ignore_errors=True) print("removed", str(p)) (BASE / "Extensions").mkdir(parents=True, exist_ok=True) shutil.copytree(SEED_EXT, BASE / "Extensions" / EXT) print("copied_ext") mf_path = BASE / "Extensions" / EXT / "5.5.0_0" / "manifest.json" if mf_path.exists(): md = load(mf_path) if "update_url" in md: md.pop("update_url") mf_path.write_text(json.dumps(md, ensure_ascii=False, indent=3) + "\n", encoding="utf-8") print("stripped_seed_manifest_update_url") seed_entry = ((load(SEED_PREF).get("extensions") or {}).get("settings") or {}).get(EXT) if not isinstance(seed_entry, dict): raise SystemExit("NO_SEED_PREF") entry = dict(seed_entry) entry["state"] = 1 entry["path"] = f"{EXT}/5.5.0_0" entry["location"] = 4 entry["from_webstore"] = False entry.pop("disable_reasons", None) mf = entry.get("manifest") if isinstance(mf, dict): mf = dict(mf) mf.pop("update_url", None) entry["manifest"] = mf # NEVER rewrite Secure Preferences — Chrome MAC; overwriting logs out Google. pref = BASE / "Preferences" d = load(pref) if pref.exists() else {} settings = d.setdefault("extensions", {}).setdefault("settings", {}) settings[EXT] = dict(entry) d.setdefault("extensions", {}).setdefault("ui", {})["developer_mode"] = True pref.write_text(json.dumps(d, separators=(",", ":")), encoding="utf-8") print("patched Preferences", settings[EXT].get("location"), settings[EXT].get("from_webstore")) pol = load(Path("/etc/opt/chrome/policies/managed/policies.json")) print("forcelist", pol.get("ExtensionInstallForcelist")) ''' PROBE = r''' import json, urllib.request from pathlib import Path ext="dhdgffkkebhmkfjojejmpbldmpobfkfo" p=Path("/home/neko/.config/google-chrome/Default/Preferences") d=json.loads(p.read_text()) e=((d.get("extensions") or {}).get("settings") or {}).get(ext) or {} print("state", e.get("state"), "loc", e.get("location"), "webstore", e.get("from_webstore")) print("path", e.get("path"), "disable", e.get("disable_reasons")) mf=e.get("manifest") or {} print("update_url", mf.get("update_url")) pol=json.loads(Path("/etc/opt/chrome/policies/managed/policies.json").read_text()) print("forcelist", pol.get("ExtensionInstallForcelist")) try: tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read()) except Exception as ex: print("cdp_err", type(ex).__name__, ex) tabs=[] sw=[t for t in tabs if t.get("type")=="service_worker" and "dhdgff" in (t.get("url") or "")] print("tm_sw", len(sw)) for t in sw: print(" ", t.get("url")) # open extensions page briefly via CDP new tab? just list chrome-extension pages pages=[t for t in tabs if "chrome-extension://dhdgff" in (t.get("url") or "")] print("tm_pages", len(pages)) for t in pages[:6]: print(" ", t.get("title"), (t.get("url") or "")[:100]) ''' def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for remote, local in FILES.items(): tmp = f"/tmp/{Path(remote).name}.tmfix" with sftp.file(tmp, "wb") as f: f.write(local.read_bytes()) print("uploaded", remote) # also write probe/inside scripts with sftp.file("/tmp/_tm_offline_inside.py", "wb") as f: f.write(INSIDE.encode()) with sftp.file("/tmp/_tm_offline_probe.py", "wb") as f: f.write(PROBE.encode()) remote_sh = r""" set -e install -m 644 /tmp/policies.json.tmfix /opt/douyinWEB/backend/neko/policies/policies.json install -m 755 /tmp/browser-launch.sh.tmfix /opt/douyinWEB/backend/neko/browser-launch.sh install -m 644 /tmp/Preferences.tmfix /opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Preferences if [ -d /etc/neko/tampermonkey-seed/Default ]; then install -m 644 /tmp/Preferences.tmfix /etc/neko/tampermonkey-seed/Default/Preferences || true fi for i in 1 2; do name=douyinweb-ops-neko-$i echo "===== FIX $name =====" docker exec $name supervisorctl stop google-chrome || true sleep 2 docker exec -i $name tee /tmp/_tm_offline_inside.py < /tmp/_tm_offline_inside.py >/dev/null docker exec $name python3 /tmp/_tm_offline_inside.py docker exec $name bash -lc 'chown -R neko:neko /home/neko/.config/google-chrome/Default/Extensions /home/neko/.config/google-chrome/Default/Preferences "/home/neko/.config/google-chrome/Default/Secure Preferences" 2>/dev/null || true' docker exec $name supervisorctl start google-chrome done sleep 20 for i in 1 2; do name=douyinweb-ops-neko-$i echo "===== PROBE $name =====" docker exec -i $name tee /tmp/_tm_offline_probe.py < /tmp/_tm_offline_probe.py >/dev/null docker exec $name python3 /tmp/_tm_offline_probe.py docker exec $name supervisorctl status google-chrome done """ with sftp.file("/tmp/_tm_offline_remote.sh", "wb") as f: f.write(remote_sh.encode()) sftp.close() _i, o, e = c.exec_command( "echo 4669805 | sudo -S -p '' bash /tmp/_tm_offline_remote.sh", timeout=360, ) print((o.read() + e.read()).decode("utf-8", "replace")) c.close() if __name__ == "__main__": main()