Files
ykxmz/_build/setup_tls_origin.py
xmz7891 9bdd36cf56 2.1.4:修 URL 协议头 bug + 清理对外材料里泄漏的服务器信息
用户报了三件事,一起处理。

== 一、发行版介绍里泄漏了服务器公网 IP ==

_build/CHANGELOG.md 的 2.1.3 那节写了服务器公网 IP,而发布脚本
把这个文件当"更新说明"传给 Gitee,于是发行版介绍里就带出去了。

为什么 IP 不该公开:直接打 IP 就绕过 Cloudflare 了 ——
没有 CDN、没有防护、没有速率限制。源站本来就不该被人知道是谁
(这也是为什么 nginx 那层只认域名、中转只绑 127.0.0.1)。

处理:
  * 已发的 v2.1.3 发行版介绍用 API 改掉(扫了全部 11 个发行版,只它一个)
  * 仓库里 16 个文件里的 IP 全部换成域名或占位符
  * 运维脚本改成从环境变量读,公开仓库里不留具体地址

顺手发现**更严重的一处**:旧管理员密码硬编码在
docs/AFTER-INSTALL.txt(会打进服务端 zip)和
installer/license-combined.txt(**安装包的许可页,每个装的人都能看到**)里。
而用户当前密码就含这个串,等于公开了一半。一并清掉,
公开材料里改成"密码在你服务器上"的说明。

== 二、手表端填了地址却连不上 ==

用户截图:填域名(没写协议头)-> 点「检查服务器」报

    MalformedURLException: no protocol: <域名>/v1/health

根因:补协议头的逻辑只写在连接那条路(RelayTransport 的构造函数)上,
checkServer() 直接拿原始字符串拼 URL —— 同一个地址,连接能用、检查却崩。

修法:把规范化收到 relayUrl() 这**唯一一个入口**,所有调用方都从那里取。
规则和电脑端一致:IP / localhost / *.local -> http,域名 -> https。

顺带补了 _tests/test_relay_url_normalize.py 盯住它(含 9 个用例实算)。

== 三、Gitee 的 README 被判"可能含有违规信息" ==

Gitee 挂的是软性公告(可自行修改或申诉),但确实挂在那。
逐项看了 README,改掉几处**不准确**且容易误判的表述:

  * 原文写"看看学生机是不是在打游戏" —— 这是**错的**:
    被控端只装在一台电脑上(讲台那台),根本看不到别的机器。
    既不准确又像监控软件,改成"作用于被控端那一台"
  * 课堂那几项功能如实说明:改的是本机 hosts、结束的是本机进程,
    都可逆,下课点「一键恢复全部」就还原
  * "免杀对抗""伪装成系统进程"这类词(虽然是在说"我们不做这些")
    本身就在审核词库里,换成正面的说法
  * 成品文件名从 1.8.2 更新到当前版本,并且以后发版自动跟着改
    (把 README 加进了 bump 脚本;以前一直停在 1.8.2,
     用户照着 README 去 releases 根本找不到那个文件)

== 四、做这件事时自己踩的坑(都写进注释了)==

  * 批量替换时**没排除 gitignore 的文件**,把本地密码文件也改了
  * 替换时往字典字面量中间塞注释,**弄坏了 5 个文件的语法**
  * 修的时候手写"坏文件列表",漏了 2 个 ——
    改成全仓库扫模式(列表是观察到的,不是穷尽的)
  * 清理工具自己又成了泄漏点(脚本里写了要匹配的 IP/密码),
    加了 _build/precommit_leak_gate.py 当闸门才抓到

教训:批量替换要先排除不该碰的文件,而且不能往表达式中间塞东西。

== 按用户要求:测完再提交 ==

    全量回归 41 个阶段全 PASS(2 项因锁屏 SKIP)
    601 个 Python 文件语法通过
    pyflakes 干净
    安装包完整周期 20/20
    对外材料清洁度 15/15
    提交前泄漏闸门通过

(这条信息是补写的:上一个提交因为 heredoc 把提交说明打到屏幕而不是
 文件,信息变成空的 "remove temp" 了,内容本身是对的。)
2026-10-08 23:51:17 +08:00

128 lines
4.8 KiB
Python
Raw Permalink 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'''给源站签一张 Let's Encrypt 证书,让 Cloudflare 的 SSL 能走"完全(严格)"。
== 522 是怎么来的 ==
DNS 配好、橙色云朵生效之后:
http://yk.xxmxxs.top/ -> 200 (经 Cloudflare,反代是好的)
https://yk.xxmxxs.top/ -> 522 (Cloudflare 连不上源站)
yk.xxmxxs.top: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())