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

108 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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
install -m 755 /tmp/browser-launch.sh.tmfix /opt/douyinWEB/backend/neko/browser-launch.sh
for p in \
/opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json \
/etc/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json
do
if [ -f "$p" ]; then
install -m 644 /tmp/tm_manifest.json.tmfix "$p" || true
echo updated "$p"
fi
done
# push browser-launch into each container if the path is writable
for i in 1 2; do
name=douyinweb-ops-neko-$i
echo "===== RESTART $name ====="
docker exec -i "$name" tee /tmp/browser-launch.sh.new < /opt/douyinWEB/backend/neko/browser-launch.sh >/dev/null
docker exec "$name" bash -lc '
for t in /etc/neko/browser-launch.sh /usr/local/bin/browser-launch.sh /opt/neko/browser-launch.sh; do
if [ -f "$t" ] && [ -w "$t" ]; then
cp /tmp/browser-launch.sh.new "$t"
chmod +x "$t"
echo copied_to "$t"
fi
done
# also find how chrome is started
supervisorctl status google-chrome || true
grep -R "browser-launch" /etc/supervisor /etc/neko 2>/dev/null | head -20 || true
'
docker exec "$name" supervisorctl restart google-chrome || docker exec "$name" supervisorctl start 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"
pref = Path("/home/neko/.config/google-chrome/Default/Preferences")
e = ((json.loads(pref.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"))
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")
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", 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"))
print("tm_pages", len([t for t in tabs if "chrome-extension://dhdgff" in (t.get("url") or "")]))
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()
with sftp.file("/tmp/browser-launch.sh.tmfix", "wb") as f:
f.write((ROOT / "backend/neko/browser-launch.sh").read_bytes())
with sftp.file("/tmp/tm_manifest.json.tmfix", "wb") as f:
f.write(
(
ROOT
/ "backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json"
).read_bytes()
)
with sftp.file("/tmp/_tm_loadext.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_loadext.sh", timeout=360)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
if __name__ == "__main__":
main()