Files
ykxmz/_build/patch_test_ua.py
T
xmz7891 b705216e4a 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 + 鍙岃繛鎺?
鏂囨。
- 璁稿彲鍗忚 / 瀹夎鍓嶅繀璇?/ 瀹夎鍚庨』鐭ワ紙浣滆€咃細灏忛害瀛愶級
2026-10-06 19:01:05 +08:00

120 lines
3.4 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 -*-
"""给测试脚本也加上 User-Agent。
背景:被控端代码已经带了 LanRemote/1.4.0(Cloudflare 的
Browser Integrity Check 会拦 Python 默认 UA,返回 403 + error 1010)。
但测试脚本自己也是用 urllib 发请求的,所以经固定域名跑就全 403。
做法:给每个测试脚本在最前面加一个 _UA 常量,
然后把所有 urllib.request.Request(...) 和 urlopen(...) 都带上它。
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
TESTS = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_tests")
FILES = [
"test_relay_e2e.py",
"test_relay_local.py",
"test_relay_admin_login.py",
]
UA_BLOCK = '''
# Cloudflare 的 Browser Integrity Check 会拦 Python 默认 UA
# (返回 403 + error code 1010),所以请求都要带上这个。
_UA = {"User-Agent": "LanRemote/1.4.0"}
'''
# 给 Request(...) 加 headers
REQ_PATTERNS = [
# Request(url, data=body, method="POST")
(re.compile(r"urllib\.request\.Request\(([^()]*?),\s*(data=[^()]*?),\s*"
r"(method=\"POST\")\)"),
r'urllib.request.Request(\1, \2, \3, headers=_UA)'),
# Request(url)
(re.compile(r"urllib\.request\.Request\(([^()]*?)\)"),
r'urllib.request.Request(\1, headers=_UA)'),
]
def patch_file(path: Path) -> int:
text = path.read_text(encoding="utf-8")
if "_UA" in text:
print(f" {path.name}: 已经加过")
return 0
changes = 0
# 1. 每个 Request( 都加 headers=_UA
# 先处理带 method= 的(更长的模式),再处理简单的
def add_headers(match):
nonlocal changes
inner = match.group(1)
if "headers=" in inner:
return match.group(0)
changes += 1
return f"urllib.request.Request({inner}, headers=_UA)"
# 匹配 Request( ... ) 的一层括号内容
text = re.sub(
r"urllib\.request\.Request\(((?:[^()]|\([^()]*\))*?)\)(?!\s*\))",
add_headers, text)
# 2. 裸 urlopen(URL + path, ...) 补 headers
def add_urlopen_headers(match):
nonlocal changes
inner = match.group(1)
if "headers=" in inner or "_UA" in inner:
return match.group(0)
changes += 1
return f"urllib.request.urlopen({inner}, headers=_UA)"
text = re.sub(
r"urllib\.request\.urlopen\(((?:[^()]|\([^()]*\))*?)\)(?!\s*\))",
add_urlopen_headers, text)
# 3. 插入 _UA 定义(放在 import 之后)
lines = text.splitlines(keepends=True)
insert_at = 0
for index, line in enumerate(lines):
if line.startswith(("import ", "from ")):
insert_at = index + 1
lines.insert(insert_at, UA_BLOCK)
text = "".join(lines)
path.write_text(text, encoding="utf-8", newline="\n")
import ast
try:
ast.parse(text)
except SyntaxError as exc:
print(f" {path.name}: 语法错误 {exc}")
return -1
print(f" {path.name}: 改了 {changes} 处")
return changes
def main() -> int:
total = 0
ok = True
for name in FILES:
path = TESTS / name
if not path.is_file():
print(f" [跳过] {name} 不存在")
continue
result = patch_file(path)
if result < 0:
ok = False
else:
total += result
print(f" 合计 {total} 处")
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())