diff --git a/browser_login/login.py b/browser_login/login.py index 7cd3f16..ea904e1 100644 --- a/browser_login/login.py +++ b/browser_login/login.py @@ -54,28 +54,33 @@ def patch_drission_ws_handshake() -> None: def resilient_create_connection(address, **kwargs): base_kwargs = dict(kwargs) no_proxy_hosts = ["127.0.0.1", "localhost", "::1"] + # Chrome DevTools 会进行 Host 校验防 DNS Rebinding,我们强行补上 Host 和 Origin candidate_kwargs = [ - {**base_kwargs, "http_no_proxy": no_proxy_hosts}, - {**base_kwargs, "http_no_proxy": no_proxy_hosts, "suppress_origin": False}, - {**{k: v for k, v in base_kwargs.items() if k != "suppress_origin"}, "http_no_proxy": no_proxy_hosts}, { - **{k: v for k, v in base_kwargs.items() if k != "suppress_origin"}, + **base_kwargs, "http_no_proxy": no_proxy_hosts, - "origin": "http://127.0.0.1", + "header": ["Host: 127.0.0.1", "Origin: http://127.0.0.1"] }, + { + **base_kwargs, + "http_no_proxy": no_proxy_hosts, + "suppress_origin": False, + "header": ["Host: localhost"] + }, + {**base_kwargs, "http_no_proxy": no_proxy_hosts}, ] last_err = None for candidate in candidate_kwargs: try: return raw_ws_create_connection( - address, + address.replace("127.0.0.1", "localhost"), # 部分 Chrome 只认 localhost http_proxy_host=None, http_proxy_port=None, **candidate, ) except WebSocketBadStatusException as ws_err: last_err = ws_err - if "Handshake status 404 Not Found" not in str(ws_err): + if "Handshake status 404" not in str(ws_err) and "Handshake status 403" not in str(ws_err): raise except Exception as other_err: last_err = other_err