douyinWEB/scripts/_repair_opsneko_tm.py
2026-09-13 22:10:49 -05:00

133 lines
4.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Force-repair Tampermonkey userscript on ops-neko 1+2 (physical)."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = [
"backend/neko/provisioning/youtube-studio-auto-dismiss.user.js",
"backend/neko/provisioning/tampermonkey-provisioning.json",
"backend/neko/policies/policies.json",
"backend/neko/browser-launch.sh",
]
def main() -> None:
# Refresh provisioning locally first
import subprocess
subprocess.check_call(
[sys.executable, str(ROOT / "backend/neko/render_tampermonkey_provisioning.py"), "--write"],
cwd=str(ROOT),
)
# Sync policy hash from render output
out = subprocess.check_output(
[sys.executable, str(ROOT / "backend/neko/render_tampermonkey_provisioning.py")],
cwd=str(ROOT),
text=True,
)
print(out)
h = ""
for line in out.splitlines():
if line.startswith("hash="):
h = line.split("=", 1)[1].strip()
if h:
pol = ROOT / "backend/neko/policies/policies.json"
import json
data = json.loads(pol.read_text(encoding="utf-8"))
data["3rdparty"]["extensions"]["dhdgffkkebhmkfjojejmpbldmpobfkfo"]["jsonImport"][0]["hash"] = h
pol.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print("policies hash ->", h)
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 rel in FILES:
tmp = f"/tmp/_tmfix_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write((ROOT / rel).read_bytes())
dst = f"/opt/douyinWEB/{rel}"
cmd = (
f"echo '4669805' | sudo -S -p '' bash -lc "
f"\"install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}; chmod +x /opt/douyinWEB/backend/neko/browser-launch.sh\""
)
c.exec_command(cmd, timeout=30)[1].read()
print("ok", rel, flush=True)
sftp.close()
repair = r"""
echo 4669805 | sudo -S -p '' bash -lc '
set -e
EXT=dhdgffkkebhmkfjojejmpbldmpobfkfo
for i in 1 2; do
name=douyinweb-ops-neko-$i
echo "===== repair $name ====="
docker exec "$name" supervisorctl stop google-chrome || true
sleep 2
docker exec "$name" bash -lc "
P=/home/neko/.config/google-chrome/Default
rm -f \"\$P/Local Extension Settings/$EXT/LOCK\" 2>/dev/null || true
# Force clean re-import via managed jsonImport (LES often goes \"internal error\" after hard restart)
rm -rf \"\$P/Local Extension Settings/$EXT\" \"\$P/Managed Extension Settings/$EXT\"
mkdir -p \"\$P/Local Extension Settings\" \"\$P/Managed Extension Settings\"
chown -R neko:neko \"\$P/Local Extension Settings\" \"\$P/Managed Extension Settings\"
echo wiped_tm_storage
"
docker exec "$name" supervisorctl start google-chrome
done
echo waiting_import
sleep 18
for i in 1 2; do
name=douyinweb-ops-neko-$i
echo "===== verify $name ====="
docker exec "$name" bash -lc "
P=/home/neko/.config/google-chrome/Default
echo LES=\$(du -sh \"\$P/Local Extension Settings/$EXT\" 2>/dev/null | awk \"{print \\\$1}\")
echo MAN=\$(du -sh \"\$P/Managed Extension Settings/$EXT\" 2>/dev/null | awk \"{print \\\$1}\")
CNT=\$(grep -aR \"YouTube Studio Auto Dismiss\" \"\$P/Local Extension Settings/$EXT\" 2>/dev/null | wc -l)
echo SCRIPT_HITS=\$CNT
supervisorctl status google-chrome
"
docker exec "$name" python3 - <<'"'"'PY'"'"'
import json, urllib.request
try:
tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=4).read())
except Exception as e:
print("cdp", e)
raise SystemExit(0)
closed = 0
for t in tabs:
u = (t.get("url") or "").lower()
if "tampermonkey.net/changelog" in u or "tampermonkey.net/installed" in u:
tid = t.get("id")
if tid:
try:
urllib.request.urlopen(f"http://127.0.0.1:9222/json/close/{tid}", timeout=3)
closed += 1
except Exception:
pass
print("tabs", len(tabs), "closed_junk", closed)
for t in tabs[:12]:
print("-", (t.get("title") or "")[:40], "|", (t.get("url") or "")[:90])
PY
done
'
"""
_i, o, e = c.exec_command(repair, timeout=180)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
print("DONE", flush=True)
if __name__ == "__main__":
main()