#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Open UDP 51821 on domestic (ufw + baota/aapanel) and verify WG mesh.""" from __future__ import annotations import sys import time import paramiko if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace") DOMESTIC = ("82.157.112.245", 22, "ubuntu", "Xiao4669805") OVERSEAS = ("107.173.30.245", 22, "root", "Xiao4669805") GUEST = ("107.173.30.245", 4014, "eric", "4669805") def ssh(t): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(t[0], t[1], t[2], t[3], timeout=45, allow_agent=False, look_for_keys=False) return c def run(c, cmd, timeout=90): _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace") def main(): d = ssh(DOMESTIC) print("=== open UDP 51821 (iptables + ufw + bt) ===") print( run( d, "sudo bash -c '" "ufw allow 51821/udp; " "iptables -I INPUT -p udp --dport 51821 -j ACCEPT; " "iptables -I YJ-FIREWALL-INPUT -p udp --dport 51821 -j ACCEPT 2>/dev/null || true; " "iptables -I IN_BT -p udp --dport 51821 -j ACCEPT 2>/dev/null || true; " "iptables -L INPUT -n | head -8; " "iptables -L YJ-FIREWALL-INPUT -n 2>/dev/null | head -8 || true" "'", 45, ) ) # tcpdump while overseas sends wg-init print("=== tcpdump 51821 (5s) while VPS pings ===") d.exec_command( "sudo timeout 8 tcpdump -ni any udp port 51821 -c 5 2>/tmp/wg-cap.log &", timeout=5, ) time.sleep(1) v = ssh(OVERSEAS) print(run(v, "wg-quick down wg-dy-cn 2>/dev/null; wg-quick up wg-dy-cn; sleep 2; wg show wg-dy-cn", 30)) v.close() time.sleep(6) print(run(d, "cat /tmp/wg-cap.log 2>/dev/null | tail -10", 10)) print("=== wg status all sides ===") print(run(d, "sudo wg show wg-dy-cn", 15)) v = ssh(OVERSEAS) print(run(v, "wg show wg-dy-cn; ping -c 3 -W 2 10.9.0.2 | tail -3", 30)) v.close() g = ssh(GUEST) print( run( g, "echo '4669805' | sudo -S -p '' bash -c '" "wg-quick down wg-dy-cn 2>/dev/null; wg-quick up wg-dy-cn; sleep 2; " "wg show wg-dy-cn; ping -c 3 -W 2 10.9.0.1 | tail -3'", 45, ) ) g.close() print(run(d, "sudo wg show wg-dy-cn", 15)) d.close() if __name__ == "__main__": main()