AnyBot/docs/restructure/notes/b03d-bidi-web.md

45 lines
5.0 KiB
Markdown
Raw 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.

# b03d:bidi.rs(393 行)+ web.rs(867 行)
## bidi.rs(BiDi 双向 WebDriver 协议,Firefox 用)
**常量**:WAIT_CONNECT_SECS=30、WAIT_RESPONSE_SECS=20、MAX_EVAL_CHARS=6000、MAX_TEXT_CHARS=12000、MAX_PAGE_CHARS=12000、MAX_MSG_BYTES=64MB(WebSocket 封框上限)。
**结构**:`Bidi`(port + ws Mutex,Send+Sync)/ `Session{ws,id}` / `Node{ref,x,y,w,h,tag,text,scroll,xpath,hidden,name,role,value,secret}` / `PageText{url,title,body}` / `InputEvent` / `Eval{type_,result}`。
- **send 锁内 next_id++ + recv 匹配 id**(同请求并发会错配 — 注释记载的真实踩坑)。`ws_call` 发 JSON-RSD、跳掉通知、`{"error":...}` → Err。
- `start`:连 /session/websocket + Subscribe(navigate, command result) → 拿实页真实 url 才返回(旧版回得太快 — 记在注释)。
- `navigate`:发完等 `browsingContext.navigate` 订阅通知落定。
- `evaluate`(sync 版;async evaluate 在 browser.rs):字串结果截 6000。
- **元素收集 JS**(ELEMENTS_JS,跟 CDP 版同一支):shadow DOM 穿透(id 接父)、data-anybot-ref 编号、13 类控件、表单容器 name(fieldset/legend/label/table)、secret 侦测(password/cc-number/cvc/13–19 位)、「要按才现身」的菜单钮(overflow:auto + 祖先 clip 高度 < 2 行)。
- `elements`:跑 JS → Node 列表;`nodes_json`(tab 快照存档格式)。
- `page_text`:url/title/body≤12000;`input_events`(键盘输入现场)。
- `dispatch`:Pointer/keyboard 原生事件(比 CDP Input.dispatch 像真鼠标:hover→pointerDown→Up→Click,坐标含 scrollX/Y)。
- `insert_input`:Input.insertText + input/change 事件冒泡(React 认得)。
- `scroll`:window.scrollBy → 等 350ms → scrollX/scrollY。
- `tab_snapshot`:nodes+url+title。`open_tab`(create + 订阅 + 等实 url)。
**测试锁**(tests):`url_path_and_bidi_payloads_are_shape`、`element_script_finds_the_controls_the_model_can_see`(真 Firefox headless --bidi-websocket)、`native_pointer_events_drag_like_a_real_mouse`(拖曳=原生 pointermove 连按;CDP 模拟被站点拒)、`page_text_is_a_web_page_not_a_file`。
## web.rs(网址/抓取/搜索/防打叉)
**常量**:UA(Chrome 124 Mac 字串,全域共用)、SEARCH_TIMEOUT_MS=25000、READ_MAX_BYTES=8MB、BROWSER_TIMEOUT_SECS=12、LOCAL_TIMEOUT_SECS=5、MAX_FETCH_CHARS=30000。
- **normalize_url**:无 scheme 补 https://;www. 前缀补;裸域名 → `web_search:{domain}`;「http://1」→ `web_search:1`。
- **is_local_target**:localhost/*.local/127.x/10.x/172.16-31.x/192.168.x/IPv6 ::1/fc/fd → shell 档走 localhost、web_fetch 可抓。
- `is_own_listener`:本機 httpd 的端口(含手机 relay 的 :41000)→ 禁止自抓。
- **`is_forbidden_browser_url`(全域安全闸, computer/browser/mcp/web 全用它)**:AWS/GCP/Azure/阿里/腾讯云云端元数据 IP(169.254.169.254、100.100.100.200、metadata.google.internal、150.171.27.x Azure)+ Docker 内部管理端点(tcp://、npipe://、localhost/linux 的 /containers|/images|/exec|/networks|…路径、*.docker.sock)→ 拒。
- `forbidden_browser_url_reason`:给使用者的中文理由(云端帐密/本机 Docker 控制站)。
- `fetch_page`:ureq + 转码(charset 头/HTML meta,非 UTF-8 用 encoding_rs)→ 状态码 → 文字/表格/JSON;>8MB 拒;非文本拒。`bytes` 抓二进制(上限 8MB)。
- `http_status_ok`:2xx/3xx 算成功(HEAD 探)。`http_request`:自寫 http 动词(含 body≤1MB、redirects≤3 手跟、Location 相对补全)。
- **html_to_text**:HTML→文字(跳 script/style、块级断行、表格→`│` 栏、entity 解码)。`is_probably_html` 门槛。
- **防打叉网**(AntiBlock):`anti_challenge`(403/429/401 + challenge 字样 + 挑战页特征);`anti_page`(200 但 body 有 CF 挑战标志:cf-turnstile/challenge-platform/just a moment…);`anti_note` → 指路开真浏览器(Bot 自己的,人手接管)。
- **web_search(双路)**:DuckDuckGo HTML 版解析(`result__a`/`result__snippet`/`result__url`;vqd token 续搜;DDG 403/挑战 → 转 Lite 版;DDG 全挂 → 转 Bing)。Bing:`bing_links`(li.b_algo,sid 续搜)、繁转简(opencc t2s)、区域 zh-TW。UA 遮 Chrome。
- `search_summary`:标题+摘要+来源域名(上限 12 条)。
- `read_url`:fetch→html_to_text→30000 字截。
- `is_loopback`:127.x/::1。
- `user_agent`:UA 常数。
**测试锁**(tests,23 条):normalize/local/forbidden 闸(元数据 Docker 全表)、html_to_text 表格、`web_search_finds_the_openai_api`、`web_search_survives_the_duckduckgo_challenge`(真网 DDG→Lite→Bing 链)、`web_search_finds_taiwan_in_traditional_chinese`(繁简)、bing_links、anti_challenge/anti_page、fetch 真页、`forbidden_reasons_say_why_in_words`。
**迁移**:bidi.rs→browser crate。web.rs→net crate(is_forbidden_browser_url 是全域闸→放 **core::net_rules**,computer/browser/mcp/web 四家用它)。