Files
jianli-boss/client/debug_cdp_lite.py
2026-08-25 22:53:57 +08:00

66 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
验证低 CDP 痕迹方案:
1. /json/new 创建标签页并导航(浏览器自身导航,无自动化 attach 痕迹)
2. 纯 CDP WebSocket 的 Network.getCookies 读取登录 Cookie(不激活 Page/Runtime)
用法:python -m client.debug_cdp_lite
"""
import json
import time
import requests
import websocket
CDP = "http://127.0.0.1:9222"
RESUME_URL = "https://www.zhipin.com/web/geek/resume"
def main():
# 0. 确保浏览器在运行(未运行则拉起,profile 持久化登录态)
try:
requests.get(f"{CDP}/json/version", timeout=3)
except Exception:
from .browser.manager import BrowserManager
BrowserManager().start_browser()
time.sleep(2)
# 1. 用 DevTools HTTP 协议新开标签页(自动加载目标 URL)
r = requests.put(f"{CDP}/json/new?{RESUME_URL}", timeout=10)
if r.status_code == 405: # 部分 Chrome 版本只接受 GET
r = requests.get(f"{CDP}/json/new?{RESUME_URL}", timeout=10)
tab = r.json()
ws_url = tab["webSocketDebuggerUrl"]
tab_id = tab["id"]
print(f"[1] 新标签页已创建:{tab_id}", flush=True)
# 2. 等 SPA 加载,轮询标签页元信息观察 URL 是否被风控重置为 about:blank
for i in range(10):
time.sleep(3)
tabs = {t["id"]: t for t in requests.get(f"{CDP}/json", timeout=5).json()}
t = tabs.get(tab_id)
if t is None:
print(f"[2 {(i + 1) * 3}s] 标签页已消失", flush=True)
return
print(f"[2 {(i + 1) * 3}s] URL={t['url']} title={t.get('title')!r}", flush=True)
# 3. 纯 CDP WebSocket 读 Cookie(只发 Network.getCookies,不激活任何调试域)
ws = websocket.create_connection(ws_url, timeout=10)
ws.send(json.dumps({"id": 1, "method": "Network.getCookies",
"params": {"urls": ["https://www.zhipin.com/"]}}))
while True:
msg = json.loads(ws.recv())
if msg.get("id") == 1:
names = {c["name"] for c in msg["result"]["cookies"]}
print(f"[3] Cookie 数量:{len(names)},含 wt2:{'wt2' in names},"
f"含 __zp_stoken__:{'__zp_stoken__' in names}", flush=True)
break
ws.close()
# 4. 关掉调试标签页
requests.get(f"{CDP}/json/close/{tab_id}", timeout=5)
print("[4] 调试标签页已关闭", flush=True)
if __name__ == "__main__":
main()