mirror of
https://gitee.com/xmz7891/ykxmz.git
synced 2026-10-18 13:40:45 +00:00
用户反馈两件事:
1. 早上打开 relay.xxmxxs.top 看到 Cloudflare Error 1033 —— "这就是你说的优化好了?"
2. "中转管理那边 ui 界面大改,现在难看的要死"
============ 一、隧道 1033/502/530 的真因 ============
之前我只测 15~25 次就说"可用率 100%",太草率 ——
实际是**间歇性假活**:隧道显示 ha_connections=2 连着,
公网却 502/530/000。watchdog 日志把过程记了下来。
根因:**4G 链路静默断开 TCP 连接**(不发 FIN/RST,双方都以为还活着)。
Cloudflare 把请求发到这条僵尸连接上就回错误。
Linux 默认 tcp_keepalive_time = **7200 秒(2 小时)**才发现,
中间这段时间服务一直"看着在线、实际打不开"。
修法(都有实测数据支撑):
1. tcp_keepalive 30 秒 + 每 10 秒探测 3 次
=> 最多约 60 秒发现僵尸连接并重连
2. 隧道连接数 2 -> 4
(对照实验:http2+2 只有 1 条连上时公网 0/12;
http2+4 稳定 12/12。连接数太少会出现"只有 1 条"的危险窗口)
3. 协议定为 http2(cloudflared 自检说 TCP 7844 被墙,
但实测 http2 反而比 quic 稳)
实测:http2+4 之后公网 40/40 = 100%,最慢 1.3 秒,0 次重连。
============ 二、中转管理后台重做 ============
原来是几百行内联在 relay.py 的 f-string 里(HTML+CSS+JS 全挤一起),
改一处样式要在一堆转义花括号里翻。拆成独立模块 relay/admin.py。
新版设计(对齐官网那套白色简洁):
* 吸顶顶栏:品牌 + 运行状态 + 版本/内存/请求数
* 公网地址给最大权重(这是用户最需要复制的东西)
* 四个数字卡(在线/会话数带颜色区分)
* 设备表卡片:状态圆点、等宽配对码、行悬停
* 工具卡:网络体检 + 下载区并排
* 空状态改成图标+引导文案,不再是干巴巴一句
* 窄屏时表格自动变卡片
relay.py 从 2089 行缩到 1550 行。
============ 三、顺手修的两个真 bug ============
1. 设备记录只增不减
self.devices 从不清理,被控端每次换端口/重装就多一条,
实测一天攒了 75 条僵尸记录,/v1/health 从几 KB 涨到 20KB。
加 DEVICE_TTL(6 小时)在 snapshot() 里清。
2. 配对码映射也没清
RELAY.codes 和 devices 是一对孪生 dict,
只清 devices 会留下指向已删设备的孤儿条目 ——
管理页显示"配对码占用 131",实际一台设备都没有。
现在一起清。
============ 四、我自己造的两次事故(如实记录) ============
1. 删文件删过头
用"从 _admin_page 截到文件末尾"的方式替换方法,
但 _admin_page 后面**还有 _esc/_kb/_uptime/class Server/main()**。
结果 relay 起不来,systemd 重启 24 次,所有接口 000。
教训:**"删到文件末尾"之前必须先确认末尾有什么。**
2. ADMIN_PATH 引用了不存在的常量
路由里写的是字面量,我却在新代码里用了 ADMIN_PATH,
管理页一访问就 NameError。现在统一定义了常量。
二者都是"改完没立刻验"造成的。现在每次改完都跑一遍
管理页 + 官网 + 关键接口的回归。
144 lines
5.1 KiB
Python
144 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
r'''把产品展示区块接进官网,并把截图传上去。'''
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
|
|
LANDING = ROOT / "relay" / "landing.py"
|
|
RELAY = ROOT / "relay" / "relay.py"
|
|
sys.path.insert(0, str(ROOT / "_build"))
|
|
|
|
from devssh import run, upload # noqa: E402
|
|
|
|
|
|
def wire_landing() -> None:
|
|
text = LANDING.read_text(encoding="utf-8")
|
|
|
|
if "showcase" in text:
|
|
print(" landing.py 已经接过 showcase")
|
|
return
|
|
|
|
# 1) 引入
|
|
text = text.replace(
|
|
"from urllib.parse import quote",
|
|
"from urllib.parse import quote\n\n"
|
|
"import showcase", 1)
|
|
|
|
# 2) CSS 拼进去
|
|
marker = '""" % {"BG": BG, "SURFACE": SURFACE, "BORDER": BORDER, "TEXT": TEXT,'
|
|
if marker in text:
|
|
text = text.replace(
|
|
marker,
|
|
'""" + showcase.SHOWCASE_CSS % {"BG": BG, "SURFACE": SURFACE,\n'
|
|
' "BORDER": BORDER, "TEXT": TEXT,', 1)
|
|
print(" CSS 已拼接")
|
|
else:
|
|
print(" [警告] CSS 锚点没找到,跳过")
|
|
|
|
# 3) HTML 区块插到"下载"之前
|
|
anchor = '<section id="download">'
|
|
if anchor in text:
|
|
text = text.replace(
|
|
anchor,
|
|
'%(showcase)s\n\n' + anchor, 1)
|
|
# 4) 渲染参数里加上
|
|
text = text.replace(
|
|
' "dls": _download_cards(files),',
|
|
' "showcase": showcase.render_showcase(),\n'
|
|
' "dls": _download_cards(files),', 1)
|
|
print(" HTML 区块已插入")
|
|
else:
|
|
print(" [警告] download 区块锚点没找到")
|
|
|
|
LANDING.write_text(text, encoding="utf-8", newline="\n")
|
|
try:
|
|
ast.parse(text)
|
|
print(" landing.py 语法 OK")
|
|
except SyntaxError as exc:
|
|
print(" [失败] 语法错误 行 %s: %s" % (exc.lineno, exc.msg))
|
|
lines = text.splitlines()
|
|
for i in range(max(0, (exc.lineno or 1) - 3),
|
|
min(len(lines), (exc.lineno or 1) + 2)):
|
|
print(" %d: %s" % (i + 1, lines[i][:92]))
|
|
|
|
|
|
def wire_relay_route() -> None:
|
|
"""加一条 /shot/<name> 路由,把截图发出去(白名单后缀)。"""
|
|
text = RELAY.read_text(encoding="utf-8")
|
|
if "/shot/" in text:
|
|
print(" relay.py 已经有 /shot/ 路由")
|
|
return
|
|
|
|
# 找下载路由附近插入
|
|
anchor = 'if path.startswith("/download/"):'
|
|
if anchor not in text:
|
|
print(" [警告] 找不到 /download/ 路由锚点,手工看下")
|
|
return
|
|
|
|
block = ''' # 官网用的产品截图。
|
|
# 单独一条路由(不放进下载区),只允许 png/jpg/webp。
|
|
if path.startswith("/shot/"):
|
|
name = path[len("/shot/"):]
|
|
if (not name or ".." in name or "/" in name or "\\\\" in name
|
|
or not name.lower().endswith((".png", ".jpg", ".jpeg",
|
|
".webp"))):
|
|
self._json(404, {"ok": False, "error": "没有这张图"})
|
|
return
|
|
target = Path(__file__).resolve().parent / "site_shots" / name
|
|
if not target.is_file():
|
|
self._json(404, {"ok": False, "error": "没有这张图"})
|
|
return
|
|
body = target.read_bytes()
|
|
ctype = ("image/png" if name.lower().endswith(".png")
|
|
else "image/jpeg")
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", ctype)
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.send_header("Cache-Control", "public, max-age=86400")
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
return
|
|
|
|
'''
|
|
text = text.replace(" " + anchor, block + " " + anchor, 1)
|
|
RELAY.write_text(text, encoding="utf-8", newline="\n")
|
|
try:
|
|
ast.parse(text)
|
|
print(" relay.py 语法 OK")
|
|
except SyntaxError as exc:
|
|
print(" [失败] relay.py 语法错误 行 %s: %s" % (exc.lineno, exc.msg))
|
|
|
|
|
|
def deploy() -> None:
|
|
print()
|
|
print(" 部署")
|
|
run("mkdir -p /root/lanremote-relay/site_shots", timeout=60)
|
|
upload(str(ROOT / "_build" / "site_shots" / "pc-main.png"),
|
|
"/root/lanremote-relay/site_shots/pc-main.png", 0o644)
|
|
print(" 截图已上传")
|
|
upload(str(LANDING), "/root/lanremote-relay/landing.py", 0o644)
|
|
upload(str(ROOT / "relay" / "showcase.py"),
|
|
"/root/lanremote-relay/showcase.py", 0o644)
|
|
upload(str(RELAY), "/root/lanremote-relay/relay.py", 0o755)
|
|
print(" 代码已上传")
|
|
run("systemctl restart lanremote-relay", timeout=90)
|
|
out = run("sleep 6; curl -s -o /dev/null -w 'HTTP %{http_code} %{size_download}\\n' "
|
|
"http://127.0.0.1:8080/; "
|
|
"curl -s -o /dev/null -w '截图 HTTP %{http_code} %{size_download}\\n' "
|
|
"http://127.0.0.1:8080/shot/pc-main.png", timeout=120)[1]
|
|
print(" " + out.replace("\n", "\n "))
|
|
|
|
|
|
def main() -> int:
|
|
wire_landing()
|
|
wire_relay_route()
|
|
deploy()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|