# -*- coding: utf-8 -*- """ Native WireGuard split-tunnel between US VPS and PVE Ubuntu guest. Safety for VPS live traffic: - Does NOT start docker / wg-easy - Does NOT restart caddy, docker, sing-box, or any existing unit - New iface name wg-dy (not wg0) - AllowedIPs 10.8.0.0/24 only (no default-route hijack) - No iptables policy changes; no ufw - After each VPS step, verify https://dylive.nomadro.com/api/health """ from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") VPS = "107.173.30.245" ROOT_PASS = "Xiao4669805" ERIC_PASS = "4669805" def connect(host, port, user, password): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, password, timeout=30, allow_agent=False, look_for_keys=False) return c def run(c, cmd, timeout=120): _, o, e = c.exec_command(cmd, timeout=timeout) out = (o.read() + e.read()).decode("utf-8", "replace") return out def must_dylive(v, label): out = run( v, "curl -sS -m 8 -o /dev/null -w '%{http_code}' https://dylive.nomadro.com/api/health; " "echo; systemctl is-active caddy sing-box docker", 20, ) print(f"[dylive {label}]\n{out}") if not out.strip().startswith("200"): raise SystemExit(f"ABORT: dylive health not 200 after {label}") def main() -> int: v = connect(VPS, 22, "root", ROOT_PASS) p = connect(VPS, 4014, "eric", ERIC_PASS) print("=== refuse if this PVE session is a hypervisor ===") virt = run(p, "systemd-detect-virt; test ! -d /etc/pve && echo not_pve_host") print(virt) if "kvm" not in virt or "not_pve_host" not in virt: raise SystemExit("ABORT: unexpected guest") must_dylive(v, "start") print("=== VPS: install wireguard-tools only (no upgrade) ===") print( run( v, "export DEBIAN_FRONTEND=noninteractive; " "apt-get install -y --no-install-recommends wireguard-tools >/tmp/wg-apt.log 2>&1; " "tail -5 /tmp/wg-apt.log; command -v wg; " "sysctl -n net.ipv4.ip_forward", 180, ) ) must_dylive(v, "after apt wireguard-tools") print("=== generate keys on VPS ===") keys = run( v, r""" set -e mkdir -p /etc/wireguard chmod 700 /etc/wireguard if [ ! -f /etc/wireguard/wg-dy.server.key ]; then wg genkey | tee /etc/wireguard/wg-dy.server.key | wg pubkey > /etc/wireguard/wg-dy.server.pub wg genkey | tee /etc/wireguard/wg-dy.pve.key | wg pubkey > /etc/wireguard/wg-dy.pve.pub chmod 600 /etc/wireguard/wg-dy.*.key fi echo SERVER_PRIV $(cat /etc/wireguard/wg-dy.server.key) echo SERVER_PUB $(cat /etc/wireguard/wg-dy.server.pub) echo PVE_PRIV $(cat /etc/wireguard/wg-dy.pve.key) echo PVE_PUB $(cat /etc/wireguard/wg-dy.pve.pub) """, 30, ) print(keys) kv = {} for line in keys.splitlines(): parts = line.split() if len(parts) == 2 and parts[0] in ("SERVER_PRIV", "SERVER_PUB", "PVE_PRIV", "PVE_PUB"): kv[parts[0]] = parts[1] if len(kv) != 4: raise SystemExit("key parse failed") server_conf = f"""[Interface] Address = 10.8.0.1/24 ListenPort = 51820 PrivateKey = {kv['SERVER_PRIV']} [Peer] PublicKey = {kv['PVE_PUB']} AllowedIPs = 10.8.0.2/32 """ pve_conf = f"""[Interface] Address = 10.8.0.2/24 PrivateKey = {kv['PVE_PRIV']} [Peer] PublicKey = {kv['SERVER_PUB']} Endpoint = 107.173.30.245:51820 AllowedIPs = 10.8.0.0/24 PersistentKeepalive = 25 """ print("=== write VPS /etc/wireguard/wg-dy.conf (new iface only) ===") sftp = v.open_sftp() with sftp.file("/etc/wireguard/wg-dy.conf", "w") as f: f.write(server_conf) sftp.chmod("/etc/wireguard/wg-dy.conf", 0o600) sftp.close() print("=== bring up wg-dy on VPS (not wg0, not docker) ===") print( run( v, "wg-quick down wg-dy 2>/dev/null || true; " "wg-quick up wg-dy; " "systemctl enable wg-quick@wg-dy >/dev/null; " "wg show wg-dy; ip -4 addr show wg-dy", 30, ) ) must_dylive(v, "after wg-quick up") print("=== PVE guest: wireguard-tools + ffmpeg (guest only) ===") print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "export DEBIAN_FRONTEND=noninteractive; " "apt-get update -y >/tmp/pve-apt.log 2>&1; " "apt-get install -y --no-install-recommends wireguard-tools ffmpeg python3-venv python3-pip >/tmp/pve-apt2.log 2>&1; " "echo APT_OK; command -v wg; ffmpeg -version | head -1; python3 -m venv -h | head -1" "'", 300, ) ) print("=== write PVE wg-dy.conf ===") sftp = p.open_sftp() with sftp.file("/tmp/wg-dy.conf", "w") as f: f.write(pve_conf) sftp.close() print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "install -m 600 /tmp/wg-dy.conf /etc/wireguard/wg-dy.conf; " "rm -f /tmp/wg-dy.conf; " "wg-quick down wg-dy 2>/dev/null || true; " "wg-quick up wg-dy; " "systemctl enable wg-quick@wg-dy >/dev/null; " "wg show wg-dy; ip -4 addr show wg-dy" "'", 30, ) ) print("=== wait handshake + ping ===") time.sleep(3) print("VPS ping 10.8.0.2:", run(v, "ping -c 3 -W 2 10.8.0.2 || true", 15)) print("PVE ping 10.8.0.1:", run(p, "ping -c 3 -W 2 10.8.0.1 || true", 15)) print("VPS wg:", run(v, "wg show wg-dy", 10)) must_dylive(v, "after pve wg up") # prevent accidental docker compose up of broken wg-easy run( v, "grep -q 'profiles:' /opt/wg-easy/docker-compose.yml || " "sed -i 's/container_name: wg-easy/container_name: wg-easy\\n profiles: [\"disabled\"]/' /opt/wg-easy/docker-compose.yml", 10, ) v.close() p.close() print("WG_OK") return 0 if __name__ == "__main__": raise SystemExit(main())