#!/usr/bin/env python3 # -*- coding: utf-8 -*- """测 Cloudflare 放行哪些 User-Agent。 背景:固定域名上线后,/v1/health 用 PowerShell 访问是 200, 但 Python urllib(默认 UA = Python-urllib/3.11)拿到 403 + error code 1010。 1010 = Cloudflare 的 Browser Integrity Check 判定为可疑客户端。 被控端(Python)和手表(Java)都会因此连不上 —— 必须换一个能过的 UA。 """ import urllib.error import urllib.request URL = "https://relay.xxmxxs.top/v1/health" AGENTS = [ ("Python 默认(现在用的)", None), ("LanRemote/1.4.0", "LanRemote/1.4.0"), ("curl/8.0", "curl/8.0.1"), ("Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"), ("完整 Chrome UA", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"), ("Cloudflared", "cloudflared/2026.9.3"), ("空 UA", ""), ] def probe(name, agent): headers = {} if agent is not None: headers["User-Agent"] = agent request = urllib.request.Request(URL, headers=headers) try: with urllib.request.urlopen(request, timeout=20) as resp: body = resp.read(200).decode("utf-8", "replace") return resp.status, body except urllib.error.HTTPError as exc: return exc.code, exc.read(200).decode("utf-8", "replace") except Exception as exc: # noqa: BLE001 return 0, str(exc) def main() -> int: print(f" {URL}") print() print(f" {'User-Agent':<40}{'HTTP':>6} 响应") print(" " + "-" * 76) good = [] for name, agent in AGENTS: status, body = probe(name, agent) head = body.strip().replace("\n", " ")[:34] mark = "✓" if status == 200 else "✗" print(f" {mark} {name:<38}{status:>6} {head}") if status == 200: good.append(name) print() if good: print(f" 能过的:{', '.join(good)}") print() print(" 修法:被控端和手表发请求时带上其中一个 UA。") print(" 用自家标识(LanRemote/1.4.0)比伪装浏览器更合适 ——") print(" 但如果 Cloudflare 只放行浏览器 UA,那就只能用它。") else: print(" 全都过不去 —— 需要在 Cloudflare 面板上关掉 Browser Integrity Check") return 0 if __name__ == "__main__": raise SystemExit(main())