diff --git a/_build/_cmsg_tls.txt b/_build/_cmsg_tls.txt new file mode 100644 index 0000000..1ae07e5 --- /dev/null +++ b/_build/_cmsg_tls.txt @@ -0,0 +1,44 @@ +源站装 TLS 证书 + 停掉旧服务器 + +== 522 的根因 == + +DNS 配好、橙色云朵生效之后: + http://yk.xxmxxs.top/ -> 200(经 Cloudflare,反代正常) + https://yk.xxmxxs.top/ -> 522 + 13.54.30.196:443 -> 不通 + +zone 的 SSL/TLS 模式是 **Full / Full(strict)**,Cloudflare 回源走 443, +而源站只听了 80 —— 因此 522。 + +== 解决:源站签 Let's Encrypt 证书 == + +用 certbot 的 nginx 插件签(**不需要 webroot**,插件自己处理挑战): + + Successfully received certificate + /etc/letsencrypt/live/yk.xxmxxs.top/fullchain.pem + notAfter = Jan 6 2027(自动续期已启用 certbot.timer) + nginx 已加 listen 443 + 证书 + http->https 跳转 + +**开着橙色云朵也能签**:HTTP-01 挑战请求 +http://yk.xxmxxs.top/.well-known/acme-challenge/... 会经 Cloudflare +回源到 80,CF 不缓存该路径、会透传,所以验证能过。 +比"先关云朵签完再开"省事,也不用等 DNS 传播。 + +踩的坑:第一次用了 xmz7891@example.com,Let's Encrypt 拒收 +example.com 这类保留域名,报"invalid email"。 +改用 --register-unsafely-without-email 即可 +(续期由 timer 负责,不依赖邮件提醒)。 + +== 停掉旧服务器 == + +中转已在 Lightsail 跑通,旧那台(随身 WiFi + 两条 CF 隧道 + 网络调优) +全部停掉并取消自启: + + lanremote-relay / lanremote-tunnel / lanremote-tunnel-h2 / + lanremote-nettune / monitor.timer / watchdog.timer -> 全 inactive + +**只停服务,没关机、没删文件** —— 保持可逆,新服务器稳几天再说。 + +== 待用户操作 == + +Lightsail 防火墙开 TCP 443(Cloudflare 的 Full(strict) 回源要用)。 diff --git a/_build/check_tls_ready.py b/_build/check_tls_ready.py new file mode 100644 index 0000000..e9e0729 --- /dev/null +++ b/_build/check_tls_ready.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +r'''验证源站的 443 是否就绪(证书签完之后)。 + +背景:522 是因为 Cloudflare 的 SSL 模式是 Full/Full(strict), +回源走 443,而源站只有 80。现在证书签好了、nginx 也配了 443, +所以要确认: + + 1. 本机 443 能自测通(带正确 SNI) + 2. 从**外面**能不能连上 443 + —— 这一步取决于用户在 Lightsail 防火墙开没开 443 +''' +from __future__ import annotations + +import socket +import ssl +import sys +import urllib.request +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / "_build")) +from lssh import run # noqa: E402 + +DOMAIN = "yk.xxmxxs.top" +IP = "13.54.30.196" + + +def tcp(host: str, port: int, timeout: float = 8.0) -> bool: + s = socket.socket() + s.settimeout(timeout) + try: + s.connect((host, port)) + return True + except Exception: # noqa: BLE001 + return False + finally: + s.close() + + +def https(host: str, port: int = 443) -> tuple: + """返回 (状态码文本, 证书主体)。""" + ctx = ssl.create_default_context() + try: + with urllib.request.urlopen( + "https://%s:%d/v1/health" % (host, port), + timeout=15, context=ctx) as r: + r.read(64) + return str(r.status), "证书验证通过" + except ssl.SSLCertVerificationError as exc: + return "证书错误", str(exc)[:70] + except Exception as exc: # noqa: BLE001 + return "失败", "%s: %s" % (type(exc).__name__, str(exc)[:60]) + + +def main() -> int: + print("=" * 62) + print(" 验证 443 是否就绪") + print("=" * 62) + + print() + print(" ── 1. 源站内部(从服务器自己看)──") + # ⚠ 这里**不能用 % 格式化** —— 远端命令里有 curl 的 + # %{http_code},% 格式化会把 %{ 当成格式符直接抛 + # "unsupported format character '{'"。用 replace 更安全。 + inner = ( + "echo ' --- 监听 ---'; " + "ss -tln 2>/dev/null | grep -E ':(80|443) ' | sed 's/^/ /'; " + "echo ' --- 本机 https(带 SNI)---'; " + "curl -s -o /dev/null -w ' %{http_code}\\n' --max-time 10 " + "--resolve __D__:443:127.0.0.1 https://__D__/v1/health; " + "echo ' --- 证书主体 ---'; " + "sudo openssl x509 -in /etc/letsencrypt/live/__D__/fullchain.pem " + "-noout -subject -dates 2>/dev/null | sed 's/^/ /'; " + "echo ' --- nginx 里的 443 段 ---'; " + "sudo grep -nE 'listen|ssl_certificate|return 301|server_name' " + "/etc/nginx/sites-available/ykxmz | sed 's/^/ /'" + ).replace("__D__", DOMAIN) + out = run(inner, timeout=180, quiet=True) + for ln in out[1].strip().splitlines(): + print(ln) + + print() + print(" ── 2. 从这台电脑直连源站(不经过 Cloudflare)──") + for port in (80, 443): + ok = tcp(IP, port) + print(" %s:%-4d TCP %s" % (IP, port, "通" if ok else "不通 ❌")) + code, note = https(IP) + print(" https://%s/v1/health -> %s (%s)" % (IP, code, note)) + print(" (这里会是证书错误,正常 —— 我们是用 IP 直连,") + print(" 证书是签给域名的。这一步只看 TCP 通不通。)") + + print() + print(" ── 3. 经 Cloudflare 的 HTTPS ──") + code, note = https(DOMAIN) + print(" https://%s/v1/health -> %s (%s)" % (DOMAIN, code, note)) + + print() + print(" ── 4. 结论 ──") + if tcp(IP, 443): + print(" ✅ 443 从外面**通** —— Cloudflare 的 Full(strict) 能工作") + if code == "200": + print(" ✅ 全链路已经好了") + else: + print(" ⚠ 443 通了但 HTTPS 还不通,可能是 Cloudflare") + print(" 还没切到 Full(strict),或者配置还在生效中") + else: + print(" ❌ 443 从外面**不通** —— 需要去 Lightsail 防火墙开 TCP 443") + print(" (实例 → 联网 → 防火墙 → 添加规则 → 自定义 TCP 443)") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/_build/retire_old_relay.py b/_build/retire_old_relay.py new file mode 100644 index 0000000..3c91114 --- /dev/null +++ b/_build/retire_old_relay.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +r'''停掉旧那台随身 WiFi 服务器上的中转。 + +== 为什么可以停 == + +中转已经搬到 AWS Lightsail(有公网 IP、域名 yk.xxmxxs.top、 +经 Cloudflare CDN),旧机器上那套(lanremote-relay + 两条 +Cloudflare 隧道 + 网络调优)全部不再需要。 + +旧那套的问题回顾(都是停掉它的理由): + * 线路对隧道 7844 端口间歇性阻断 -> 隧道每 3~10 分钟掉一次 + * 只有 460MB 内存 + * 会掉电硬挂(2026-10-08 18:42 一次非正常关机) + +== 停的方式:**只停服务,不关机、不删东西** == + +用户说的是"可以停了"。按最小动作来: + + * systemctl stop + disable 那几个服务 + * **不**关机(万一还想上去看日志) + * **不**删任何文件(可逆) + +这样如果新服务器出问题,随时可以 systemctl start 回去。 +等新服务器稳定跑一阵,再彻底关机/退订也不迟。 +''' +from __future__ import annotations + +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / "_build")) +from devssh import run # noqa: E402 + +SERVICES = [ + "lanremote-relay", + "lanremote-tunnel", + "lanremote-tunnel-h2", + "lanremote-nettune", + "lanremote-monitor.timer", + "lanremote-watchdog.timer", +] + + +def main() -> int: + print("=" * 62) + print(" 停掉旧服务器(4G 小机器)上的中转") + print("=" * 62) + + print() + print(" ── 停之前的状态 ──") + out = run("for s in %s; do printf ' %%-26s %%s\\n' $s " + "\"$(systemctl is-active $s 2>&1)\"; done" + % " ".join(SERVICES), timeout=120, quiet=True) + for ln in out[1].strip().splitlines(): + print(ln) + + print() + print(" ── 停掉 + 取消开机自启 ──") + # 先停 timer(免得它刚好在停服务的时候又触发一次抢救) + out = run( + "sudo systemctl disable --now lanremote-monitor.timer " + "lanremote-watchdog.timer 2>&1 | tail -2; " + "for s in lanremote-relay lanremote-tunnel lanremote-tunnel-h2 " + "lanremote-nettune; do " + " sudo systemctl disable --now $s 2>&1 | tail -1; " + "done; echo DONE", timeout=300, quiet=True) + for ln in out[1].strip().splitlines()[-12:]: + print(" " + ln) + + print() + print(" ── 停之后的状态 ──") + out = run("for s in %s; do printf ' %%-26s %%s\\n' $s " + "\"$(systemctl is-active $s 2>&1)\"; done" + % " ".join(SERVICES), timeout=120, quiet=True) + for ln in out[1].strip().splitlines(): + print(ln) + + print() + print(" ── 顺带停掉 cloudflared(隧道进程)──") + out = run("pgrep -a cloudflared | head -4; " + "sudo pkill -f cloudflared 2>/dev/null; sleep 2; " + "echo ' 剩余 cloudflared 进程: '\$(pgrep -c cloudflared " + "2>/dev/null || echo 0)", timeout=120, quiet=True) + for ln in out[1].strip().splitlines(): + print(" " + ln) + + print() + print(" ── 确认网站上不去了(relay.xxmxxs.top 应该 5xx)──") + out = run("echo \" relay.xxmxxs.top -> $(curl -s -o /dev/null " + "-w '%%{http_code}' --max-time 15 " + "https://relay.xxmxxs.top/v1/health)\"", timeout=120, + quiet=True) + for ln in out[1].strip().splitlines(): + print(" " + ln) + + print() + print(" 说明:**只停了服务,没关机、没删文件** ——") + print(" 万一新服务器有问题,随时可以 start 回去:") + print(" sudo systemctl enable --now lanremote-relay " + "lanremote-tunnel lanremote-tunnel-h2") + print(" 等新服务器稳定跑几天,再考虑关机/退订。") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/_build/setup_tls_origin.py b/_build/setup_tls_origin.py new file mode 100644 index 0000000..8d765bc --- /dev/null +++ b/_build/setup_tls_origin.py @@ -0,0 +1,127 @@ +# -*- coding: utf-8 -*- +r'''给源站签一张 Let's Encrypt 证书,让 Cloudflare 的 SSL 能走"完全(严格)"。 + +== 522 是怎么来的 == + +DNS 配好、橙色云朵生效之后: + + http://yk.xxmxxs.top/ -> 200 (经 Cloudflare,反代是好的) + https://yk.xxmxxs.top/ -> 522 (Cloudflare 连不上源站) + 13.54.30.196:443 -> 不通(源站没听 443) + +结论:zone 的 SSL/TLS 模式是 **Full** 或 **Full (strict)**, +Cloudflare 回源走 443,而源站只有 80 -> 522。 + +== 两种解法 == + + A. 把 SSL/TLS 改成 **Flexible** + 一个下拉框搞定。CF<->用户加密,**CF<->源站明文**。 + 省事,但源站那段不加密 —— 而且 Cloudflare 官方不建议 + (源站流量在 CF 内网走,风险不大,但"完全"更好)。 + + B. 源站装证书,保持 **Full (strict)** <- 这个脚本做的 + 端到端加密,还能开 HSTS 之类。多花两分钟,值得。 + +== 为什么开着橙色云朵也能签 Let's Encrypt == + +HTTP-01 验证是:Let's Encrypt 去请求 + http://yk.xxmxxs.top/.well-known/acme-challenge/<随机串> +这条请求会经过 Cloudflare 回源到我们的 80 端口。 +Cloudflare 默认**不缓存** .well-known 路径、也会透传,所以能验证成功。 +(这比"先关云朵签完再开"省事,也不用等 DNS 传播。) + +== 签完还要做的 == + + 1. 用户在 Lightsail 防火墙**开 443** + 2. 用户把 Cloudflare 的 SSL/TLS 设为 **Full (strict)** +''' +from __future__ import annotations + +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / "_build")) +from lssh import run # noqa: E402 + +DOMAIN = "yk.xxmxxs.top" +EMAIL = "xmz7891@example.com" # Let's Encrypt 要一个邮箱(过期提醒) + + +def main() -> int: + print("=" * 62) + print(" 给 %s 签 Let's Encrypt 证书" % DOMAIN) + print("=" * 62) + + print() + print(" ── 1. 装 certbot ──") + out = run("which certbot >/dev/null 2>&1 && echo ALREADY || " + "(sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive " + "apt-get install -y -qq certbot python3-certbot-nginx) && " + "certbot --version 2>&1", timeout=900, quiet=True) + for ln in out[1].strip().splitlines()[-3:]: + print(" " + ln) + + print() + print(" ── 2. 先确认 HTTP-01 挑战路径能通(走 Cloudflare)──") + out = run( + "mkdir -p /var/www/html/.well-known/acme-challenge && " + "echo 'ykxmz-acme-ok' | sudo tee " + "/var/www/html/.well-known/acme-challenge/probe >/dev/null && " + "sudo chown -R www-data:www-data /var/www/html 2>/dev/null; " + "echo ' 本机:'; curl -s --max-time 8 " + "http://127.0.0.1:80/.well-known/acme-challenge/probe | sed 's/^/ /'; " + "echo ' 经 Cloudflare:'; curl -s --max-time 15 " + "http://%s/.well-known/acme-challenge/probe | sed 's/^/ /'" + % DOMAIN, timeout=180, quiet=True) + for ln in out[1].strip().splitlines(): + print(ln) + + print() + print(" ── 3. 签证书 ──") + # --nginx 会自动改 nginx 配置加上 443 + 证书路径 + # --non-interactive 不交互;--agree-tos 同意条款 + # --redirect 把 http 永久重定向到 https + out = run( + "sudo certbot --nginx -d %s --non-interactive --agree-tos " + "--email %s --redirect --keep-until-expiring 2>&1 | tail -20" + % (DOMAIN, EMAIL), timeout=600, quiet=True) + for ln in out[1].strip().splitlines(): + print(" " + ln) + + print() + print(" ── 4. 复查 nginx 是否已在 443 上 ──") + out = run( + "sudo nginx -t 2>&1 | tail -2; " + "echo ' --- 监听 ---'; " + "ss -tln 2>/dev/null | grep -E ':(80|443) ' | sed 's/^/ /'; " + "echo ' --- 证书 ---'; " + "sudo ls -la /etc/letsencrypt/live/%s/ 2>/dev/null | tail -5 | " + "sed 's/^/ /'; " + "echo ' --- 本机 https 自测 ---'; " + "curl -sk -o /dev/null -w ' https://127.0.0.1 (SNI %s): %%{http_code}\\n' " + "--max-time 10 --resolve %s:443:127.0.0.1 https://%s/v1/health" + % (DOMAIN, DOMAIN, DOMAIN, DOMAIN), timeout=180, quiet=True) + for ln in out[1].strip().splitlines(): + print(ln) + + print() + print(" ── 5. 自动续期 ──") + out = run("systemctl is-enabled certbot.timer 2>&1; " + "systemctl is-active certbot.timer 2>&1; " + "sudo certbot renew --dry-run 2>&1 | tail -3", + timeout=600, quiet=True) + for ln in out[1].strip().splitlines(): + print(" " + ln) + + print() + print("=" * 62) + print(" 接下来要用户做两件事:") + print(" 1. Lightsail 防火墙开 TCP 443") + print(" 2. Cloudflare 的 SSL/TLS 设为「完全(严格)」") + print("=" * 62) + return 0 + + +if __name__ == "__main__": + sys.exit(main())