静态资源交给 Cloudflare 边缘缓存(下载提速 41 倍)

实测基线:
  ykxmz-setup-1.8.1.exe   只下到 237568/15067199 字节就停(17 KB/s)
  ykxmz-watch-1.8.1.apk   201523 字节 / 66.8 秒 = 3 KB/s
  ykxmz-server-1.8.1.zip  214900 字节 / 3.1 秒  = 67 KB/s

原因:relay 给所有响应都发 Cache-Control: no-store,
Cloudflare 完全不缓存 —— 每次下载/每张图都要穿隧道回 4G 源站。

改动:
  /download/*   no-store -> public, max-age=604800, immutable
                文件名带版本号,同一名字内容永不改变,可以长缓存
  /             -> public, max-age=60(挡住刷新重复回源)
  /favicon.ico  已是 max-age=86400,保持不变
  /shot/*       已是 max-age=86400,保持不变
  /v1/*         保持 no-store(接口必须实时)

顺带修两个自己踩的坑:

1. _send() 无条件先发 no-store,再追加 extra 里的 Cache-Control,
   结果是**两个 Cache-Control 头**,客户端只认第一个 ——
   调用方传的缓存策略被吃掉。改成 extra 里有就不再发默认值。

2. do_HEAD 和 do_GET 的缓存头不一致:HEAD 对非 /shot/ 一律报 no-store。
   下载器/代理用 HEAD 探测时会误判文件不可缓存。
   现在 _head_info 给每个路由带上和 GET 完全相同的缓存头,
   do_HEAD 也不再盲目覆盖。

结果(同一个测试口径):
  setup.exe  17 KB/s(截断) -> 695 KB/s 完整 15 MB   41 倍
  watch.apk   3 KB/s         -> 101 KB/s              33 倍
  server.zip 67 KB/s         ->  87 KB/s
  三个文件全部 cf-cache-status=HIT(不再穿隧道回源)

排查过程记一笔:我一开始用 curl -I 验证,
看到的全是 no-store —— 因为 **-I 发的是 HEAD**,
而 HEAD 走的是另一个分支。查了半天才发现是验证方法错了。
This commit is contained in:
xmz7891
2026-10-07 15:44:48 +08:00
parent 46196c3618
commit bf15a2ee61
203 changed files with 1789 additions and 40 deletions
+103
View File
@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-
r'''把当前版本的三个成品上传到中转的下载目录。
用法:
python _build/upload_release.py 1.8.1
会自动:
* 本地确认三个成品都在(安装包 exe / 服务端 zip / 手表 apk)
* 删掉设备上的旧版本文件
* 上传新的四个文件(apk 还带一份说明)
* 确认中转的 /v1/downloads 能列出来
发布规则(用户定的):
* Windows 成品必须是**安装包 exe**,不是 agent zip
* 服务端给 zip,手表直接给 APK
* **每次都升版号**(用户原话:自行更新版号)
'''
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
sys.path.insert(0, str(ROOT / "_build"))
from devssh import run, upload # noqa: E402
REMOTE_DIR = "/root/lanremote-relay/downloads"
def main() -> int:
version = sys.argv[1] if len(sys.argv) > 1 else "1.8.1"
files = [
("dist/ykxmz-setup-%s.exe" % version,
"ykxmz-setup-%s.exe" % version),
("dist/ykxmz-server-%s.zip" % version,
"ykxmz-server-%s.zip" % version),
("dist/ykxmz-watch-%s.apk" % version,
"ykxmz-watch-%s.apk" % version),
("dist/ykxmz-watch-%s-说明.txt" % version,
"ykxmz-watch-%s-说明.txt" % version),
]
print(" 版本 %s" % version)
print()
print(" 本地成品:")
missing = []
for local, _remote in files:
path = ROOT / local
if path.is_file():
print(" ✓ %-38s %7.2f MB"
% (path.name, path.stat().st_size / 1024 / 1024))
else:
print(" ✗ %-38s 不存在" % local)
missing.append(local)
if missing:
print()
print(" [失败] 少了 %d 个,先构建" % len(missing))
return 1
# 清掉设备上所有旧版本(任何不是当前版本的 ykxmz-*)
print()
print(" 清理设备上的旧版本:")
_c, out, _e = run("ls %s/ 2>/dev/null" % REMOTE_DIR, timeout=60)
for name in (out or "").strip().splitlines():
name = name.strip()
if not name or version in name:
continue
if not name.startswith("ykxmz-"):
continue
run("rm -f '%s/%s'" % (REMOTE_DIR, name), timeout=30)
print(" - 删掉 %s" % name)
print()
print(" 上传:")
for local, remote_name in files:
path = ROOT / local
upload(str(path), "%s/%s" % (REMOTE_DIR, remote_name), 0o644)
print(" ✓ %s" % remote_name)
print()
print(" 设备上的下载目录:")
_c, out, _e = run("ls -la %s/" % REMOTE_DIR, timeout=60)
for line in (out or "").strip().splitlines():
print(" " + line)
print()
print(" 中转 /v1/downloads:")
_c, out, _e = run(
"curl -s --max-time 10 http://127.0.0.1:8080/v1/downloads", timeout=60)
print(" " + (out or "").strip()[:500])
print()
print(" 官网下载按钮会指向:")
for _local, remote_name in files:
if remote_name.endswith((".exe", ".zip", ".apk")):
print(" /download/%s" % remote_name)
return 0
if __name__ == "__main__":
sys.exit(main())