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

133 lines
5.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Force-recreate assign Nekos so bind mounts pick up new browser-launch/seed/policies inodes."""
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
# Push latest assets again with in-place cat (same inode if possible), then recreate.
PUSH = r'''
set -e
ROOT=/opt/douyinWEB/backend/neko
# Prefer in-place overwrite if files exist
for pair in \
"browser-launch.sh.tmfix:browser-launch.sh" \
"policies.json.tmfix:policies/policies.json" \
"Preferences.tmfix:tampermonkey-seed/Default/Preferences" \
"manifest.json.tmfix:tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json"
do
src="/tmp/${pair%%:*}"
dst="$ROOT/${pair##*:}"
if [ -f "$dst" ]; then
cat "$src" > "$dst"
else
mkdir -p "$(dirname "$dst")"
cp -f "$src" "$dst"
fi
done
chmod 755 "$ROOT/browser-launch.sh"
# Ensure seed tree exists on host
ls -la "$ROOT/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json"
ls -la "$ROOT/policies/policies.json"
md5sum "$ROOT/browser-launch.sh" | awk '{print "host_launch",$1}'
grep -c Offline "$ROOT/browser-launch.sh" | awk '{print "host_offline_hits",$1}'
COMPOSE=/opt/douyinWEB/backend/data/neko/docker-compose.yml
# Remember which were running
RUNNING=$(docker ps --format '{{.Names}}' | grep -E '^douyinweb-neko-[0-9]+$' | sort || true)
echo "was_running: $RUNNING"
cd /opt/douyinWEB/backend/data/neko
docker compose -f "$COMPOSE" up -d --force-recreate --no-start
echo "recreated_ok"
# Start previously running
for name in $RUNNING; do
echo "start $name"
docker start "$name" || true
done
# If none were running (e.g. VPS), leave stopped — operator starts manually
sleep 18
for name in $RUNNING; do
echo "===== VERIFY $name ====="
docker exec "$name" bash -lc '
echo offline_hits=$(grep -c Offline /etc/neko/browser-launch.sh || true)
echo seed=$(ls /etc/neko/tampermonkey-seed/Default/Extensions 2>/dev/null | head -1 || echo MISSING)
echo policy=$(test -f /etc/opt/chrome/policies/managed/policies.json && echo OK || echo MISSING)
md5sum /etc/neko/browser-launch.sh | awk "{print \"ctr_launch\",\$1}"
'
docker exec "$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={}
if pref.exists():
e=((json.loads(pref.read_text()).get("extensions") or {}).get("settings") or {}).get(ext) or {}
print("tm", e.get("state"), e.get("location"), e.get("from_webstore"))
print("ext", Path(f"/home/neko/.config/google-chrome/Default/Extensions/{ext}/5.5.0_0/manifest.json").exists())
sp=Path("/home/neko/.config/google-chrome/Default/Secure Preferences")
print("secure_bytes", sp.stat().st_size if sp.exists() else -1)
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()
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_ext", m.group(1) if m else None); found=True; break
if not found: print("load_ext NONE")
try:
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read())
except Exception as ex:
print("cdp", type(ex).__name__); tabs=[]
print("tm_sw", sum(1 for t in tabs if t.get("type")=="service_worker" and "dhdgff" in (t.get("url") or "")))
print("tabs", len(tabs))
PY
done
'''
def do_host(label: str, host: str, port: int, user: str, pw: str, use_sudo: bool) -> None:
print(f"\n########## {label} ##########", flush=True)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
files = {
"browser-launch.sh.tmfix": ROOT / "backend/neko/browser-launch.sh",
"policies.json.tmfix": ROOT / "backend/neko/policies/policies.json",
"Preferences.tmfix": ROOT / "backend/neko/tampermonkey-seed/Default/Preferences",
"manifest.json.tmfix": ROOT
/ "backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json",
}
for name, path in files.items():
with sftp.file(f"/tmp/{name}", "wb") as f:
f.write(path.read_bytes())
print("up", name, flush=True)
with sftp.file("/tmp/_recreate_assign_neko.sh", "wb") as f:
f.write(PUSH.encode())
sftp.close()
cmd = (
f"echo {pw} | sudo -S -p '' bash /tmp/_recreate_assign_neko.sh"
if use_sudo
else "bash /tmp/_recreate_assign_neko.sh"
)
_, o, e = c.exec_command(cmd, timeout=900)
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
c.close()
def main() -> None:
do_host("PHYSICAL", "107.173.30.245", 4014, "eric", "4669805", True)
do_host("VPS", "107.173.30.245", 22, "root", "Xiao4669805", False)
if __name__ == "__main__":
main()