#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Diagnose + open UDP 51821 on domestic Tencent for wg-dy-cn.""" 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" DOMESTIC_USER = "ubuntu" DOMESTIC_PASS = "Xiao4669805" OVERSEAS = "107.173.30.245" OVERSEAS_PASS = "Xiao4669805" def ssh(host, port, user, pw): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, 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, 22, DOMESTIC_USER, DOMESTIC_PASS) print("=== domestic firewall / listen ===") print( run( d, "sudo ss -ulnp | grep 51821; " "sudo ufw status verbose 2>/dev/null | head -20 || true; " "sudo iptables -L INPUT -n | head -15; " "curl -sS -m 3 http://metadata.tencentyun.com/latest/meta-data/public-ipv4; echo; " "curl -sS -m 3 http://metadata.tencentyun.com/latest/meta-data/instance-id; echo; " "command -v tccli || echo no_tccli", 60, ) ) print("=== try tccli open UDP 51821 (if configured) ===") print( run( d, "sudo bash -c '" "if command -v tccli >/dev/null 2>&1; then " " tccli configure list 2>/dev/null | head -5; " "else " " pip3 install -q tccli 2>/dev/null || apt-get install -y -qq python3-pip && pip3 install -q tccli; " "fi; " "echo tccli=$(command -v tccli || echo missing)'", 180, ) ) print("=== ensure ufw allows 51821 ===") print(run(d, "sudo ufw allow 51821/udp; sudo ufw status | grep 51821 || true", 30)) print("=== wg handshake from domestic perspective ===") print(run(d, "sudo wg show wg-dy-cn", 20)) d.close() v = ssh(OVERSEAS, 22, "root", OVERSEAS_PASS) print("=== restart wg-dy-cn on VPS + test ===") print(run(v, "wg-quick down wg-dy-cn; wg-quick up wg-dy-cn; sleep 3; wg show wg-dy-cn; ping -c 3 -W 2 10.9.0.2 | tail -3", 45)) v.close() d = ssh(DOMESTIC, 22, DOMESTIC_USER, DOMESTIC_PASS) print("=== domestic wg after VPS retry ===") print(run(d, "sudo wg show wg-dy-cn", 20)) d.close() if __name__ == "__main__": main()