Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
278 lines
10 KiB
Python
278 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Polish running physical deploy (dy.nomadro.com guest) — idempotent.
|
|
|
|
- Fix wg-quick@wg-dy 'already exists' failed unit (idempotent drop-in)
|
|
- Harden API/Web TimeoutStopSec + ensure DY_NEKO_* / DY_PUBLIC_BASE_URL
|
|
- Deploy latest network/neko hubs
|
|
- Sync Caddy /neko routes if missing
|
|
- Verify matrix without touching dylive / sing-box
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
VPS = "107.173.30.245"
|
|
PVE_WG = "10.8.0.2"
|
|
GUEST_PW = "4669805"
|
|
VPS_PW = "Xiao4669805"
|
|
DOMAIN = "dy.nomadro.com"
|
|
|
|
FILES = [
|
|
"backend/app/youtube_neko.py",
|
|
"backend/app/network_hub.py",
|
|
"backend/app/wireguard_hub.py",
|
|
"backend/app/shellcrash_hub.py",
|
|
"backend/app/global_socks.py",
|
|
]
|
|
|
|
|
|
def ssh(port, user, password, tries=8):
|
|
last = None
|
|
for i in range(tries):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
VPS,
|
|
port,
|
|
user,
|
|
password,
|
|
timeout=45,
|
|
banner_timeout=90,
|
|
auth_timeout=60,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print(f"ssh {port} fail{i} {type(ex).__name__}", flush=True)
|
|
time.sleep(2 + i)
|
|
raise last # type: ignore[misc]
|
|
|
|
|
|
def run(c, cmd, timeout=120):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def must_dylive(v, label):
|
|
code = run(v, "curl -sS -m 8 -o /dev/null -w '%{http_code}' https://dylive.nomadro.com/api/health", 15).strip()
|
|
print(f"[dylive {label}] {code}", flush=True)
|
|
if code != "200":
|
|
raise SystemExit(f"ABORT dylive {code}")
|
|
|
|
|
|
def main() -> int:
|
|
v = ssh(22, "root", VPS_PW)
|
|
must_dylive(v, "start")
|
|
|
|
g = ssh(4014, "eric", GUEST_PW)
|
|
sftp = g.open_sftp()
|
|
for rel in FILES:
|
|
sftp.put(str(ROOT / rel), f"/tmp/_up_{Path(rel).name}")
|
|
print("put", rel, flush=True)
|
|
with sftp.file("/tmp/_wg_override.conf", "w") as f:
|
|
f.write(
|
|
"[Service]\n"
|
|
"ExecStart=\n"
|
|
"ExecStart=/usr/bin/bash -c 'if ip link show wg-dy >/dev/null 2>&1; then exit 0; else exec /usr/bin/wg-quick up wg-dy; fi'\n"
|
|
"ExecStop=\n"
|
|
"ExecStop=/usr/bin/bash -c 'if ip link show wg-dy >/dev/null 2>&1; then exec /usr/bin/wg-quick down wg-dy; else exit 0; fi'\n"
|
|
)
|
|
sftp.close()
|
|
|
|
print("=== guest polish ===", flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
|
|
"set -e; "
|
|
"install -m 644 /tmp/_up_youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py; "
|
|
"install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; "
|
|
"install -m 644 /tmp/_up_wireguard_hub.py /opt/douyinWEB/backend/app/wireguard_hub.py; "
|
|
"install -m 644 /tmp/_up_shellcrash_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py; "
|
|
"install -m 644 /tmp/_up_global_socks.py /opt/douyinWEB/backend/app/global_socks.py; "
|
|
# WG idempotent unit
|
|
"mkdir -p /etc/systemd/system/wg-quick@wg-dy.service.d; "
|
|
"install -m 644 /tmp/_wg_override.conf /etc/systemd/system/wg-quick@wg-dy.service.d/override.conf; "
|
|
# API unit harden — append missing envs / stop timeout
|
|
"API=/etc/systemd/system/douyinweb-api.service; "
|
|
"grep -q DY_NEKO_HTTP_BIND $API || sed -i \"/DY_SESSION_SECRET=/a Environment=DY_NEKO_HTTP_BIND=0.0.0.0\" $API; "
|
|
"grep -q DY_NEKO_EDGE_UPSTREAM $API || sed -i \"/DY_NEKO_HTTP_BIND=/a Environment=DY_NEKO_EDGE_UPSTREAM=10.8.0.2\" $API; "
|
|
"grep -q DY_NEKO_NAT1TO1 $API || sed -i \"/DY_NEKO_EDGE_UPSTREAM=/a Environment=DY_NEKO_NAT1TO1=107.173.30.245\" $API; "
|
|
"grep -q DY_PUBLIC_BASE_URL $API || sed -i \"/Environment=DY_SESSION_SECRET=/a Environment=DY_PUBLIC_BASE_URL=https://dy.nomadro.com\" $API; "
|
|
"grep -q TimeoutStopSec $API || sed -i \"/Type=simple/a TimeoutStopSec=25\\nKillMode=mixed\" $API; "
|
|
"WEB=/etc/systemd/system/douyinweb-web.service; "
|
|
"grep -q TimeoutStopSec $WEB || sed -i \"/Type=simple/a TimeoutStopSec=20\\nKillMode=mixed\" $WEB; "
|
|
"systemctl daemon-reload; "
|
|
"systemctl reset-failed wg-quick@wg-dy || true; "
|
|
"systemctl start wg-quick@wg-dy || true; "
|
|
"systemctl is-active wg-quick@wg-dy; "
|
|
"ip -br a show wg-dy; "
|
|
# soft restart API (avoid hang-wait on open keepalives)
|
|
"systemctl restart douyinweb-api; "
|
|
"sleep 4; systemctl is-active douyinweb-api douyinweb-web docker; "
|
|
"cd /opt/douyinWEB/backend && .venv/bin/python -m py_compile app/youtube_neko.py app/network_hub.py app/wireguard_hub.py app/shellcrash_hub.py && echo COMPILE_OK"
|
|
"'",
|
|
90,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
for i in range(12):
|
|
time.sleep(2)
|
|
code = run(g, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x")
|
|
print("health", code.strip(), flush=True)
|
|
if "200" in code:
|
|
break
|
|
else:
|
|
raise SystemExit("api unhealthy")
|
|
|
|
# Ensure caddy neko routes present on VPS
|
|
caddy = run(v, "grep -c 'handle_path /neko/' /etc/caddy/sites/dy.nomadro.com.caddy || echo 0").strip()
|
|
print("caddy_neko_handles", caddy, flush=True)
|
|
if caddy in {"0", ""}:
|
|
print("=== rewrite caddy neko blocks ===", flush=True)
|
|
blocks = []
|
|
for i in range(1, 5):
|
|
port = 9200 + i - 1
|
|
blocks.append(
|
|
f"""
|
|
handle /neko/{i} {{
|
|
redir /neko/{i}/ 308
|
|
}}
|
|
handle_path /neko/{i}/* {{
|
|
reverse_proxy {PVE_WG}:{port} {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}"""
|
|
)
|
|
site = f"""# Physical app — {DOMAIN} → WG {PVE_WG}
|
|
# Do not edit dylive.nomadro.com.caddy
|
|
|
|
{DOMAIN} {{
|
|
encode gzip
|
|
request_body {{
|
|
max_size 200MB
|
|
}}
|
|
|
|
handle /api/* {{
|
|
reverse_proxy {PVE_WG}:8014 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle /covers/* {{
|
|
reverse_proxy {PVE_WG}:8014 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
{''.join(blocks)}
|
|
handle /_/* {{
|
|
reverse_proxy {PVE_WG}:8092 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle /douyin-covers/* {{
|
|
reverse_proxy {PVE_WG}:8333 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle /shellcrash-ui {{
|
|
redir /shellcrash-ui/ 308
|
|
}}
|
|
handle_path /shellcrash-ui/* {{
|
|
reverse_proxy {PVE_WG}:9999 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
@ui_noslash {{
|
|
path /ui
|
|
not path /ui/
|
|
not path /ui/*
|
|
}}
|
|
redir @ui_noslash /ui/ 308
|
|
@clash path /ui/ /ui/* /version /proxies /proxies/* /configs /configs/* /rules /rules/* /connections /connections/* /providers /providers/* /cache /cache/* /dns /dns/* /debug /debug/* /traffic /logs /memory /restart /upgrade /upgrade/* /group /group/* /script /script/* /fakeip /fakeip/* /endpoint /endpoint/* /runtime /runtime/* /meta /meta/* /schema /schema/* /flushfakeip /externaloverride /externaloverride/* /storage /storage/*
|
|
handle @clash {{
|
|
reverse_proxy {PVE_WG}:9999 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle {{
|
|
reverse_proxy {PVE_WG}:4015 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
}}
|
|
"""
|
|
must_dylive(v, "before_caddy")
|
|
sftp = v.open_sftp()
|
|
with sftp.file(f"/etc/caddy/sites/{DOMAIN}.caddy", "w") as f:
|
|
f.write(site)
|
|
sftp.close()
|
|
val = run(v, "caddy validate --config /etc/caddy/Caddyfile 2>&1", 30)
|
|
print(val[-400:], flush=True)
|
|
if "Valid configuration" not in val and "valid" not in val.lower():
|
|
raise SystemExit("caddy validate failed")
|
|
print(run(v, "systemctl reload caddy; sleep 2; systemctl is-active caddy"), flush=True)
|
|
must_dylive(v, "after_caddy")
|
|
|
|
print("=== public matrix ===", flush=True)
|
|
with httpx.Client(base_url=f"https://{DOMAIN}", timeout=25.0, follow_redirects=False) as client:
|
|
login = client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
|
|
print("login", login.status_code, flush=True)
|
|
net = client.get("/api/network")
|
|
print("network", net.status_code, flush=True)
|
|
if net.status_code == 200:
|
|
j = net.json()
|
|
cl = j.get("physical_checklist") or {}
|
|
print("checklist", cl.get("ok"), "failed", cl.get("failed"), flush=True)
|
|
for c in cl.get("checks") or []:
|
|
print(f" [{'OK' if c.get('ok') else '!!'}] {c.get('name')}: {c.get('detail')}", flush=True)
|
|
wg = j.get("wireguard") or {}
|
|
print("wg_active", wg.get("active"), "unit", wg.get("unit_wg_dy"), flush=True)
|
|
neko = client.get("/api/youtube/neko/status")
|
|
print("neko", neko.status_code, flush=True)
|
|
if neko.status_code == 200:
|
|
nj = neko.json()
|
|
print(
|
|
"neko desired",
|
|
nj.get("desired"),
|
|
"running",
|
|
nj.get("running"),
|
|
"bind",
|
|
nj.get("http_bind"),
|
|
"studio",
|
|
((nj.get("instances") or [{}])[0].get("studio_url") if nj.get("instances") else None),
|
|
flush=True,
|
|
)
|
|
print("page", client.get("/network").status_code, flush=True)
|
|
print("neko1", client.get("/neko/1/").status_code, flush=True)
|
|
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 8 -o /dev/null -w 'dylive:%{http_code}\\n' https://dylive.nomadro.com/api/health; "
|
|
f"curl -sS -m 5 -o /dev/null -w 'wg_api:%{{http_code}}\\n' http://{PVE_WG}:8014/api/health",
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
v.close()
|
|
print("DONE", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|