douyinWEB/scripts/_continue_wg_harden.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
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>
2026-09-04 22:42:40 -05:00

136 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""Continue WG harden: verify dual watch, ensure wg-quick@wg-dy-cn unit, health check."""
from __future__ import annotations
import sys
import time
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
PASS = "4669805"
def ssh(host, port, user, pw):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect(
host,
port,
user,
pw,
timeout=45,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
return c
except Exception as ex:
print("ssh", i, type(ex).__name__, flush=True)
time.sleep(2)
raise SystemExit("ssh failed")
def run(c, cmd, timeout=90):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def pub(url: str) -> str:
try:
with urllib.request.urlopen(url, timeout=15) as r:
return str(r.status)
except Exception as ex:
return f"ERR {getattr(ex, 'code', type(ex).__name__)}"
def main() -> int:
print(
"public",
"com",
pub("https://dy.nomadro.com/api/health"),
"cn",
pub("https://dy.nomadro.cn/api/health"),
"dylive",
pub("https://dylive.nomadro.com/api/health"),
flush=True,
)
g = ssh("107.173.30.245", 4014, "eric", PASS)
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
# Ensure wg-quick@wg-dy-cn exists (idempotent like wg-dy)
sh = f"""echo '{PASS}' | sudo -S -p '' bash -lc '
set -e
# Idempotent drop-ins for both ifaces
for iface in wg-dy wg-dy-cn; do
mkdir -p /etc/systemd/system/wg-quick@${{iface}}.service.d
cat >/etc/systemd/system/wg-quick@${{iface}}.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/bash -c "if ip link show ${{iface}} >/dev/null 2>&1; then exit 0; else exec /usr/bin/wg-quick up ${{iface}}; fi"
ExecStop=
ExecStop=/usr/bin/bash -c "if ip link show ${{iface}} >/dev/null 2>&1; then exec /usr/bin/wg-quick down ${{iface}}; else exit 0; fi"
EOF
systemctl enable wg-quick@${{iface}}.service >/dev/null 2>&1 || true
systemctl reset-failed wg-quick@${{iface}}.service 2>/dev/null || true
systemctl start wg-quick@${{iface}}.service || true
done
systemctl daemon-reload
# Confirm watch still has BOTH ifaces quoted
systemctl show douyinweb-wireguard-watch -p Environment --no-pager
systemctl is-active douyinweb-wireguard-watch wg-quick@wg-dy wg-quick@wg-dy-cn
# Handshakes + ping
echo "=== handshakes ==="
wg show wg-dy | sed -n "1,12p"
echo "---"
wg show wg-dy-cn | sed -n "1,12p"
echo "=== ping ==="
ping -c 2 -W 2 10.8.0.1 | tail -3
ping -c 2 -W 2 10.9.0.1 | tail -3
# Watch journal — must mention both ifaces in startup line
journalctl -u douyinweb-wireguard-watch -n 12 --no-pager | tail -12
# Process environ
PID=$(systemctl show -p MainPID --value douyinweb-wireguard-watch)
echo "PID=$PID"
tr "\\0" "\\n" < /proc/$PID/environ | grep WIREGUARD || true
echo CONTINUE_OK
'"""
print(run(g, sh, timeout=90), flush=True)
print(
"VPS",
run(
v,
"wg show wg-dy | head -12; echo ---; "
"ping -c 2 -W 2 10.8.0.2 | tail -2; "
"curl -sS -m 6 -o /dev/null -w 'api:%{http_code}\\n' http://10.8.0.2:8014/api/health",
),
flush=True,
)
print(
"public after",
"com",
pub("https://dy.nomadro.com/api/health"),
"cn",
pub("https://dy.nomadro.cn/api/health"),
"dylive",
pub("https://dylive.nomadro.com/api/health"),
flush=True,
)
g.close()
v.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())