From 0c4a0a20833a15b1588a7bb3541bf22cd72188a5 Mon Sep 17 00:00:00 2001 From: hjq <770690987@qq.com> Date: Tue, 23 Jun 2026 17:33:12 +0800 Subject: [PATCH] =?UTF-8?q?BOM=E5=8F=91=E6=96=99=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser_login/login.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/browser_login/login.py b/browser_login/login.py index ab53067..9f3478e 100644 --- a/browser_login/login.py +++ b/browser_login/login.py @@ -45,39 +45,50 @@ def is_linux_env() -> bool: def patch_drission_ws_handshake() -> None: """ 为 Linux 环境下的 DrissionPage WebSocket 握手增加兼容性降级。 - 某些 Chrome/Ubuntu 组合下,`suppress_origin=True` 会导致 DevTools - 返回 404,因此这里按多种握手参数顺序尝试。 """ if getattr(dp_driver_module, "_DTSK_WS_PATCHED", False): return def resilient_create_connection(address, **kwargs): + # 终极暴力破解法:移除所有可能导致 Chrome 安全校验失败的头 base_kwargs = dict(kwargs) + + # 强制禁用代理,防止请求被容器内的网络规则重定向 no_proxy_hosts = ["127.0.0.1", "localhost", "::1"] - # Chrome DevTools 会进行 Host 校验防 DNS Rebinding,我们强行补上 Host 和 Origin + base_kwargs["http_no_proxy"] = no_proxy_hosts + base_kwargs["http_proxy_host"] = None + base_kwargs["http_proxy_port"] = None + + # Chrome 149 增强了 DevTools 的安全校验。 + # suppress_origin=True 意味着不发送 Origin 头。 + # 我们按照不同的组合暴力重试 candidate_kwargs = [ + # 策略1:最原始、最干净的连接方式(类似 curl) { **base_kwargs, - "http_no_proxy": no_proxy_hosts, + "suppress_origin": True, + "header": [] + }, + # 策略2:显式声明是本地请求 + { + **base_kwargs, + "suppress_origin": False, "header": ["Host: 127.0.0.1", "Origin: http://127.0.0.1"] }, + # 策略3:伪装成 localhost { **base_kwargs, - "http_no_proxy": no_proxy_hosts, "suppress_origin": False, - "header": ["Host: localhost"] - }, - {**base_kwargs, "http_no_proxy": no_proxy_hosts}, + "header": ["Host: localhost", "Origin: http://localhost"] + } ] + last_err = None for candidate in candidate_kwargs: try: - return raw_ws_create_connection( - address.replace("127.0.0.1", "localhost"), # 部分 Chrome 只认 localhost - http_proxy_host=None, - http_proxy_port=None, - **candidate, - ) + # 强制使用 127.0.0.1,因为在 Docker 内 localhost 可能解析异常 + target_url = address.replace("localhost", "127.0.0.1") + return raw_ws_create_connection(target_url, **candidate) except WebSocketBadStatusException as ws_err: last_err = ws_err if "Handshake status 404" not in str(ws_err) and "Handshake status 403" not in str(ws_err):