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

140 lines
5.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""In-place overwrite bind-mounted browser-launch.sh so containers see TM --load-extension."""
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
REMOTE_SH = r"""
set -e
BL=/opt/douyinWEB/backend/neko/browser-launch.sh
# in-place overwrite (same inode) so docker bind mounts refresh
cat /tmp/browser-launch.sh.tmfix > "$BL"
chmod 755 "$BL"
# policies + seed prefs + manifest in-place too
cat /tmp/policies.json.tmfix > /opt/douyinWEB/backend/neko/policies/policies.json
chmod 644 /opt/douyinWEB/backend/neko/policies/policies.json
cat /tmp/Preferences.tmfix > /opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Preferences
chmod 644 /opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Preferences
MF=/opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json
if [ -f "$MF" ]; then
cat /tmp/tm_manifest.json.tmfix > "$MF"
chmod 644 "$MF"
fi
echo "--- host inode check ---"
grep -n "Offline Tampermonkey\|tm_load_dir" "$BL" | head -5
docker exec douyinweb-ops-neko-1 grep -n "Offline Tampermonkey\|tm_load_dir" /etc/neko/browser-launch.sh | head -5 || true
for i in 1 2; do
name=douyinweb-ops-neko-$i
echo "===== RESTART $name ====="
docker exec -i "$name" python3 - <<'PY'
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/"Managed Extension Settings"/EXT):
if p.exists():
shutil.rmtree(p, ignore_errors=True)
(BASE/"Extensions").mkdir(parents=True, exist_ok=True)
shutil.copytree(SEED_EXT, BASE/"Extensions"/EXT)
mf=BASE/"Extensions"/EXT/"5.5.0_0"/"manifest.json"
if mf.exists():
d=load(mf); d.pop("update_url", None)
mf.write_text(json.dumps(d, ensure_ascii=False, indent=3)+"\n", encoding="utf-8")
seed=((load(SEED_PREF).get("extensions") or {}).get("settings") or {}).get(EXT) or {}
entry=dict(seed)
entry.update({"state":1,"path":f"{EXT}/5.5.0_0","location":4,"from_webstore":False})
entry.pop("disable_reasons", None)
mf2=entry.get("manifest")
if isinstance(mf2, dict):
mf2=dict(mf2); mf2.pop("update_url", None); entry["manifest"]=mf2
# NEVER rewrite Secure Preferences — nukes Google login.
pref=BASE/"Preferences"
d=load(pref) if pref.exists() else {}
d.setdefault("extensions",{}).setdefault("settings",{})[EXT]=dict(entry)
d.setdefault("extensions",{}).setdefault("ui",{})["developer_mode"]=True
pref.write_text(json.dumps(d, separators=(",",":")), encoding="utf-8")
print("profile_ready")
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 restart google-chrome
done
sleep 28
for i in 1 2; do
name=douyinweb-ops-neko-$i
echo "===== PROBE $name ====="
docker exec -i "$name" python3 - <<'PY'
import json, os, re, urllib.request
from pathlib import Path
ext="dhdgffkkebhmkfjojejmpbldmpobfkfo"
e=((json.loads(Path("/home/neko/.config/google-chrome/Default/Preferences").read_text()).get("extensions") or {}).get("settings") or {}).get(ext) or {}
print("state", e.get("state"), "loc", e.get("location"), "webstore", e.get("from_webstore"), "disable", e.get("disable_reasons"))
pol=json.loads(Path("/etc/opt/chrome/policies/managed/policies.json").read_text())
print("forcelist", pol.get("ExtensionInstallForcelist"))
found=False
for pid in os.listdir("/proc"):
if not pid.isdigit(): continue
try:
cmd=open(f"/proc/{pid}/cmdline","rb").read().replace(b"\x00", b" ").decode("utf-8","replace")
except Exception:
continue
if "google-chrome" in cmd and "--type=" not in cmd and "load-extension" in cmd:
m=re.search(r"--load-extension=([^ ]+)", cmd)
print("load_extension", m.group(1) if m else None)
found=True
break
if not found:
print("load_extension NOT_FOUND")
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read())
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"))
print("tm_pages", sum(1 for t in tabs if "chrome-extension://dhdgff" in (t.get("url") or "")))
# also check live-ops loaded
lo=[t for t in tabs if t.get("type")=="service_worker" and "live-ops" in (t.get("url") or "")]
print("live_ops_sw", len(lo))
PY
done
"""
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()
uploads = {
"/tmp/browser-launch.sh.tmfix": ROOT / "backend/neko/browser-launch.sh",
"/tmp/policies.json.tmfix": ROOT / "backend/neko/policies/policies.json",
"/tmp/Preferences.tmfix": ROOT / "backend/neko/tampermonkey-seed/Default/Preferences",
"/tmp/tm_manifest.json.tmfix": ROOT
/ "backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json",
}
for remote, local in uploads.items():
with sftp.file(remote, "wb") as f:
f.write(local.read_bytes())
print("uploaded", local.name)
with sftp.file("/tmp/_tm_inplace.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_inplace.sh", timeout=360)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
if __name__ == "__main__":
main()