douyinWEB/scripts/_fix_wg_watch_env_quote.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

103 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
"""Fix systemd Environment quoting so BOTH wg-dy and wg-dy-cn are watched."""
from __future__ import annotations
import sys
import time
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
PASS = "4669805"
# Quotes are required — unquoted "wg-dy wg-dy-cn" only keeps first token.
DROPIN = """[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
"""
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 main() -> int:
g = paramiko.SSHClient()
g.set_missing_host_key_policy(paramiko.AutoAddPolicy())
g.connect(
"107.173.30.245",
4014,
"eric",
PASS,
timeout=45,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
sftp = g.open_sftp()
with sftp.file("/tmp/_wg_watch_fix.service", "w") as f:
f.write(SERVICE)
with sftp.file("/tmp/_wg_watch_fix.dropin", "w") as f:
f.write(DROPIN)
sftp.close()
cmd = f"""echo '{PASS}' | sudo -S -p '' bash -lc '
set -e
install -m 644 /tmp/_wg_watch_fix.service /etc/systemd/system/douyinweb-wireguard-watch.service
mkdir -p /etc/systemd/system/douyinweb-wireguard-watch.service.d
# Replace ALL drop-ins that may have broken unquoted ifaces
rm -f /etc/systemd/system/douyinweb-wireguard-watch.service.d/*.conf
install -m 644 /tmp/_wg_watch_fix.dropin /etc/systemd/system/douyinweb-wireguard-watch.service.d/aggressive.conf
systemctl daemon-reload
systemctl reset-failed douyinweb-wireguard-watch || true
systemctl restart douyinweb-wireguard-watch
sleep 1
systemctl is-active douyinweb-wireguard-watch
systemctl show douyinweb-wireguard-watch -p Environment --no-pager
journalctl -u douyinweb-wireguard-watch -n 8 --no-pager
# prove both ifaces are in the env the process sees
tr "\\0" "\\n" < /proc/$(systemctl show -p MainPID --value douyinweb-wireguard-watch)/environ | grep WIREGUARD || true
echo FIX_OK
'"""
_, o, e = g.exec_command(cmd, timeout=60)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out)
g.close()
for url in [
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.cn/api/health",
"https://dylive.nomadro.com/api/health",
]:
try:
with urllib.request.urlopen(url, timeout=15) as r:
print(url, r.status)
except Exception as ex:
print(url, "ERR", getattr(ex, "code", ex))
return 0 if "FIX_OK" in out else 1
if __name__ == "__main__":
raise SystemExit(main())