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>
368 lines
12 KiB
Python
368 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Add dy.nomadro.cn to Baota nginx on Tencent → wg-dy-cn 10.9.0.2.
|
|
|
|
Disables the conflicting system Caddy (ports 80/443 owned by BT nginx).
|
|
"""
|
|
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"
|
|
USER = "ubuntu"
|
|
PASS = "Xiao4669805"
|
|
UP = "10.9.0.2"
|
|
DOMAIN = "dy.nomadro.cn"
|
|
VHOST = f"/www/server/panel/vhost/nginx/{DOMAIN}.conf"
|
|
WWWROOT = f"/www/wwwroot/{DOMAIN}"
|
|
CERT_LIVE = f"/etc/letsencrypt/live/{DOMAIN}"
|
|
|
|
|
|
def ssh():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(DOMESTIC, 22, USER, PASS, timeout=45, allow_agent=False, look_for_keys=False)
|
|
return c
|
|
|
|
|
|
def run(c, cmd, timeout=180):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
NGINX_HTTP = f"""# dy.nomadro.cn → physical app via wg-dy-cn {UP}
|
|
# Dual-edge: .cn uses Tencent WG; do not point at overseas 10.8.0.2
|
|
|
|
server {{
|
|
listen 80;
|
|
server_name {DOMAIN};
|
|
root {WWWROOT};
|
|
client_max_body_size 200m;
|
|
|
|
location ^~ /.well-known/acme-challenge/ {{
|
|
allow all;
|
|
root {WWWROOT};
|
|
}}
|
|
|
|
location / {{
|
|
return 301 https://$host$request_uri;
|
|
}}
|
|
|
|
access_log /www/wwwlogs/{DOMAIN}.log;
|
|
error_log /www/wwwlogs/{DOMAIN}.error.log;
|
|
}}
|
|
"""
|
|
|
|
NGINX_FULL = f"""# dy.nomadro.cn → physical app via wg-dy-cn {UP}
|
|
# Dual-edge: .cn uses Tencent WG; do not point at overseas 10.8.0.2
|
|
|
|
upstream dy_cn_api {{
|
|
server {UP}:8014;
|
|
keepalive 16;
|
|
}}
|
|
upstream dy_cn_web {{
|
|
server {UP}:4015;
|
|
keepalive 16;
|
|
}}
|
|
upstream dy_cn_pb {{
|
|
server {UP}:8092;
|
|
keepalive 8;
|
|
}}
|
|
upstream dy_cn_s3 {{
|
|
server {UP}:8333;
|
|
keepalive 8;
|
|
}}
|
|
upstream dy_cn_sc {{
|
|
server {UP}:9999;
|
|
keepalive 8;
|
|
}}
|
|
upstream dy_cn_neko1 {{ server {UP}:9200; keepalive 8; }}
|
|
upstream dy_cn_neko2 {{ server {UP}:9201; keepalive 8; }}
|
|
upstream dy_cn_neko3 {{ server {UP}:9202; keepalive 8; }}
|
|
upstream dy_cn_neko4 {{ server {UP}:9203; keepalive 8; }}
|
|
|
|
map $http_upgrade $connection_upgrade {{
|
|
default upgrade;
|
|
'' close;
|
|
}}
|
|
|
|
server {{
|
|
listen 80;
|
|
server_name {DOMAIN};
|
|
root {WWWROOT};
|
|
client_max_body_size 200m;
|
|
|
|
location ^~ /.well-known/acme-challenge/ {{
|
|
allow all;
|
|
root {WWWROOT};
|
|
}}
|
|
|
|
location / {{
|
|
return 301 https://$host$request_uri;
|
|
}}
|
|
|
|
access_log /www/wwwlogs/{DOMAIN}.log;
|
|
error_log /www/wwwlogs/{DOMAIN}.error.log;
|
|
}}
|
|
|
|
server {{
|
|
listen 443 ssl http2;
|
|
server_name {DOMAIN};
|
|
root {WWWROOT};
|
|
client_max_body_size 200m;
|
|
|
|
ssl_certificate {CERT_LIVE}/fullchain.pem;
|
|
ssl_certificate_key {CERT_LIVE}/privkey.pem;
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
ssl_ciphers HIGH:!aNULL:!MD5;
|
|
ssl_prefer_server_ciphers on;
|
|
ssl_session_cache shared:SSL:10m;
|
|
ssl_session_timeout 10m;
|
|
|
|
location ^~ /.well-known/acme-challenge/ {{
|
|
allow all;
|
|
root {WWWROOT};
|
|
}}
|
|
|
|
location /api/ {{
|
|
proxy_pass http://dy_cn_api;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header Connection '';
|
|
proxy_buffering off;
|
|
proxy_cache off;
|
|
proxy_read_timeout 3600s;
|
|
proxy_send_timeout 3600s;
|
|
}}
|
|
|
|
location /covers/ {{
|
|
proxy_pass http://dy_cn_api;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}}
|
|
|
|
location = /neko/1 {{ return 308 /neko/1/; }}
|
|
location /neko/1/ {{
|
|
proxy_pass http://dy_cn_neko1/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 3600s;
|
|
proxy_send_timeout 3600s;
|
|
proxy_buffering off;
|
|
}}
|
|
location = /neko/2 {{ return 308 /neko/2/; }}
|
|
location /neko/2/ {{
|
|
proxy_pass http://dy_cn_neko2/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 3600s;
|
|
proxy_buffering off;
|
|
}}
|
|
location = /neko/3 {{ return 308 /neko/3/; }}
|
|
location /neko/3/ {{
|
|
proxy_pass http://dy_cn_neko3/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 3600s;
|
|
proxy_buffering off;
|
|
}}
|
|
location = /neko/4 {{ return 308 /neko/4/; }}
|
|
location /neko/4/ {{
|
|
proxy_pass http://dy_cn_neko4/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 3600s;
|
|
proxy_buffering off;
|
|
}}
|
|
|
|
location /_/ {{
|
|
proxy_pass http://dy_cn_pb;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
}}
|
|
|
|
location /douyin-covers/ {{
|
|
proxy_pass http://dy_cn_s3;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}}
|
|
|
|
location = /shellcrash-ui {{ return 308 /shellcrash-ui/; }}
|
|
location /shellcrash-ui/ {{
|
|
proxy_pass http://dy_cn_sc/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
}}
|
|
|
|
# Clash/Meta dashboard paths (same as Caddy @clash)
|
|
location ~ ^/(ui|version|proxies|configs|rules|connections|providers|cache|dns|debug|traffic|logs|memory|restart|upgrade|group|script|fakeip|endpoint|runtime|meta|schema|flushfakeip|externaloverride|storage)(/|$) {{
|
|
proxy_pass http://dy_cn_sc;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_buffering off;
|
|
proxy_read_timeout 3600s;
|
|
}}
|
|
|
|
location / {{
|
|
proxy_pass http://dy_cn_web;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection $connection_upgrade;
|
|
proxy_read_timeout 120s;
|
|
}}
|
|
|
|
access_log /www/wwwlogs/{DOMAIN}.log;
|
|
error_log /www/wwwlogs/{DOMAIN}.error.log;
|
|
}}
|
|
"""
|
|
|
|
|
|
def write_remote(sftp, path, content):
|
|
with sftp.file(path, "w") as f:
|
|
f.write(content)
|
|
|
|
|
|
def nginx_reload(d) -> str:
|
|
return run(
|
|
d,
|
|
"sudo /www/server/nginx/sbin/nginx -t 2>&1 && "
|
|
"sudo /www/server/nginx/sbin/nginx -s reload 2>&1 || "
|
|
"sudo kill -HUP $(cat /www/server/nginx/logs/nginx.pid) 2>&1",
|
|
30,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
d = ssh()
|
|
print("=== disable conflicting caddy ===", flush=True)
|
|
print(run(d, "sudo systemctl disable --now caddy 2>/dev/null; sudo killall caddy 2>/dev/null; echo done", 20))
|
|
|
|
print("=== preflight WG API ===", flush=True)
|
|
pre = run(
|
|
d,
|
|
f"curl -sS -m 8 -o /dev/null -w 'hub_api=%{{http_code}}\\n' http://{UP}:8014/api/health; "
|
|
f"ping -c 2 -W 2 {UP} | tail -2",
|
|
20,
|
|
)
|
|
print(pre, flush=True)
|
|
if "hub_api=200" not in pre:
|
|
raise SystemExit("ABORT: cannot reach physical via wg-dy-cn")
|
|
|
|
print("=== prepare wwwroot + HTTP vhost ===", flush=True)
|
|
print(run(d, f"sudo mkdir -p {WWWROOT}/.well-known/acme-challenge; sudo chown -R www:www {WWWROOT} 2>/dev/null || sudo chown -R ubuntu:ubuntu {WWWROOT}", 20))
|
|
sftp = d.open_sftp()
|
|
write_remote(sftp, f"/tmp/{DOMAIN}.conf", NGINX_HTTP)
|
|
sftp.close()
|
|
print(run(d, f"sudo mv /tmp/{DOMAIN}.conf {VHOST} && sudo chmod 644 {VHOST}", 15))
|
|
print(nginx_reload(d), flush=True)
|
|
|
|
print("=== issue Let's Encrypt cert ===", flush=True)
|
|
# HTTP-01 via webroot; keep existing sites untouched
|
|
cert_out = run(
|
|
d,
|
|
f"sudo certbot certonly --webroot -w {WWWROOT} -d {DOMAIN} "
|
|
f"--agree-tos --email admin@nomadro.cn --non-interactive --keep-until-expiring 2>&1",
|
|
180,
|
|
)
|
|
print(cert_out, flush=True)
|
|
if "Successfully received certificate" not in cert_out and "Certificate not yet due" not in cert_out and "Keeping the existing" not in cert_out:
|
|
# fallback nginx plugin if webroot failed
|
|
cert_out2 = run(
|
|
d,
|
|
f"sudo certbot certonly --nginx -d {DOMAIN} --agree-tos --email admin@nomadro.cn --non-interactive 2>&1",
|
|
180,
|
|
)
|
|
print(cert_out2, flush=True)
|
|
|
|
has = run(d, f"sudo test -f {CERT_LIVE}/fullchain.pem && echo HAS_CERT || echo NO_CERT", 10).strip()
|
|
print(has, flush=True)
|
|
if "HAS_CERT" not in has:
|
|
raise SystemExit("ABORT: no TLS cert for dy.nomadro.cn")
|
|
|
|
print("=== write full HTTPS reverse proxy ===", flush=True)
|
|
sftp = d.open_sftp()
|
|
write_remote(sftp, f"/tmp/{DOMAIN}.conf", NGINX_FULL)
|
|
sftp.close()
|
|
print(run(d, f"sudo mv /tmp/{DOMAIN}.conf {VHOST} && sudo chmod 644 {VHOST}", 15))
|
|
print(nginx_reload(d), flush=True)
|
|
|
|
print("=== verify ===", flush=True)
|
|
# wait a moment for TLS
|
|
time.sleep(2)
|
|
print(
|
|
run(
|
|
d,
|
|
"curl -sS -m 15 -o /dev/null -w 'cn_api=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN}/api/health; "
|
|
"curl -sS -m 15 -o /dev/null -w 'cn_neko=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN}/neko/1/; "
|
|
"curl -sS -m 15 -o /dev/null -w 'cn_web=%{http_code} t=%{time_total}s\\n' "
|
|
f"https://{DOMAIN}/login; "
|
|
# ensure existing site still ok
|
|
"curl -sS -m 10 -o /dev/null -w 'ypp=%{http_code}\\n' https://ypp.nomadro.cn/ || true",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
d.close()
|
|
print("DONE — dy.nomadro.cn on BT nginx → 10.9.0.2", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|