Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
387 lines
12 KiB
Python
387 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Dual edge:
|
|
|
|
dy.nomadro.com → overseas VPS Caddy → wg-dy → 10.8.0.2
|
|
dy.nomadro.cn → Tencent Caddy → wg-dy-cn → 10.9.0.2
|
|
|
|
Does NOT touch dylive / other overseas sites. Keeps both WG tunnels.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
if hasattr(sys.stdout, "reconfigure"):
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
OVERSEAS = "107.173.30.245"
|
|
DOMESTIC = "82.157.112.245"
|
|
OVERSEAS_PASS = "Xiao4669805"
|
|
DOMESTIC_USER = "ubuntu"
|
|
DOMESTIC_PASS = "Xiao4669805"
|
|
GUEST_USER = "eric"
|
|
GUEST_PASS = "4669805"
|
|
FRP_PORT = 4014
|
|
|
|
LEGACY_WG = "10.8.0.2" # overseas path
|
|
CN_WG = "10.9.0.2" # domestic path
|
|
DOMAIN_COM = "dy.nomadro.com"
|
|
DOMAIN_CN = "dy.nomadro.cn"
|
|
PUBLIC_COM = f"https://{DOMAIN_COM}"
|
|
PUBLIC_CN = f"https://{DOMAIN_CN}"
|
|
|
|
def ssh(host, port, user, pw, tries=6):
|
|
last = None
|
|
for i in range(tries):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
host,
|
|
port,
|
|
user,
|
|
pw,
|
|
timeout=45,
|
|
banner_timeout=90,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print(f"ssh {host}:{port} try{i} {type(ex).__name__}: {ex}", flush=True)
|
|
time.sleep(2 + i)
|
|
raise last # type: ignore[misc]
|
|
|
|
|
|
def run(c, cmd, timeout=180):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def must_dylive(v, retries=4):
|
|
last = ""
|
|
for i in range(retries):
|
|
last = run(
|
|
v,
|
|
"curl -sS -m 20 -o /dev/null -w '%{http_code}' https://dylive.nomadro.com/api/health",
|
|
35,
|
|
).strip()
|
|
print(f"[dylive] try{i} {last}", flush=True)
|
|
if last == "200":
|
|
return
|
|
time.sleep(3 + i)
|
|
raise SystemExit(f"ABORT dylive health {last}")
|
|
|
|
|
|
def full_site(domain: str, upstream: str) -> str:
|
|
"""Preserve ShellCrash /ui + neko routes from the live physical edge site."""
|
|
return f"""# Dual-edge: {domain} → WG {upstream}
|
|
# Do not edit dylive.nomadro.com.caddy
|
|
# Generated by scripts/_setup_dual_edge_cn_com.py
|
|
|
|
{domain} {{
|
|
encode gzip
|
|
request_body {{
|
|
max_size 200MB
|
|
}}
|
|
|
|
# Zashboard: /ui without slash must redirect (browsers often open /ui#/… → blank)
|
|
@ui_noslash {{
|
|
path /ui
|
|
not path /ui/
|
|
not path /ui/*
|
|
}}
|
|
redir @ui_noslash /ui/ 308
|
|
|
|
handle /api/* {{
|
|
reverse_proxy {upstream}:8014 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle /covers/* {{
|
|
reverse_proxy {upstream}:8014 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
|
|
handle /neko/1 {{
|
|
redir /neko/1/ 308
|
|
}}
|
|
handle_path /neko/1/* {{
|
|
reverse_proxy {upstream}:9200 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
handle /neko/2 {{
|
|
redir /neko/2/ 308
|
|
}}
|
|
handle_path /neko/2/* {{
|
|
reverse_proxy {upstream}:9201 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
handle /neko/3 {{
|
|
redir /neko/3/ 308
|
|
}}
|
|
handle_path /neko/3/* {{
|
|
reverse_proxy {upstream}:9202 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
handle /neko/4 {{
|
|
redir /neko/4/ 308
|
|
}}
|
|
handle_path /neko/4/* {{
|
|
reverse_proxy {upstream}:9203 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
handle /_/* {{
|
|
reverse_proxy {upstream}:8092 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
handle /douyin-covers/* {{
|
|
reverse_proxy {upstream}:8333 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
|
|
handle /shellcrash-ui {{
|
|
redir /shellcrash-ui/ 308
|
|
}}
|
|
handle_path /shellcrash-ui/* {{
|
|
reverse_proxy {upstream}:9999 {{
|
|
header_up Host {{host}}
|
|
header_up X-Forwarded-Proto {{scheme}}
|
|
}}
|
|
}}
|
|
|
|
@clash path /ui/ /ui/* /version /proxies /proxies/* /configs /configs/* /rules /rules/* /connections /connections/* /providers /providers/* /cache /cache/* /dns /dns/* /debug /debug/* /traffic /logs /memory /restart /upgrade /upgrade/* /group /group/* /script /script/* /fakeip /fakeip/* /endpoint /endpoint/* /runtime /runtime/* /meta /meta/* /schema /schema/* /flushfakeip /externaloverride /externaloverride/* /storage /storage/*
|
|
handle @clash {{
|
|
reverse_proxy {upstream}:9999 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
|
|
handle {{
|
|
reverse_proxy {upstream}:4015 {{
|
|
header_up Host {{host}}
|
|
}}
|
|
}}
|
|
}}
|
|
"""
|
|
|
|
|
|
def revert_overseas_com(v) -> None:
|
|
print("=== revert dy.nomadro.com → 10.8.0.2 (overseas wg-dy) ===", flush=True)
|
|
site = "/etc/caddy/sites/dy.nomadro.com.caddy"
|
|
body = full_site(DOMAIN_COM, LEGACY_WG)
|
|
sftp = v.open_sftp()
|
|
ts = int(time.time())
|
|
try:
|
|
with sftp.file(site, "r") as cur:
|
|
old = cur.read()
|
|
with sftp.file(f"{site}.bak-dual-{ts}", "w") as bak:
|
|
bak.write(old)
|
|
except OSError:
|
|
pass
|
|
with sftp.file(site, "w") as f:
|
|
f.write(body)
|
|
sftp.close()
|
|
out = run(
|
|
v,
|
|
f"grep -E '10\\.(8|9)\\.0\\.' {site} | head -8; "
|
|
"caddy validate --config /etc/caddy/Caddyfile 2>&1; "
|
|
"systemctl reload caddy; sleep 2; systemctl is-active caddy",
|
|
60,
|
|
)
|
|
print(out, flush=True)
|
|
must_dylive(v)
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 12 -o /dev/null -w 'dy_com_api=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_COM}/api/health; "
|
|
"curl -sS -m 12 -o /dev/null -w 'dy_com_neko=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_COM}/neko/1/",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def install_caddy_domestic(d) -> None:
|
|
print("=== install Caddy on Tencent hub ===", flush=True)
|
|
print(
|
|
run(
|
|
d,
|
|
r"""sudo bash -c '
|
|
set -euo pipefail
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
if ! command -v caddy >/dev/null 2>&1; then
|
|
apt-get update -y
|
|
apt-get install -y --no-install-recommends debian-keyring debian-archive-keyring apt-transport-https curl gnupg
|
|
curl -1sLf "https://dl.cloudsmith.io/public/caddy/stable/gpg.key" | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
|
|
curl -1sLf "https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt" > /etc/apt/sources.list.d/caddy-stable.list
|
|
apt-get update -y
|
|
apt-get install -y caddy
|
|
fi
|
|
mkdir -p /etc/caddy/sites
|
|
if [ ! -f /etc/caddy/Caddyfile ] || ! grep -q "sites/\*\.caddy" /etc/caddy/Caddyfile 2>/dev/null; then
|
|
cat >/etc/caddy/Caddyfile <<EOF
|
|
{
|
|
email admin@nomadro.cn
|
|
}
|
|
import /etc/caddy/sites/*.caddy
|
|
EOF
|
|
fi
|
|
# open local firewall if ufw active
|
|
if command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -qi active; then
|
|
ufw allow 80/tcp || true
|
|
ufw allow 443/tcp || true
|
|
ufw allow 443/udp || true
|
|
fi
|
|
caddy version
|
|
systemctl enable caddy
|
|
'""",
|
|
300,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def write_cn_site(d) -> None:
|
|
print("=== write dy.nomadro.cn Caddy site → 10.9.0.2 ===", flush=True)
|
|
body = full_site(DOMAIN_CN, CN_WG)
|
|
sftp = d.open_sftp()
|
|
# write as ubuntu then sudo move
|
|
with sftp.file("/tmp/dy.nomadro.cn.caddy", "w") as f:
|
|
f.write(body)
|
|
sftp.close()
|
|
print(
|
|
run(
|
|
d,
|
|
"sudo mv /tmp/dy.nomadro.cn.caddy /etc/caddy/sites/dy.nomadro.cn.caddy && "
|
|
"sudo chmod 644 /etc/caddy/sites/dy.nomadro.cn.caddy && "
|
|
"sudo caddy validate --config /etc/caddy/Caddyfile 2>&1 && "
|
|
"sudo systemctl restart caddy && sleep 3 && "
|
|
"systemctl is-active caddy && "
|
|
"sudo ss -lntp | grep -E ':80|:443' || true",
|
|
90,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def update_physical_cors(g) -> None:
|
|
print("=== physical CORS / public base for both domains ===", flush=True)
|
|
cors = f"{PUBLIC_COM},{PUBLIC_CN},http://127.0.0.1:4015"
|
|
# Keep DY_PUBLIC_BASE_URL on .com (primary); CORS must include .cn
|
|
# NEKO edge: leave as-is if set; dual-edge WebRTC still uses overseas NAT for .com
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PASS}' | sudo -S -p '' bash -c '"
|
|
f"set -e; "
|
|
f"if [ -f /etc/douyinweb.env ]; then "
|
|
f" grep -q ^DY_CORS_ORIGINS= /etc/douyinweb.env && "
|
|
f" sed -i \"s|^DY_CORS_ORIGINS=.*|DY_CORS_ORIGINS={cors}|\" /etc/douyinweb.env || "
|
|
f" echo DY_CORS_ORIGINS={cors} >> /etc/douyinweb.env; "
|
|
f" grep -q ^DY_PUBLIC_BASE_URL= /etc/douyinweb.env || "
|
|
f" echo DY_PUBLIC_BASE_URL={PUBLIC_COM} >> /etc/douyinweb.env; "
|
|
f"fi; "
|
|
f"for svc in /etc/systemd/system/douyinweb-api.service /etc/systemd/system/douyinweb-web.service; do "
|
|
f" [ -f \"$svc\" ] || continue; "
|
|
f" if grep -q DY_CORS_ORIGINS= \"$svc\"; then "
|
|
f" sed -i \"s|Environment=DY_CORS_ORIGINS=.*|Environment=DY_CORS_ORIGINS={cors}|\" \"$svc\"; "
|
|
f" else "
|
|
f" sed -i \"/[Service]/a Environment=DY_CORS_ORIGINS={cors}\" \"$svc\"; "
|
|
f" fi; "
|
|
f"done; "
|
|
f"systemctl daemon-reload; "
|
|
f"systemctl restart douyinweb-api douyinweb-web; "
|
|
f"sleep 3; "
|
|
f"systemctl is-active douyinweb-api douyinweb-web; "
|
|
f"grep -E \"DY_CORS|DY_PUBLIC\" /etc/douyinweb.env /etc/systemd/system/douyinweb-api.service 2>/dev/null | head -20'",
|
|
120,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def verify(v, d) -> None:
|
|
print("=== verify both public edges ===", flush=True)
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 15 -o /dev/null -w 'com_api=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_COM}/api/health; "
|
|
"curl -sS -m 15 -o /dev/null -w 'com_neko=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_COM}/neko/1/; "
|
|
"curl -sS -m 8 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health; "
|
|
f"grep -E '10\\.(8|9)\\.0\\.' /etc/caddy/sites/{DOMAIN_COM}.caddy | head -4",
|
|
50,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(
|
|
run(
|
|
d,
|
|
"curl -sS -m 15 -o /dev/null -w 'cn_api=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_CN}/api/health; "
|
|
"curl -sS -m 15 -o /dev/null -w 'cn_neko=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN_CN}/neko/1/; "
|
|
"curl -sS -m 8 -o /dev/null -w 'cn_via_wg=%{http_code} t=%{time_total}s\\n' "
|
|
f"http://{CN_WG}:8014/api/health; "
|
|
f"sudo grep -E '10\\.(8|9)\\.0\\.' /etc/caddy/sites/{DOMAIN_CN}.caddy | head -4; "
|
|
"systemctl is-active caddy; sudo journalctl -u caddy -n 25 --no-pager",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
v = ssh(OVERSEAS, 22, "root", OVERSEAS_PASS)
|
|
must_dylive(v)
|
|
revert_overseas_com(v)
|
|
|
|
d = ssh(DOMESTIC, 22, DOMESTIC_USER, DOMESTIC_PASS)
|
|
# ensure hub can reach app
|
|
pre = run(
|
|
d,
|
|
f"ping -c 2 -W 2 {CN_WG} | tail -2; "
|
|
f"curl -sS -m 8 -o /dev/null -w 'hub_api=%{{http_code}}\\n' http://{CN_WG}:8014/api/health",
|
|
30,
|
|
)
|
|
print(pre, flush=True)
|
|
if "hub_api=200" not in pre:
|
|
raise SystemExit("ABORT: domestic hub cannot reach physical via wg-dy-cn")
|
|
|
|
install_caddy_domestic(d)
|
|
write_cn_site(d)
|
|
|
|
g = ssh(OVERSEAS, FRP_PORT, GUEST_USER, GUEST_PASS)
|
|
update_physical_cors(g)
|
|
g.close()
|
|
|
|
verify(v, d)
|
|
must_dylive(v)
|
|
v.close()
|
|
d.close()
|
|
print("DONE — dual edge: .com→overseas wg-dy, .cn→tencent wg-dy-cn", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|