Files
ykxmz/_build/setup_nginx.py
T
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

201 lines
7.6 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'''在 Lightsail 上装 nginx,把 yk.xxmxxs.top 反代到中转。
== 架构 ==
用户 ──HTTPS──> Cloudflare CDN ──HTTP:80──> nginx ──> 127.0.0.1:13245
(yk.xxmxxs.top) <源站IP>
== 为什么要加 nginx 这一层 ==
1. **Cloudflare 只能回源到特定端口**
(HTTP: 80/8080/8880/2052/2082/2086/2095,
HTTPS: 443/2053/2083/2087/2096/8443)
中转跑的 13245 **不在列表里** —— Cloudflare 没法直接回源。
2. **顺便把中转藏起来**
加了 nginx 之后,可以让 relay 只监听 127.0.0.1,
公网只能通过 nginx 进来。少一个暴露面。
3. **以后上 HTTPS 有地方挂证书**
现在先用 Cloudflare 的"灵活"模式(CF→源站走 HTTP),
以后要"完全(严格)"就把源站证书挂到 nginx 上。
== 关于端口 80 的安全 ==
nginx 监听 80 是**必须的**(Cloudflare 的开源端口)。
但要注意:如果有人知道 <源站IP>,也能直接打 80 绕过 Cloudflare。
所以配置里加了:
* 只接受来自 Cloudflare 网段的请求(可选,见 allowlist 那段)
* 或者更简单:不启用 allowlist,但把 relay 绑到 127.0.0.1,
反正绕不绕过都是同一个网站,没有额外信息泄露
这里选后者(简单、不容易把自己锁在外面)。
'''
from __future__ import annotations
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "_build"))
from lssh import run, upload # noqa: E402
DOMAIN = "yk.xxmxxs.top"
RELAY_PORT = "13245"
NGINX_SITE = '''# ykxmz 中转 —— 给 Cloudflare 回源用的
#
# Cloudflare 只能回源到特定端口,中转跑的 %(port)s 不在列表里,
# 所以中间放一层 nginx(监听 80)。
#
# 客户端拿到的大文件(安装包 15MB)会被 Cloudflare 缓存 ——
# 第二次下载就不回源了,速度取决于离用户最近的 CF 边缘节点。
server {
listen 80;
listen [::]:80;
server_name %(domain)s;
# 日志:单独一份,方便排查"到底是 CF 打的还是别人直接打的"
access_log /var/log/nginx/ykxmz-access.log;
error_log /var/log/nginx/ykxmz-error.log;
# ---- 下载:让 Cloudflare 放心缓存 ----
#
# 文件名带版本号(ykxmz-setup-2.1.3.exe),同一个名字内容永不改变,
# 所以可以长缓存。中转自己也发过 Cache-Control,
# 这里显式再写一遍是为了让 CF 一定看到。
location /download/ {
proxy_pass http://127.0.0.1:%(port)s;
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 CF-Connecting-IP $http_cf_connecting_ip;
proxy_buffering on;
proxy_cache_valid 200 7d;
add_header Cache-Control "public, max-age=604800, immutable" always;
}
# ---- 长轮询 ----
#
# 中转的 /v1/agent/poll 要挂 60 秒。nginx 默认 60 秒就断,
# 必须放宽,否则手表那边会一直掉线重连。
location /v1/ {
proxy_pass http://127.0.0.1:%(port)s;
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 CF-Connecting-IP $http_cf_connecting_ip;
proxy_read_timeout 180s;
proxy_send_timeout 180s;
proxy_connect_timeout 30s;
# 长轮询不能缓冲,否则要等整个响应完才吐给客户端 —— 那就不是长轮询了
proxy_buffering off;
proxy_cache off;
}
# ---- 其余(官网 / 管理页 / 抢救报告 / 截图)----
location / {
proxy_pass http://127.0.0.1:%(port)s;
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 CF-Connecting-IP $http_cf_connecting_ip;
proxy_read_timeout 180s;
proxy_connect_timeout 30s;
}
# 别把 nginx 版本号报出去
server_tokens off;
client_max_body_size 200m;
}
'''
def main() -> int:
print("=" * 62)
print(" 装 nginx,把 %s 反代到中转" % DOMAIN)
print("=" * 62)
print()
print(" ── 1. 装 nginx ──")
out = run("which nginx >/dev/null 2>&1 && echo ALREADY || "
"(sudo apt-get update -qq && "
"sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx) "
"&& echo OK; nginx -v 2>&1", timeout=600, quiet=True)
for ln in out[1].strip().splitlines()[-4:]:
print(" " + ln)
print()
print(" ── 2. 写站点配置 ──")
conf = NGINX_SITE % {"domain": DOMAIN, "port": RELAY_PORT}
tmp = Path(tempfile.gettempdir()) / "ykxmz.conf"
tmp.write_text(conf, encoding="utf-8", newline="\n")
upload(str(tmp), "/tmp/ykxmz.conf")
out = run("sudo mv /tmp/ykxmz.conf /etc/nginx/sites-available/ykxmz && "
"sudo ln -sf /etc/nginx/sites-available/ykxmz "
"/etc/nginx/sites-enabled/ykxmz && "
# 关掉默认站点,免得抢 80
"sudo rm -f /etc/nginx/sites-enabled/default && "
"sudo nginx -t 2>&1 | tail -3", timeout=180, quiet=True)
for ln in out[1].strip().splitlines():
print(" " + ln)
print()
print(" ── 3. 重启 nginx ──")
out = run("sudo systemctl enable nginx >/dev/null 2>&1; "
"sudo systemctl restart nginx; sleep 2; "
"systemctl is-active nginx", timeout=180, quiet=True)
print(" nginx: " + out[1].strip())
print()
print(" ── 4. 中转改为只监听 127.0.0.1(不再直接对公网)──")
# 公网只能通过 nginx 进来,少一个暴露面。
out = run("sudo sed -i 's/^Environment=RELAY_BIND=.*$/"
"Environment=RELAY_BIND=127.0.0.1/' "
"/etc/systemd/system/ykxmz-relay.service && "
"grep RELAY_BIND /etc/systemd/system/ykxmz-relay.service && "
"sudo systemctl daemon-reload && "
"sudo systemctl restart ykxmz-relay && sleep 3 && "
"systemctl is-active ykxmz-relay", timeout=180, quiet=True)
for ln in out[1].strip().splitlines():
print(" " + ln)
print()
print(" ── 5. 验证 ──")
script = (
'printf " %-22s %s\\n" "nginx 直连(本机:80)" '
'"$(curl -s -o /dev/null -w \'%{http_code}\' --max-time 8 '
'http://127.0.0.1:80/v1/health)"\n'
'printf " %-22s %s\\n" "带 Host 头" '
'"$(curl -s -o /dev/null -w \'%{http_code}\' --max-time 8 '
'-H \'Host: ' + DOMAIN + '\' http://127.0.0.1:80/v1/health)"\n'
'printf " %-22s %s\\n" "公网 80(IP)" '
'"$(curl -s -o /dev/null -w \'%{http_code}\' --max-time 15 '
'http://<源站IP>:80/v1/health)"\n'
'printf " %-22s %s\\n" "中转本机(13245)" '
'"$(curl -s -o /dev/null -w \'%{http_code}\' --max-time 8 '
'http://127.0.0.1:13245/v1/health)"\n'
'printf " %-22s %s\\n" "中转是否还暴露公网" '
'"$(curl -s -o /dev/null -w \'%{http_code}\' --max-time 8 '
'http://<源站IP>:13245/v1/health)"\n'
)
out = run(script, timeout=180, quiet=True)
for ln in out[1].strip().splitlines():
print(ln)
print()
print(" 说明:上面「中转是否还暴露公网」应该是 000(连不上)——")
print(" 那正说明它已经只认 127.0.0.1,公网只能走 nginx。")
return 0
if __name__ == "__main__":
sys.exit(main())