BOM发料对比

This commit is contained in:
hjq
2026-06-23 18:10:01 +08:00
parent 6d01d8b637
commit a412cabce7

View File

@@ -50,56 +50,34 @@ def patch_drission_ws_handshake() -> None:
return return
def resilient_create_connection(address, **kwargs): def resilient_create_connection(address, **kwargs):
# 终极暴力破解法:移除所有可能导致 Chrome 安全校验失败的头 kwargs = dict(kwargs)
base_kwargs = dict(kwargs)
# 强制禁用代理,防止请求被容器内的网络规则重定向 # 禁止 websocket-client 使用代理
no_proxy_hosts = ["127.0.0.1", "localhost", "::1"] kwargs["http_no_proxy"] = [
base_kwargs["http_no_proxy"] = no_proxy_hosts "127.0.0.1",
base_kwargs["http_proxy_host"] = None "localhost",
base_kwargs["http_proxy_port"] = None "::1"
# 提取目标端口,用于构造合法的 Host 头
try:
port = address.split(":")[2].split("/")[0]
except IndexError:
port = "9222"
# Chrome 149 增强了 DevTools 的安全校验。
# Host header 必须是 IP 地址或 localhost且必须包含端口号
candidate_kwargs = [
# 策略1最标准的 localhost 组合,带端口
{
**base_kwargs,
"suppress_origin": False,
"header": [f"Host: 127.0.0.1:{port}", "Origin: http://127.0.0.1"]
},
# 策略2最原始、最干净的连接方式类似 curl
{
**base_kwargs,
"suppress_origin": True,
"header": []
},
# 策略3伪装成 localhost 带端口
{
**base_kwargs,
"suppress_origin": False,
"header": [f"Host: localhost:{port}", "Origin: http://localhost"]
}
] ]
kwargs["http_proxy_host"] = None
kwargs["http_proxy_port"] = None
last_err = None # 删除 DrissionPage 可能传入的 header
for candidate in candidate_kwargs: kwargs.pop("header", None)
try: kwargs.pop("host", None)
# 强制使用 127.0.0.1,因为在 Docker 内 localhost 可能解析异常 kwargs.pop("origin", None)
target_url = address.replace("localhost", "127.0.0.1")
return raw_ws_create_connection(target_url, **candidate) target_url = address.replace(
except WebSocketBadStatusException as ws_err: "localhost",
last_err = ws_err "127.0.0.1"
except Exception as other_err: )
last_err = other_err
break log("INFO", f"[DEBUG] WS URL: {target_url}")
raise last_err log("INFO", f"[DEBUG] WS KWARGS: {kwargs}")
return raw_ws_create_connection(
target_url,
**kwargs
)
if is_linux_env(): if is_linux_env():
dp_driver_module.create_connection = resilient_create_connection dp_driver_module.create_connection = resilient_create_connection
@@ -276,7 +254,6 @@ def get_page(headless: bool = False, port: int = 9222) -> ChromiumPage:
co.set_argument("--disable-software-rasterizer") co.set_argument("--disable-software-rasterizer")
co.set_argument("--remote-allow-origins=*") co.set_argument("--remote-allow-origins=*")
co.set_argument("--remote-debugging-address=127.0.0.1") co.set_argument("--remote-debugging-address=127.0.0.1")
co.set_argument("--disable-web-security")
co.set_argument("--ignore-certificate-errors") co.set_argument("--ignore-certificate-errors")
co.set_argument("--proxy-server=direct://") co.set_argument("--proxy-server=direct://")
co.set_argument("--proxy-bypass-list=*") co.set_argument("--proxy-bypass-list=*")
@@ -477,5 +454,9 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
log("INFO", "用户中断") log("INFO", "用户中断")
finally: finally:
page.quit() try:
if page:
page.quit()
except Exception:
pass
log("INFO", "浏览器已关闭") log("INFO", "浏览器已关闭")