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

167 lines
6.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Continue: verify ops-neko chrome + edges; harden API env; deploy code both hosts."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
TENCENT = ("82.157.112.245", 22, "ubuntu", "Xiao4669805")
LOCAL = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/youtube_neko.py",
"backend/app/ops_neko.py",
"backend/app/ops_neko_db.py",
]
def ssh(spec):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(spec[0], spec[1], spec[2], spec[3], timeout=45, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, t=120):
_i, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
def put_files(c, dest_root: str, *, sudo_user_pass: str | None = None):
sftp = c.open_sftp()
for rel in FILES:
data = (LOCAL / rel).read_bytes()
tmp = f"/tmp/_cont_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
dst = f"{dest_root}/{rel}"
if sudo_user_pass:
print(
run(
c,
f"echo '{sudo_user_pass}' | sudo -S -p '' bash -lc "
f"\"install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}\"",
),
end="",
)
else:
print(run(c, f"install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}"), end="")
print(f" -> {dst}", flush=True)
sftp.close()
def main():
# --- physical verify + harden ---
p = ssh(PHYS)
print("=== PHYS status ===", flush=True)
print(
run(
p,
"echo '4669805' | sudo -S -p '' bash -lc '"
"docker exec douyinweb-ops-neko-1 supervisorctl status 2>/dev/null | head -10; "
"docker exec douyinweb-ops-neko-1 env | grep -E \"NAT1TO1|ENV_USER|BROWSER_PROXY\" | sort; "
"curl -sS -m 5 -o /dev/null -w \"ops_http=%{http_code}\\n\" http://127.0.0.1:18200/; "
"grep -E \"DY_NEKO_CN_NAT1TO1|DY_NEKO_VPS_NAT1TO1|DY_NEKO_NAT1TO1|DY_NEKO_HTTP_BIND\" "
" /etc/systemd/system/douyinweb-api.service | head -20; "
"'",
),
flush=True,
)
print("=== PHYS deploy modules + soft restart API (keep containers) ===", flush=True)
put_files(p, "/opt/douyinWEB", sudo_user_pass="4669805")
print(
run(
p,
"echo '4669805' | sudo -S -p '' bash -lc '"
"SVC=/etc/systemd/system/douyinweb-api.service; "
"grep -q DY_NEKO_CN_NAT1TO1= \"$SVC\" || "
" sed -i \"/DY_NEKO_NAT1TO1=/a Environment=DY_NEKO_CN_NAT1TO1=82.157.112.245\" \"$SVC\"; "
"grep -q DY_NEKO_VPS_NAT1TO1= \"$SVC\" || "
" sed -i \"/DY_NEKO_NAT1TO1=/a Environment=DY_NEKO_VPS_NAT1TO1=107.173.30.245\" \"$SVC\"; "
"grep -q DY_NEKO_HTTP_BIND= \"$SVC\" || "
" sed -i \"/\\[Service\\]/a Environment=DY_NEKO_HTTP_BIND=0.0.0.0\" \"$SVC\"; "
"sed -i \"s|Environment=DY_NEKO_NAT1TO1=.*|Environment=DY_NEKO_NAT1TO1=82.157.112.245|\" \"$SVC\"; "
"systemctl daemon-reload; "
"systemctl restart douyinweb-api; "
"sleep 2; "
"systemctl is-active douyinweb-api; "
# ensure compose on disk still correct after code push
"export DY_NEKO_CN_NAT1TO1=82.157.112.245 DY_NEKO_VPS_NAT1TO1=107.173.30.245 "
" DY_NEKO_NAT1TO1=82.157.112.245 DY_NEKO_HTTP_BIND=0.0.0.0 DY_NEKO_EDGE_UPSTREAM=10.8.0.2; "
"cd /opt/douyinWEB/backend; "
"/opt/douyinWEB/backend/.venv/bin/python - <<\"PY\"\n"
"import os,sys\n"
"os.environ.setdefault(\"DY_NEKO_CN_NAT1TO1\",\"82.157.112.245\")\n"
"os.environ.setdefault(\"DY_NEKO_NAT1TO1\",\"82.157.112.245\")\n"
"os.environ.setdefault(\"DY_NEKO_HTTP_BIND\",\"0.0.0.0\")\n"
"os.environ.setdefault(\"DY_NEKO_EDGE_UPSTREAM\",\"10.8.0.2\")\n"
"sys.path.insert(0,\".\")\n"
"from app import ops_neko, ops_neko_db\n"
"ms=max(ops_neko_db.max_slot(),1)\n"
"t=ops_neko._render_compose(ms)\n"
"ops_neko._compose_path().write_text(t,encoding=\"utf-8\")\n"
"print(\"compose_ok\", \"ENV_USER\" in t, \"82.157\" in t, \"0.0.0.0:18200\" in t)\n"
"PY\n"
"docker exec douyinweb-ops-neko-1 supervisorctl status google-chrome || true; "
"'",
180,
),
flush=True,
)
p.close()
# --- VPS (dylive) deploy same modules (no recreate ops unless present) ---
v = ssh(VPS)
print("=== VPS deploy modules ===", flush=True)
put_files(v, "/opt/douyinWEB")
print(
run(
v,
"systemctl restart douyinweb-api 2>/dev/null || "
"systemctl restart dylive-api 2>/dev/null || true; "
"sleep 2; "
"systemctl is-active douyinweb-api 2>/dev/null; "
"systemctl is-active dylive-api 2>/dev/null; "
"curl -sS -m 12 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health; "
"curl -sS -m 12 -o /dev/null -w 'dy_com=%{http_code}\\n' https://dy.nomadro.com/api/health; "
"curl -sS -m 12 -o /dev/null -w 'ops_com=%{http_code}\\n' https://dy.nomadro.com/ops-neko/1/; "
"systemctl is-active dy-frp-http-aux sing-box frpp 'wg-quick@wg-dy' | paste -sd' ' -",
90,
),
flush=True,
)
v.close()
# --- Tencent quick check ---
try:
t = ssh(TENCENT)
print("=== TENCENT ===", flush=True)
print(
run(
t,
"curl -sS -m 12 -o /dev/null -w 'cn_ops=%{http_code}\\n' https://dy.nomadro.cn/ops-neko/1/; "
"sudo iptables -t nat -S | grep OPS-NEKO | head -5; "
"nc -vz -w 2 10.9.0.2 18200 2>&1 | tail -1; "
"nc -vz -w 2 10.9.0.2 62300 2>&1 | tail -1",
40,
),
flush=True,
)
t.close()
except Exception as exc:
print("tencent skip", exc, flush=True)
print("DONE continue", flush=True)
if __name__ == "__main__":
main()