ykxmz 1.5.0锛氫慨澶嶆墜琛ㄧ TLS 璇佷功鏍¢獙 + MTU 淇缃戠粶绋冲畾鎬?

琚帶绔?- 淇 MTU 闂锛氬す MSS + 璺緞 MTU 鎺㈡祴锛堝叕缃戝彲鐢ㄧ巼 48% -> 100%锛?- 澶嶇敤 HTTP 杩炴帴锛堝厤鎺夋瘡娆℃柊寤?TLS 鐨?30% 鎻℃墜澶辫触锛?- 姣忓垎閽熷洖澶存壘灞€鍩熺綉锛岃嚜鍔ㄥ垏鍥炲揩 400 鍊嶇殑璺緞
- 鐢婚潰蹇冭烦甯э細闈欐鏃剁渷娴侀噺锛屽鎴风浠嶇煡閬撴祦鏄椿鐨?- 閿佸睆鏃剁粰鍑哄彲鎿嶄綔鐨勬彁绀?
鎵嬭〃绔?- 鍐呯疆 GTS Root R4 / GlobalSign Root CA锛屼慨 SSLHandshakeException
- 杩炴帴閿欒缈昏瘧鎴愪汉璇濓紙璇佷功/鏃堕棿/瑙f瀽/瓒呮椂鍒嗗埆鎻愮ず锛?- 缁忎腑杞繛涓婂悗鐢ㄦ湇鍔″櫒鎶ョ殑鍦板潃鍒锋柊宸查厤瀵硅褰?- 鏂板灞€鍩熺綉杩炴帴椤甸潰銆佹湇鍔″櫒寤惰繜妫€娴?
鏈嶅姟鍣ㄧ
- 鏀寔 chunked 璇锋眰浣擄紙淇瘡 16 绉掍竴娆$殑杩炴帴閿欎贡涓庡ぇ闈㈢Н 502锛?- 缃戠粶璋冧紭鑴氭湰 net-tune.sh锛堝紑鏈鸿嚜鍔ㄥ簲鐢級
- 绠$悊椤碉細鎵归噺鍒犻櫎銆佺綉缁滀綋妫€銆佷笅杞藉尯
- 闅ч亾鏀?HTTP/2 + 鍙岃繛鎺?
鏂囨。
- 璁稿彲鍗忚 / 瀹夎鍓嶅繀璇?/ 瀹夎鍚庨』鐭ワ紙浣滆€咃細灏忛害瀛愶級
This commit is contained in:
xmz7891
2026-10-06 19:01:05 +08:00
commit b705216e4a
3195 changed files with 203292 additions and 0 deletions
+286
View File
@@ -0,0 +1,286 @@
# -*- coding: utf-8 -*-
"""验证中转管理器的「删除设备(连同配对码)」。
用户要求:
"这个中转管理器,首先删除设备,就是删除设备并删除对应的配对码"
要验证:
1. 删设备 -> 设备从列表消失
2. 删设备 -> 配对码绑定也清了(别的设备能用这个码了)
3. 删设备 -> 它的会话被踢掉
4. 按 code 删也行
5. delete_all 能一次清光
6. 管理页上有删除按钮
7. 删不存在的设备要报错,不能崩
"""
from __future__ import annotations
import json
import os
import shutil
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.request
from pathlib import Path
_UA = {"User-Agent": "LanRemote/1.4.0"}
ROOT = Path(__file__).resolve().parent.parent
RELAY_PORT = 8094
BASE = f"http://127.0.0.1:{RELAY_PORT}"
passed = 0
failed = 0
def check(name, ok, detail=""):
global passed, failed
if ok:
passed += 1
print(" [PASS] " + name)
else:
failed += 1
print(" [FAIL] " + name + " " + str(detail))
def post(path, payload=None, headers=None):
body = json.dumps(payload or {}, ensure_ascii=False).encode("utf-8")
hdrs = dict(_UA)
hdrs["Content-Type"] = "application/json"
hdrs.update(headers or {})
req = urllib.request.Request(BASE + path, data=body, method="POST",
headers=hdrs)
try:
with urllib.request.urlopen(req, timeout=15) as resp:
return resp.status, json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as exc:
raw = exc.read().decode("utf-8", "replace")
try:
return exc.code, json.loads(raw)
except Exception: # noqa: BLE001
return exc.code, {"raw": raw[:120]}
def get(path, headers=None):
hdrs = dict(_UA)
hdrs.update(headers or {})
req = urllib.request.Request(BASE + path, headers=hdrs)
try:
with urllib.request.urlopen(req, timeout=15) as resp:
return resp.status, resp.read().decode("utf-8", "replace")
except urllib.error.HTTPError as exc:
return exc.code, exc.read().decode("utf-8", "replace")
def main() -> int:
work = Path(tempfile.mkdtemp(prefix="admin_delete_"))
relay = None
try:
relay_data = work / "relaydata"
relay_data.mkdir(parents=True, exist_ok=True)
env = dict(os.environ)
env.update({
"RELAY_PORT": str(RELAY_PORT), "RELAY_BIND": "127.0.0.1",
"RELAY_DATA": str(relay_data), "RELAY_POLL": "6",
"PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1",
})
relay = subprocess.Popen(
[sys.executable, "-u", str(ROOT / "relay" / "relay.py")], env=env,
stdout=open(work / "relay.log", "wb"),
stderr=subprocess.STDOUT)
up = False
for _ in range(40):
time.sleep(0.5)
try:
status, _ = get("/v1/health")
if status == 200:
up = True
break
except Exception: # noqa: BLE001
continue
check("本地中转起来了", up)
if not up:
print((work / "relay.log").read_text(encoding="utf-8",
errors="replace")[-500:])
return 1
admin_key = (relay_data / "admin_key.txt").read_text().strip()
admin = {"X-Admin": admin_key}
print()
print("一、准备:登记两台设备")
post("/v1/agent/register", {"deviceId": "pc-A", "code": "111111",
"name": "电脑A", "version": "1.4.0"})
post("/v1/agent/register", {"deviceId": "pc-B", "code": "222222",
"name": "电脑B", "version": "1.4.0"})
status, body = get("/v1/health")
devices = json.loads(body).get("devices", [])
check("两台设备都登记上了", len(devices) == 2, len(devices))
print()
print("二、管理页上有删除按钮吗")
status, html = get("/", headers=admin)
check("管理页能打开", status == 200, status)
# class 名改了(del -> "btn del"),改成按行为判定:
# 有带 data-device 的按钮、且文字是"删除"。
check("有「删除」按钮",
"data-device" in html and "删除" in html,
"找不到删除按钮")
check("有「删除全部设备」按钮", "delAll" in html, "没找到 delAll")
check("删除前的确认提示里说明了会清配对码",
"配对码绑定" in html or "清掉它的配对码" in html)
print()
print("三、按 deviceId 删一台")
status, result = post("/v1/admin/delete", {"deviceId": "pc-A"},
headers=admin)
check("删除返回 ok", result.get("ok") is True, result)
check("删的是 A", result.get("name") == "电脑A", result.get("name"))
check("它占了 1 条码绑定",
len(result.get("codesCleared") or []) == 1,
result.get("codesCleared"))
status, body = get("/v1/health")
devices = json.loads(body).get("devices", [])
check("A 从设备列表消失了", len(devices) == 1, len(devices))
check("剩下的是 B", devices and devices[0]["name"] == "电脑B",
devices)
print()
print("四、A 的配对码要能被别的设备用了")
status, again = post("/v1/agent/register", {
"deviceId": "pc-C", "code": "111111", "name": "电脑C",
"version": "1.4.0"})
check("新设备可以用刚释放的 111111",
again.get("ok") is True, again)
check("拿到的就是 111111", again.get("code") == "111111",
again.get("code"))
print()
print("五、按 code 删")
status, result = post("/v1/admin/delete", {"code": "222222"},
headers=admin)
check("按码删也能删掉", result.get("ok") is True, result)
check("删的是 B", result.get("name") == "电脑B", result.get("name"))
print()
print("六、删不存在的设备要报错,不能崩")
status, result = post("/v1/admin/delete", {"deviceId": "不存在"},
headers=admin)
check("返回 404", status == 404, status)
check("带了错误说明", bool(result.get("error")), result)
status, result = post("/v1/admin/delete", {}, headers=admin)
check("什么都不给返回 400", status == 400, status)
print()
print("七、delete_all 一次清光")
status, result = post("/v1/admin/delete_all", headers=admin)
check("delete_all 返回 ok", result.get("ok") is True, result)
status, body = get("/v1/health")
devices = json.loads(body).get("devices", [])
check("设备列表空了", len(devices) == 0, len(devices))
status, again = post("/v1/agent/register", {
"deviceId": "pc-D", "code": "111111", "name": "电脑D",
"version": "1.4.0"})
check("清光之后 111111 也可以用了",
again.get("ok") is True, again)
print()
print("八、删除后状态要落盘(重启不复活)")
post("/v1/agent/register", {"deviceId": "pc-E", "code": "333333",
"name": "电脑E", "version": "1.4.0"})
post("/v1/admin/delete", {"deviceId": "pc-E"}, headers=admin)
state_file = relay_data / "relay_state.json"
state = json.loads(state_file.read_text(encoding="utf-8"))
codes = [item.get("code") for item in state.get("codes", [])]
check("落盘的状态里没有 333333", "333333" not in codes, codes)
print()
print("九、孤儿绑定也要能清掉")
# 造一个"设备下线了但码还占着"的状态:
# 登记一台设备,然后直接把它从 devices 里摘掉(模拟超时清理),
# 但保留 codes 里的绑定。
post("/v1/agent/register", {"deviceId": "pc-orphan",
"code": "778899", "name": "孤儿",
"version": "1.4.0"})
state_file = relay_data / "relay_state.json"
before = json.loads(state_file.read_text(encoding="utf-8"))
codes_before = [i.get("code") for i in before.get("codes", [])]
check("778899 已经有绑定了", "778899" in codes_before, codes_before)
# 制造孤儿:把设备记录摘掉,只留绑定
import subprocess as _sp
_sp.run([sys.executable, "-c", f"""
import json
p = {str(state_file)!r}
d = json.load(open(p))
# 保留 778899,其余清掉,模拟"只剩绑定没有设备"
d["codes"] = [c for c in d.get("codes", []) if c.get("code") == "778899"]
json.dump(d, open(p, "w"))
"""], check=False)
# 重启中转,让它从盘上加载这个孤儿状态
relay.terminate()
relay.wait(timeout=8)
relay = subprocess.Popen(
[sys.executable, "-u", str(ROOT / "relay" / "relay.py")], env=env,
stdout=open(work / "relay2.log", "wb"),
stderr=subprocess.STDOUT)
for _ in range(30):
time.sleep(0.5)
try:
if get("/v1/health")[0] == 200:
break
except Exception: # noqa: BLE001
continue
status, body = get("/v1/health")
devices = json.loads(body).get("devices", [])
check("设备列表是空的(只剩孤儿绑定)", len(devices) == 0, len(devices))
status, again = post("/v1/agent/register", {
"deviceId": "pc-new", "code": "778899", "name": "新设备",
"version": "1.4.0"})
check("孤儿绑定确实挡着这个码(复现了用户的问题)",
again.get("ok") is False, again)
admin_key = (relay_data / "admin_key.txt").read_text().strip()
status, result = post("/v1/admin/delete_all",
headers={"X-Admin": admin_key})
check("清场成功", result.get("ok") is True, result)
check("报告清掉了孤儿绑定",
result.get("orphansCleared", 0) >= 1,
result.get("orphansCleared"))
status, body = get("/v1/health")
state = json.loads(state_file.read_text(encoding="utf-8"))
check("落盘里没有残留绑定了",
len(state.get("codes", [])) == 0, state.get("codes"))
status, again = post("/v1/agent/register", {
"deviceId": "pc-final", "code": "778899", "name": "终于能用了",
"version": "1.4.0"})
check("清场之后这个码能用了", again.get("ok") is True, again)
print()
print("=" * 62)
print(f"通过 {passed} 项,失败 {failed} 项")
return 0 if failed == 0 else 1
finally:
if relay is not None:
try:
relay.terminate()
relay.wait(timeout=8)
except Exception: # noqa: BLE001
try:
relay.kill()
except Exception: # noqa: BLE001
pass
time.sleep(0.5)
shutil.rmtree(work, ignore_errors=True)
if __name__ == "__main__":
raise SystemExit(main())