# -*- coding: utf-8 -*- """区分「代理导致的失败」和「真失败」。 用户开了代理之后,Windows 这边的测试客户端会受影响: WinError 10061 由于目标计算机积极拒绝,无法连接 这是本机代理拒了连接,不是隧道的问题。 这个脚本: 1. 先看系统代理设置 2. 绕过代理(proxy=None)直连隧道 3. 对比走代理 / 不走代理的结果 """ from __future__ import annotations import os import socket import ssl import sys import urllib.error import urllib.request URL = "https://relay.xxmxxs.top/v1/health" UA = {"User-Agent": "LanRemote/1.4.0"} def show_proxy_settings() -> None: print(" 系统代理相关环境变量:") found = False for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy", "NO_PROXY"): value = os.environ.get(name) if value: found = True print(f" {name} = {value}") if not found: print(" (没有代理环境变量)") # Windows 的 IE/系统代理设置(注册表) try: import winreg key = winreg.OpenKey( winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings") enable, _ = winreg.QueryValueEx(key, "ProxyEnable") try: server, _ = winreg.QueryValueEx(key, "ProxyServer") except FileNotFoundError: server = "(没设)" print(f" Windows 系统代理: {'开' if enable else '关'} {server}") except Exception: # noqa: BLE001 print(" (读不到 Windows 代理设置)") def probe(label: str, opener) -> tuple: try: with opener() as resp: return resp.status, resp.read(60).decode("utf-8", "replace") except urllib.error.HTTPError as exc: return exc.code, exc.read(60).decode("utf-8", "replace") except Exception as exc: # noqa: BLE001 return 0, f"{type(exc).__name__}: {exc}" def direct_opener(): """完全绕过代理。""" handler = urllib.request.ProxyHandler({}) return urllib.request.build_opener(handler).open( urllib.request.Request(URL, headers=UA), timeout=20) def default_opener(): """用系统默认(可能有代理)。""" return urllib.request.urlopen( urllib.request.Request(URL, headers=UA), timeout=20) def main() -> int: print("=" * 66) print(" 代理影响检查") print("=" * 66) print() print("一、当前代理设置") show_proxy_settings() print() print("二、绕过代理,直连隧道(每种 5 次)") direct_ok = 0 for i in range(1, 6): status, body = probe("direct", direct_opener) if status == 200: direct_ok += 1 print(f" {i}: HTTP {status} {body[:44]}") print(f" 直连成功 {direct_ok}/5") print() print("三、用默认设置(可能走代理,5 次)") default_ok = 0 for i in range(1, 6): status, body = probe("default", default_opener) if status == 200: default_ok += 1 print(f" {i}: HTTP {status} {body[:44]}") print(f" 走默认成功 {default_ok}/5") print() print("四、TCP 层直连(不经任何 HTTP 代理)") try: ctx = ssl.create_default_context() with socket.create_connection(("relay.xxmxxs.top", 443), timeout=15) as raw: with ctx.wrap_socket(raw, server_hostname="relay.xxmxxs.top"): print(" ✓ TLS 握手成功(说明域名和 443 端口没问题)") except Exception as exc: # noqa: BLE001 print(f" ✗ {type(exc).__name__}: {exc}") print() print("=" * 66) print(" 结论") print("=" * 66) if direct_ok > default_ok: print(f" 绕过代理更好({direct_ok}/5 vs {default_ok}/5)") print(" -> 你的代理在干扰。测试和程序都应该绕开它访问中转。") elif direct_ok == default_ok == 5: print(" 两种都通 —— 代理没影响") elif direct_ok == 0: print(" 直连也不通 —— 是隧道/线路的问题,不是代理") else: print(f" 直连 {direct_ok}/5,默认 {default_ok}/5") return 0 if __name__ == "__main__": raise SystemExit(main())