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

85 lines
2.8 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Scan VPS assign Nekos + sync browser-launch stop trap if needed."""
from __future__ import annotations
import hashlib
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
LOCAL = ROOT / "backend/neko/browser-launch.sh"
SCAN = r"""#!/bin/bash
set +e
echo "=== vps browser-launch trap ==="
grep -n on_supervisor_stop /opt/douyinWEB/backend/neko/browser-launch.sh 2>/dev/null | head || echo NO_TRAP
echo "=== assign containers ==="
docker ps --format '{{.Names}}' | grep -E 'neko|douyin' | sort
for N in $(docker ps --format '{{.Names}}' | grep -E 'douyinweb-neko-[0-9]+$' | sort); do
out=$(docker exec -i "$N" python3 - <<'PY' 2>/dev/null
import os, json, urllib.request
mains=0; load=False; sw=-1
for pid in os.listdir("/proc"):
if not pid.isdigit(): continue
try: raw=open(f"/proc/{pid}/cmdline","rb").read()
except: continue
if not raw: continue
if b"google-chrome" in raw and b"--type=" not in raw and b"crashpad" not in raw and b"browser-launch" not in raw:
mains += 1
if b"load-extension" in raw and b"dhdgff" in raw: load=True
try:
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=2).read())
sw=sum(1 for t in tabs if t.get("type")=="service_worker" and "dhdgff" in (t.get("url") or ""))
except Exception:
sw=-1
print(f"mains={mains} load_ext={load} tm_sw={sw}")
PY
)
echo "$N $out"
done
"""
def main() -> None:
data = LOCAL.read_bytes()
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=30,
allow_agent=False,
look_for_keys=False,
banner_timeout=60,
)
sftp = c.open_sftp()
with sftp.file("/tmp/_scan_vps_neko.sh", "w") as f:
f.write(SCAN)
with sftp.file("/tmp/browser-launch.sh.stoptrap", "w") as f:
f.write(data)
sftp.close()
_i, o, e = c.exec_command("bash /tmp/_scan_vps_neko.sh", timeout=120)
print((o.read() + e.read()).decode("utf-8", "replace"))
# sync trap if missing
_i, o, e = c.exec_command(
"grep -q on_supervisor_stop /opt/douyinWEB/backend/neko/browser-launch.sh || "
"{ cp -a /opt/douyinWEB/backend/neko/browser-launch.sh "
"/opt/douyinWEB/backend/neko/browser-launch.sh.bak.stoptrap_20260905 && "
"install -m 755 /tmp/browser-launch.sh.stoptrap /opt/douyinWEB/backend/neko/browser-launch.sh && "
"echo SYNCED_TRAP; }; grep -n on_supervisor_stop /opt/douyinWEB/backend/neko/browser-launch.sh | head",
timeout=30,
)
print((o.read() + e.read()).decode("utf-8", "replace"))
print("local_sha", hashlib.sha256(data).hexdigest()[:12])
c.close()
if __name__ == "__main__":
main()