144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Emergency: dy.nomadro.com via SSH-FRP local forwards (WG one-way dead).
|
|
Physical FRP: 127.0.0.1:4014 eric/4669805 on VPS.
|
|
Maps:
|
|
VPS 127.0.0.1:18014 -> guest 127.0.0.1:8014
|
|
VPS 127.0.0.1:14015 -> guest 127.0.0.1:4015
|
|
Caddy dy.nomadro.com temporarily points at these instead of 10.8.0.2.
|
|
No Neko. Live flush.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import httpx
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
|
|
HOST = "107.173.30.245"
|
|
ROOT_PW = "Xiao4669805"
|
|
GUEST_PW = "4669805"
|
|
|
|
|
|
def connect():
|
|
for i in range(10):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
HOST,
|
|
22,
|
|
"root",
|
|
ROOT_PW,
|
|
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 RuntimeError("ssh fail")
|
|
|
|
|
|
def run(c, cmd, t=120):
|
|
print(f"$ {cmd[:240]}", 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("->", o.channel.recv_exit_status(), flush=True)
|
|
|
|
|
|
REMOTE = r'''
|
|
set -e
|
|
SITE=/etc/caddy/sites/dy.nomadro.com.caddy
|
|
cp -a "$SITE" "$SITE.bak.frpaux.$(date +%s)"
|
|
|
|
# Install sshpass if needed for non-interactive ssh
|
|
command -v sshpass >/dev/null || apt-get install -y sshpass >/dev/null 2>&1 || true
|
|
|
|
# systemd unit for FRP-aux HTTP tunnels (via existing :4014)
|
|
cat >/etc/systemd/system/dy-frp-http-aux.service <<'EOF'
|
|
[Unit]
|
|
Description=dy.nomadro.com FRP-aux HTTP tunnels (8014/4015 via :4014)
|
|
After=network-online.target frpp.service
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
Restart=always
|
|
RestartSec=3
|
|
Environment=SSHPASS=4669805
|
|
ExecStart=/usr/bin/sshpass -e ssh -N -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -L 127.0.0.1:18014:127.0.0.1:8014 -L 127.0.0.1:14015:127.0.0.1:4015 -p 4014 eric@127.0.0.1
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now dy-frp-http-aux.service
|
|
sleep 2
|
|
systemctl is-active dy-frp-http-aux.service
|
|
ss -lntp | grep -E ':(18014|14015)\b' || true
|
|
curl -sS -m 8 http://127.0.0.1:18014/api/health | head -c 160; echo
|
|
curl -sS -m 8 -o /dev/null -w 'aux_web=%{http_code}\n' http://127.0.0.1:14015/
|
|
|
|
# Point Caddy main app proxies to FRP-aux locals (keep neko ports on 10.8.0.2 for now)
|
|
# Only replace API/web backends that use 8014/4015
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
p = Path("/etc/caddy/sites/dy.nomadro.com.caddy")
|
|
text = p.read_text(encoding="utf-8")
|
|
# common patterns
|
|
text2 = text.replace("reverse_proxy 10.8.0.2:8014", "reverse_proxy 127.0.0.1:18014")
|
|
text2 = text2.replace("reverse_proxy 10.8.0.2:4015", "reverse_proxy 127.0.0.1:14015")
|
|
if text2 == text:
|
|
print("WARN: no 8014/4015 replacements made")
|
|
else:
|
|
p.write_text(text2, encoding="utf-8")
|
|
print("caddy site rewritten 8014/4015 -> frp-aux")
|
|
print("sample:")
|
|
for i, line in enumerate(p.read_text(encoding="utf-8").splitlines(), 1):
|
|
if "reverse_proxy" in line and ("18014" in line or "14015" in line or "8014" in line or "4015" in line):
|
|
print(f"{i}:{line}")
|
|
PY
|
|
|
|
caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -20
|
|
systemctl reload caddy || systemctl restart caddy
|
|
sleep 2
|
|
systemctl is-active caddy
|
|
'''
|
|
|
|
print("## install FRP-aux tunnels + retarget Caddy", flush=True)
|
|
c = connect()
|
|
run(c, REMOTE, t=180)
|
|
c.close()
|
|
|
|
print("## public", flush=True)
|
|
with httpx.Client(follow_redirects=False, timeout=20) as cl:
|
|
for u in [
|
|
"https://dy.nomadro.com/api/health",
|
|
"https://dy.nomadro.com/",
|
|
"https://dy.nomadro.com/login",
|
|
"https://dylive.nomadro.com/api/health",
|
|
]:
|
|
try:
|
|
r = cl.get(u)
|
|
print(f"{u} -> {r.status_code} loc={(r.headers.get('location') or '')[:40]}", flush=True)
|
|
except Exception as ex:
|
|
print(f"{u} ERR {ex}", flush=True)
|
|
|
|
print("DONE", flush=True)
|