Files
ykxmz/windows/lanremote/autostart.py
T
xmz7891 bf15a2ee61 静态资源交给 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 走的是另一个分支。查了半天才发现是验证方法错了。
2026-10-07 15:44:48 +08:00

108 lines
3.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 -*-
"""开机自启(当前用户级,不需要管理员)。
写 HKCU\\...\\Run,不碰 HKLM —— 不需要 UAC,也不会影响别的用户。
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
RUN_KEY = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
# 任务管理器「启动」标签页里显示的就是这个名字。
# 用 ykxmz 而不是冒充 "Windows xxx" —— 用的人应该看得出这是什么。
VALUE_NAME = "ykxmz"
# 早期版本用过的名字。开着自启的老用户升级后要把它清掉,别留两条。
LEGACY_VALUE_NAMES = ("LanRemoteAgent",)
_NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0)
def executable_path() -> str:
"""要写进注册表的可执行文件路径。"""
if getattr(sys, "frozen", False):
return str(Path(sys.executable).resolve())
# 源码模式:用 pythonw + run_agent.py,这样不会有黑窗
script = Path(__file__).resolve().parent.parent / "run_agent.py"
pythonw = Path(sys.executable).with_name("pythonw.exe")
exe = pythonw if pythonw.is_file() else Path(sys.executable)
return f'"{exe}" "{script}"'
def _run(args: list[str]) -> tuple[int, str]:
try:
proc = subprocess.run(args, capture_output=True, timeout=20,
creationflags=_NO_WINDOW,
encoding="mbcs", errors="replace")
return proc.returncode, ((proc.stdout or "") + (proc.stderr or "")).strip()
except Exception as exc: # noqa: BLE001
return -1, str(exc)
def command_line() -> str:
"""注册表里存的完整命令行。"""
if getattr(sys, "frozen", False):
return f'"{Path(sys.executable).resolve()}" --silent'
script = Path(__file__).resolve().parent.parent / "run_agent.py"
pythonw = Path(sys.executable).with_name("pythonw.exe")
exe = pythonw if pythonw.is_file() else Path(sys.executable)
return f'"{exe}" "{script}" --silent'
def is_enabled() -> bool:
code, out = _run(["reg", "query", RUN_KEY, "/v", VALUE_NAME])
return code == 0 and VALUE_NAME in out
def current_value() -> str:
code, out = _run(["reg", "query", RUN_KEY, "/v", VALUE_NAME])
if code != 0:
return ""
for line in out.splitlines():
if VALUE_NAME in line and "REG_SZ" in line:
return line.split("REG_SZ", 1)[-1].strip()
return ""
def cleanup_legacy() -> list:
"""清掉早期版本留下的自启项(换名字了,别留两条)。"""
removed = []
for name in LEGACY_VALUE_NAMES:
code, _out = _run(["reg", "delete", RUN_KEY, "/v", name, "/f"])
if code == 0:
removed.append(name)
return removed
def enable() -> str:
value = command_line()
code, out = _run(["reg", "add", RUN_KEY, "/v", VALUE_NAME,
"/t", "REG_SZ", "/d", value, "/f"])
if code != 0:
raise ValueError(f"写入开机自启失败: {out[:120]}")
gone = cleanup_legacy()
extra = f"(顺手清掉旧自启项 {', '.join(gone)})" if gone else ""
return f"已开启开机自启(静默模式){extra}\n{value}"
def disable() -> str:
code, out = _run(["reg", "delete", RUN_KEY, "/v", VALUE_NAME, "/f"])
if code != 0 and "找不到" not in out and "unable" not in out.lower():
# 本来就没开,也算成功
pass
return "已关闭开机自启"
def toggle(on: bool) -> str:
return enable() if on else disable()
def status() -> dict:
enabled = is_enabled()
return {
"enabled": enabled,
"command": current_value() if enabled else "",
"expected": command_line(),
}