# -*- coding: utf-8 -*- """Harden WG keepalive + deploy aggressive watchdog. No Chrome/Neko touch.""" from __future__ import annotations import sys import time import urllib.request from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") PASS = "4669805" ROOT = Path(__file__).resolve().parents[1] SERVICE = """[Unit] Description=douyinWEB WireGuard aggressive keepalive watchdog After=network-online.target Wants=network-online.target [Service] Type=simple Environment=WIREGUARD_WATCH_IFACES=wg-dy wg-dy-cn Environment=WIREGUARD_WATCH_INTERVAL=15 Environment=WIREGUARD_STALE_SECONDS=90 Environment=WIREGUARD_RESTART_COOLDOWN=60 ExecStart=/opt/douyinWEB/scripts/linux/wireguard_watch.sh Restart=always RestartSec=5 [Install] WantedBy=multi-user.target """ def ssh(host, port, user, pw): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(12): 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=120): _, 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 f"{r.status}" except Exception as ex: return f"ERR {getattr(ex, 'code', type(ex).__name__)}" def main() -> int: print("health before", "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) sftp = g.open_sftp() with sftp.file("/tmp/_wg_watch_hard.sh", "wb") as f: f.write((ROOT / "scripts/linux/wireguard_watch.sh").read_bytes()) with sftp.file("/tmp/_wg_watch_hard.service", "w") as f: f.write(SERVICE) sftp.close() sh = r"""#!/bin/bash set -euo pipefail install -m 755 /tmp/_wg_watch_hard.sh /opt/douyinWEB/scripts/linux/wireguard_watch.sh sed -i 's/\r$//' /opt/douyinWEB/scripts/linux/wireguard_watch.sh install -m 644 /tmp/_wg_watch_hard.service /etc/systemd/system/douyinweb-wireguard-watch.service sed -i 's/\r$//' /etc/systemd/system/douyinweb-wireguard-watch.service mkdir -p /etc/systemd/system/douyinweb-wireguard-watch.service.d cat >/etc/systemd/system/douyinweb-wireguard-watch.service.d/aggressive.conf <<'EOF' [Service] Environment= Environment=WIREGUARD_WATCH_IFACES=wg-dy wg-dy-cn Environment=WIREGUARD_WATCH_INTERVAL=15 Environment=WIREGUARD_STALE_SECONDS=90 Environment=WIREGUARD_RESTART_COOLDOWN=60 Restart=always RestartSec=5 EOF # Tighten keepalive in confs (15s) for conf in /etc/wireguard/wg-dy.conf /etc/wireguard/wg-dy-cn.conf; do [ -f "$conf" ] || continue if grep -qE '^[[:space:]]*PersistentKeepalive[[:space:]]*=' "$conf"; then sed -i -E 's/^([[:space:]]*PersistentKeepalive[[:space:]]*=[[:space:]]*)[0-9]+/\115/' "$conf" else sed -i '/^[[:space:]]*AllowedIPs[[:space:]]*=/a PersistentKeepalive = 15' "$conf" fi echo "=== $conf ==="; grep -E 'Keepalive|Endpoint|AllowedIPs|Address' "$conf" || true done # Apply runtime keepalive without full bounce if already healthy for iface in wg-dy wg-dy-cn; do ip link show "$iface" >/dev/null 2>&1 || continue for peer in $(wg show "$iface" peers 2>/dev/null); do wg set "$iface" peer "$peer" persistent-keepalive 15 || true done done systemctl daemon-reload systemctl enable douyinweb-wireguard-watch systemctl reset-failed douyinweb-wireguard-watch || true systemctl restart douyinweb-wireguard-watch sleep 1 systemctl is-active douyinweb-wireguard-watch wg-quick@wg-dy wg-quick@wg-dy-cn || true echo '=== watch once ===' /opt/douyinWEB/scripts/linux/wireguard_watch.sh once || true echo '=== wg status ===' wg show wg-dy echo '---' wg show wg-dy-cn | head -20 echo '=== ping peers ===' ping -c 2 -W 2 10.8.0.1 || true ping -c 2 -W 2 10.9.0.1 || true echo '=== journal ===' journalctl -u douyinweb-wireguard-watch -n 15 --no-pager || true echo HARDEN_OK """ with g.open_sftp() as sftp2: with sftp2.file("/tmp/_wg_harden.sh", "w") as f: f.write(sh) print(run(g, f"echo '{PASS}' | sudo -S -p '' bash /tmp/_wg_harden.sh", timeout=90), flush=True) # Verify from VPS v = ssh("107.173.30.245", 22, "root", "Xiao4669805") print("VPS->10.8.0.2", run(v, "ping -c 2 -W 2 10.8.0.2; curl -sS -m 6 -o /dev/null -w 'api:%{http_code}\\n' http://10.8.0.2:8014/api/health"), flush=True) v.close() g.close() print("health 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) return 0 if __name__ == "__main__": raise SystemExit(main())