Files
ykxmz/_build/diag_server_self_access.py
T
xmz7891 a53a3eee21 搬到 Lightsail:nginx 反代 + TLS + 公共 DNS + 后台口令改由服务器持有
== 架构(最终形态)==

    用户 --HTTPS--> Cloudflare CDN --HTTP/HTTPS--> nginx --+--> 127.0.0.1:13245
                    yk.xxmxxs.top                  13.54.30.196

中转只绑 127.0.0.1,公网只能经 nginx 进来 —— 少一个暴露面
(实测公网直连 :13245 返回 000,符合预期)。

== 这一轮加的 ==

1. **nginx 反代**(_build/setup_nginx.py)
   Cloudflare 只能回源到特定端口(80/8080/443/2053…),
   中转跑的 13245 不在列表里,所以中间必须有一层。
   配置要点:
     /download/  proxy_buffering on
     /v1/        proxy_read_timeout 180s + **proxy_buffering off**
                 (长轮询要挂 60 秒;关缓冲,否则要等整个响应完
                   才吐给客户端,那就不是长轮询了)

2. **源站 TLS 证书**(_build/setup_tls_origin.py)
   Cloudflare 的 SSL 模式是 Full(strict),回源走 443,源站只有 80
   -> 522。用 certbot 的 nginx 插件签了 Let's Encrypt 证书。
   开着橙色云朵也能签:HTTP-01 挑战经 Cloudflare 回源到 80 能验证。
   踩的坑:第一次用 example.com 当邮箱,Let's Encrypt 拒收保留域名。

3. **公共 DNS**(_build/fix_server_dns.py)
   换域名之后 **服务器自己解析不了** yk.xxmxxs.top:
   Lightsail 默认的内网解析器 172.26.0.2 还缓存着"不存在"(负缓存),
   而 1.1.1.1 / 8.8.8.8 已经能解析。
   后果不只是监测误报 —— 服务器上任何解析该域名的东西都会失败。
   配上公共 DNS,内网那个降为后备(它对 AWS 自家服务更快)。

4. **监测与预案重写**(relay/monitor-lightsail.sh、rescue-lightsail.sh)
   分两层探测,才能分清是哪一层坏的:
     PRIMARY   https://域名          用户实际走的
     FALLBACK  127.0.0.1:13245       中转本身
     PRIMARY 挂 + FALLBACK 好 -> edge 层 -> 动 nginx
     两个都挂               -> core 层 -> 动中转
   不区分的话只能瞎重启,而 edge 层的问题重启中转一万次也没用。

   做法上也改了:不再"SSH 过去用嵌套 heredoc 现场打补丁"
   (这一轮因此踩了五次转义/求值/百分号格式化的坑),
   改成**本地写完整文件、整份上传**,能进 git 做版本管理。

5. **后台口令改由服务器持有**(_build/set_admin_password.py)
   密码只写进 /opt/ykxmz-relay/data/admin_password.txt(600),
   **不进代码、不进仓库**。本地脚本从环境变量或
   _build/.admin_password(已 gitignore)读取。
   这个仓库已经吃过一次"管理员密码硬编码进提交历史"的教训了。

6. **验证脚本**(_build/verify_admin_login.py)
   第一版只看登录请求的状态码,结果"对的密码/错的密码/不带凭据"
   **全是 302** —— 等于没测。改成看 cookie 再访问 /admin,
   区分拿到的是登录页还是后台页。实测:
     正确密码 -> cookie 1 条 -> 后台页
     错误密码 -> cookie 0 条 -> 登录页
     账号写错 -> cookie 0 条 -> 登录页
     经 Cloudflare 域名 -> 后台页

== 其他修复 ==

  * 下载响应有**两条** Cache-Control(nginx 和 relay 各发一条),
    Cloudflare 遇到重复的缓存指令可能直接跳过缓存 -> 去掉 nginx 那条
  * 监测探测地址跟着监听地址改(改入口必须回头看所有探测入口)
  * curl 加 -L 跟跳转(certbot 加了 http->https 301)
  * 旧服务器(随身 WiFi 那台)的服务全部停掉并取消自启,
    但**没关机、没删文件** —— 保持可逆

全量回归 46 个阶段通过;594 个 Python 文件语法通过。
2026-10-08 23:07:45 +08:00

104 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
r'''查服务器为什么访问不了自己的域名(curl 返回 000)。
== 现象 ==
从我的电脑 -> https://yk.xxmxxs.top = 200 ✅
从服务器自己-> https://yk.xxmxxs.top = 000 ❌
监测单元就跑在服务器上、查的就是这个域名,所以它一直判失败
-> fails 一直涨 -> 会启动紧急预案 -> 瞎折腾。
== 最可能的原因:IPv6 ==
yk.xxmxxs.top 在 Cloudflare 上有 **AAAA 记录**:
2606:4700:3036::6815:362d
2606:4700:3036::ac43:df96
Lightsail 的实例默认**没有 IPv6 地址**。curl 会先试 IPv6、失败,
如果不自动回退到 IPv4,就直接报连接失败(=000)。
验证办法很简单:`curl -4`(强制 IPv4)通不通。
== 其他可能 ==
* DNS 在服务器上解析不出来(getent hosts 看看)
* Cloudflare 拒绝来自源站自己 IP 的请求(回环防护)——不太可能
* 出站被挡(Lightsail 默认出站全开,概率低)
'''
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"
REMOTE = r'''
D=__DOMAIN__
echo "════ 1. 服务器有没有 IPv6 ════"
ip -brief addr show 2>/dev/null | sed 's/^/ /'
echo " --- IPv6 默认路由 ---"
ip -6 route show default 2>/dev/null | sed 's/^/ /' || echo " (没有 IPv6 默认路由)"
echo
echo "════ 2. 域名解析(IPv4 / IPv6 分开看)════"
echo " A 记录:"
getent ahostsv4 $D 2>/dev/null | awk '{print " " $1}' | sort -u
echo " AAAA 记录:"
getent ahostsv6 $D 2>/dev/null | awk '{print " " $1}' | sort -u
echo " --- dig 结果 ---"
dig +short A $D 2>/dev/null | sed 's/^/ A: /'
dig +short AAAA $D 2>/dev/null | sed 's/^/ AAAA: /'
echo
echo "════ 3. curl 各种姿势 ════"
printf ' %-30s %s\n' "默认(可能走 IPv6)" "$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 https://$D/v1/health)"
printf ' %-30s %s\n' "强制 IPv4 (-4)" "$(curl -s4 -o /dev/null -w '%{http_code}' --max-time 15 https://$D/v1/health)"
printf ' %-30s %s\n' "强制 IPv6 (-6)" "$(curl -s6 -o /dev/null -w '%{http_code}' --max-time 10 https://$D/v1/health)"
printf ' %-30s %s\n' "http 强制 IPv4" "$(curl -s4L -o /dev/null -w '%{http_code}' --max-time 15 http://$D/v1/health)"
echo
echo " --- 详细报错(默认那条)---"
curl -s -o /dev/null -w ' %{http_code}\n' --max-time 10 https://$D/v1/health 2>&1 || true
curl -v -o /dev/null --max-time 12 https://$D/v1/health 2>&1 | grep -iE 'trying|connected|refused|timed|error|Could not' | head -10 | sed 's/^/ /'
echo
echo "════ 4. 直连 Cloudflare 边缘 IP(跳过 DNS)════"
for ip in 104.21.54.45 172.67.223.150; do
printf ' %-20s %s\n' "$ip (HTTPS)" "$(curl -sk -o /dev/null -w '%{http_code}' --max-time 12 --resolve $D:443:$ip https://$D/v1/health)"
done
echo
echo "════ 5. 出站到 Cloudflare 通不通(纯 TCP)════"
for ip in 104.21.54.45 172.67.223.150; do
if timeout 6 bash -c "</dev/tcp/$ip/443" 2>/dev/null; then
echo " $ip:443 通"
else
echo " $ip:443 不通"
fi
done
echo
echo "════ 6. 对比:直连自己的 nginx ════"
printf ' %-30s %s\n' "127.0.0.1:443(带 SNI)" "$(curl -sk -o /dev/null -w '%{http_code}' --max-time 8 --resolve $D:443:127.0.0.1 https://$D/v1/health)"
printf ' %-30s %s\n' "公网 IP:443" "$(curl -sk -o /dev/null -w '%{http_code}' --max-time 8 --resolve $D:443:13.54.30.196 https://$D/v1/health)"
'''
def main() -> int:
print("=" * 62)
print(" 查服务器为什么访问不了自己的域名")
print("=" * 62)
out = run(REMOTE.replace("__DOMAIN__", DOMAIN), timeout=300, quiet=True)
print(out[1] if isinstance(out, tuple) else out)
return 0
if __name__ == "__main__":
sys.exit(main())