105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Open UDP 51820 on VPS for wg-dy. Live flush. No Neko."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import httpx
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
|
|
|
|
def connect():
|
|
for i in range(8):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
"107.173.30.245",
|
|
22,
|
|
"root",
|
|
"Xiao4669805",
|
|
timeout=25,
|
|
banner_timeout=40,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
print("connected", flush=True)
|
|
return c
|
|
except Exception as ex:
|
|
print(f"retry {i} {ex}", flush=True)
|
|
time.sleep(2)
|
|
raise SystemExit("ssh fail")
|
|
|
|
|
|
def run(c, cmd, t=45):
|
|
print(f"$ {cmd[:220]}", flush=True)
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
while not o.channel.exit_status_ready():
|
|
if o.channel.recv_ready():
|
|
sys.stdout.write(o.channel.recv(4096).decode("utf-8", "replace"))
|
|
sys.stdout.flush()
|
|
if o.channel.recv_stderr_ready():
|
|
sys.stdout.write(o.channel.recv_stderr(4096).decode("utf-8", "replace"))
|
|
sys.stdout.flush()
|
|
time.sleep(0.1)
|
|
sys.stdout.write((o.read() + e.read()).decode("utf-8", "replace"))
|
|
sys.stdout.flush()
|
|
print(f"→ {o.channel.recv_exit_status()}", flush=True)
|
|
|
|
|
|
c = connect()
|
|
run(c, "ufw status verbose 2>/dev/null | head -40; iptables -L INPUT -n | head -30; iptables -L INPUT -n | grep -E '51820|51821' || true")
|
|
run(
|
|
c,
|
|
"ufw allow 51820/udp comment 'wg-dy' 2>/dev/null || true; "
|
|
"iptables -C INPUT -p udp --dport 51820 -j ACCEPT 2>/dev/null || iptables -I INPUT -p udp --dport 51820 -j ACCEPT; "
|
|
"iptables -C INPUT -p udp --dport 51821 -j ACCEPT 2>/dev/null || iptables -I INPUT -p udp --dport 51821 -j ACCEPT; "
|
|
"echo rules_ok; "
|
|
"ufw status | grep 51820 || true; "
|
|
"iptables -L INPUT -n | grep 5182 || true",
|
|
)
|
|
c.close()
|
|
|
|
# poke from guest
|
|
print("## guest poke", flush=True)
|
|
g = connect()
|
|
# reconnect as guest
|
|
g.close()
|
|
g = paramiko.SSHClient()
|
|
g.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
g.connect(
|
|
"107.173.30.245",
|
|
4014,
|
|
"eric",
|
|
"4669805",
|
|
timeout=25,
|
|
banner_timeout=40,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
print("connected :4014", flush=True)
|
|
run(
|
|
g,
|
|
"echo 4669805 | sudo -S -p '' bash -lc '"
|
|
"wg-quick down wg-dy; wg-quick up wg-dy; sleep 5; "
|
|
"wg show wg-dy; ping -c 4 -W 2 10.8.0.1 | tail -6"
|
|
"'",
|
|
t=50,
|
|
)
|
|
g.close()
|
|
|
|
c = connect()
|
|
run(c, "wg show wg-dy; ping -c 3 -W 2 10.8.0.2 | tail -5; curl -sS -m 8 -o /dev/null -w 'via_wg=%{http_code}\\n' http://10.8.0.2:8014/api/health", t=35)
|
|
c.close()
|
|
|
|
with httpx.Client(follow_redirects=False, timeout=12) as cl:
|
|
for u in ["https://dy.nomadro.com/api/health", "https://dylive.nomadro.com/api/health"]:
|
|
try:
|
|
print(u, cl.get(u).status_code, flush=True)
|
|
except Exception as ex:
|
|
print(u, ex, flush=True)
|
|
print("DONE", flush=True)
|