@jayyuen66/dsh-scrapling 0.0.1-rc → 0.0.1-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -151,9 +151,13 @@ displays normally. Use `scrapling_fetch` directly when you need `html` or a CSS
151
151
 
152
152
  ## Configuration
153
153
 
154
- Settings live under the **scrapling** card. Every field marked user-editable in
155
- [`host.ts`](./host.ts) is exposed; deployment-level fields are only settable through
156
- the `config:` block in [`cordis.patch.yml`](./cordis.patch.yml).
154
+ Settings live on the **scrapling** card in the plugin detail page (Settings → Plugins →
155
+ expand this bundle). Every field marked user-editable in [`host.ts`](./host.ts) appears
156
+ there, grouped by what it is for and with a one-line "what changes if you edit this";
157
+ deployment-level fields are only settable through the `config:` block in
158
+ [`cordis.patch.yml`](./cordis.patch.yml). Setting a field back to its default saves a
159
+ **clear-override**, not a write of the same value - so when the default changes later,
160
+ that field follows along.
157
161
 
158
162
  Notable switches:
159
163
 
@@ -170,6 +174,18 @@ Notable switches:
170
174
  ceiling over the model-supplied `maxPages`; an out-of-range `maxPages` (0, negative,
171
175
  NaN) is passed through unchanged so the sidecar reports the parameter error rather
172
176
  than this plugin silently clamping it into a legal-looking value.
177
+ - `stripInlineImages` - **on** by default: inline `data:` URI images are dropped from
178
+ the body and only their alt text is kept. Such an image is pure token noise - a
179
+ base64 SVG costs characters in the context while the model cannot read a shape out
180
+ of it. Pages with no inline images come back **byte-identical**; where there are some, the
181
+ saving depends entirely on how many - a site either inlines none or inlines a whole wall of
182
+ logos, and little in between. So turning this on is free for pages without inline images, and
183
+ the whole benefit lands when it matches.
184
+ `data:` URIs inside code fences are left untouched - there they are almost always
185
+ sample code a tutorial is teaching, and stripping would be editing the document.
186
+ Turn this off only when the raw base64 *is* the data (colours, fingerprints).
187
+ Stripping happens **before** the cap: the other way round is pointless, since those
188
+ tens of kilobytes have already eaten the `maxContentChars` budget.
173
189
  - `answerProvider` / `answerModel` / `answerMaxTokens` - the model `scrapling_answer`
174
190
  routes to and the per-call token ceiling. Provider and model left empty mean "follow
175
191
  the model the current session is already using"; `answerMaxTokens` caps the reply.
@@ -194,14 +210,16 @@ Notable switches:
194
210
  feeds both layers, so an explicitly allowed intranet target is not rejected by the
195
211
  host-side policy before the Python side ever sees it. Pointing it at an intranet
196
212
  address means giving up SSRF protection for those targets on purpose.
197
- - `allowSyntheticDns` - accept the synthetic DNS answers a proxy hands back. A fake-IP
198
- proxy resolves **every** domain into `198.18.0.0/15` (and TUN clients often
199
- `100.64.0.0/10`); neither is a public unicast range, so the gate would reject every
200
- URL and turning a proxy on would make the plugin stop working. **Off** by default —
201
- without a proxy you have no reason to weaken the check, and with one it should be your
202
- decision, not the model's. The relaxation applies to **resolved addresses only**: an IP
203
- literal written into the URL is still judged strictly, so `http://169.254.169.254/`
204
- stays blocked.
213
+ - `syntheticDnsRanges` - manual fallback for synthetic DNS: a comma-separated CIDR list
214
+ of your proxy's fake-IP pool. Detection is the main path and should handle it; this is
215
+ for the case where the resolver mixes synthetic and real answers. Invalid entries are
216
+ dropped rather than refusing to load - this is a fallback, and a broken one degrades to
217
+ "stay strict", not to a crash.
218
+ - `proxyUrl` - where fetches go out, e.g. `http://127.0.0.1:7890` or
219
+ `socks5://127.0.0.1:7890`, optionally with `user:pass@`. **Empty means follow the
220
+ environment.** It is a deployment-level capability and never enters a tool schema. The
221
+ resolution order is spelled out under [Proxies](#proxies).
222
+ - `proxyBypass` - host names that should connect directly, mirroring `NO_PROXY`.
205
223
 
206
224
  **Deployment-grade knobs** (not on the settings card; set them in the `config:` block of
207
225
  `cordis.patch.yml`): `requestTimeoutSeconds`, `maxUrlLength`, `browserExecutablePath`,
@@ -217,6 +235,105 @@ deployment config and are unreachable from model input.
217
235
  Disabling a tool also removes its system-prompt guidance, so the model is never told
218
236
  about a capability that is not registered.
219
237
 
238
+ ## Proxies
239
+
240
+ Where a fetch goes out, resolved in this order - **first hit wins**:
241
+
242
+ 1. `proxyBypass` or the environment's `NO_PROXY` matches this host -> direct.
243
+ 2. `proxyUrl` on the settings card is non-empty -> use it.
244
+ 3. `http_proxy` / `https_proxy` / `all_proxy` in the environment (upper case works too) ->
245
+ use the matching slot.
246
+ 4. The operating system's proxy settings (`scutil --proxy` on macOS, Internet Settings on
247
+ Windows) -> use them.
248
+ 5. Neither -> direct.
249
+
250
+ Each rung exists because leaving it out loses a class of users. The settings card is
251
+ explicit intent; environment variables cover CI, ssh sessions and `docker exec`, where a
252
+ proxy is only ever `export`ed in the shell; the OS settings are what proxy clients default
253
+ to on macOS and Windows - flip "system proxy" on in Clash Verge or Surge and every browser
254
+ on the machine is proxied, while curl_cffi has no idea system settings exist.
255
+
256
+ An explicit `proxyUrl` makes the last two rungs irrelevant: an explicit setting should
257
+ outrank an implicit source, otherwise "I set 7890 and it still goes through 1080" has
258
+ nowhere to be debugged from. The bypass list cuts across everything above it - a hit
259
+ connects directly no matter where the proxy came from.
260
+
261
+ What that looks like in practice - **with or without a proxy, the tools just work, with no
262
+ setting to fill in**:
263
+
264
+ | Your setup | Static fetching | Render / stealth / XHR |
265
+ | --- | --- | --- |
266
+ | No proxy | direct | direct |
267
+ | TUN mode (system-wide) | direct, forwarded by the TUN | same |
268
+ | `HTTPS_PROXY` exported in the shell | follows it automatically | follows it automatically |
269
+ | macOS / Windows "system proxy" switch | **follows it automatically** | follows it automatically |
270
+ | Intranet targets / internal Git | exclude with `proxyBypass` | exclude with `proxyBypass` |
271
+ | PAC (auto-config script) | not followed; stderr says to set `proxyUrl` | followed natively by the browser |
272
+
273
+ PAC is the one shape that still needs a hand-filled setting: evaluating it means
274
+ downloading a script and running JavaScript, and the dialects differ per platform, so this
275
+ plugin does not guess. When it sees one it says so on stderr instead of quietly connecting
276
+ directly.
277
+
278
+ Details worth knowing:
279
+
280
+ - **A proxy and synthetic DNS are two different problems.** A fake-IP proxy resolves
281
+ **every** domain into `198.18.0.0/15` (TUN clients often land in `100.64.0.0/10`);
282
+ neither is a public unicast range, so the gate rejects every URL and turning a proxy on
283
+ makes the plugin stop working. The plugin **detects** that class of resolver on its
284
+ own: it resolves three control names unrelated to any target - `example.com`,
285
+ `www.iana.org`, `www.wikipedia.org` - and if all of them land in one non-public pool
286
+ while staying distinct, it treats the resolver as fake-IP and **learns that pool's
287
+ boundary from the control answers**, allowing only addresses inside it.
288
+ `syntheticDnsRanges` is the manual fallback for detection failures. When detection does
289
+ not fire, the gate **stays strict**; an IP literal written into the URL is still judged
290
+ strictly, so `http://169.254.169.254/` stays blocked either way.
291
+ - **Through a proxy, a host name this machine cannot resolve is no longer rejected.** The
292
+ request never leaves this machine's network namespace, the proxy resolves the name, and
293
+ a local resolver coming up empty says nothing - while on a network with DNS filtered by
294
+ region, those are exactly the sites that need a proxy to be reachable at all. IP
295
+ literals, private answers the local resolver *can* produce, and single-label host names
296
+ (`intranet`, `router`) stay rejected.
297
+ - **WebRTC leaks are closed when proxied.** Chromium's WebRTC can send UDP around an
298
+ HTTP/SOCKS proxy, so one STUN call in a page is enough to learn the visitor's real exit
299
+ address. patchright gets `block_webrtc`; Playwright has no equivalent switch, so it goes
300
+ in as a Chromium launch flag.
301
+ - **A browser session picks its proxy once, at `session_open`**, and the whole session
302
+ shares it. The bypass list therefore does not apply per URL inside a browser session:
303
+ Playwright's proxy lives on the BrowserContext, and switching it means a new context,
304
+ which throws away the session's cookies and login state - precisely the reason a
305
+ session exists. Static sessions have no such limit and decide per request.
306
+ - **Credentials never reach a log or the capability frame.** The capability report echoes
307
+ `scheme://host:port` only, and its `source` field names which rung answered
308
+ (`settings` / `environment` / `system` / `none`) - that column is the first thing to look
309
+ at when someone reports that fetching does not work.
310
+ - **A malformed proxy address does not stop the plugin from loading.** A scheme outside
311
+ `http` / `https` / `socks4` / `socks5` / `socks5h` is logged to stderr and treated as
312
+ "not configured", rather than thrown out as a load failure.
313
+
314
+ ## Render or static: how to choose
315
+
316
+ `scrapling_render` is much slower and heavier than `scrapling_fetch`, so start static.
317
+ Two criteria:
318
+
319
+ **If the result is shorter than the static fetch, go back to static.** Plenty of sites
320
+ ship readable text from the server (for search engines and no-JS users) and then have their
321
+ own script empty those containers and swap in canvas / Lottie animation - for those pages
322
+ rendering can only lose content, and `networkIdle`, `waitSelector`, `mainContentOnly` and
323
+ `headless` all fail to bring it back: in the rendered DOM those containers are simply empty.
324
+ That is site behaviour, not a plugin defect.
325
+
326
+ **`waitSelector` waits for an element to appear, not for content to settle.** The element
327
+ can be removed after it matched, so "the selector matched" does not mean that content is
328
+ in the result.
329
+
330
+ One thing that is not a criterion but worth knowing: axis ticks inside SVG **cannot be
331
+ fetched at all**. After Chromium renders, the markdown conversion emits no `<svg>` or
332
+ `<text>` nodes, so "the numbers on the chart" are out of reach whichever tool you use - the
333
+ copy on an example card is server-rendered and was already there statically, it is not a
334
+ rendering product. For actual values, go through `scrapling_capture_xhr` to locate the
335
+ JSON endpoint.
336
+
220
337
  ## Security notes
221
338
 
222
339
  - **SSRF.** Scrapling itself has no SSRF protection. This plugin adds a URL policy on
package/README.zh-CN.md CHANGED
@@ -133,8 +133,10 @@ Scrapling 的 Markdown 输出报成 `text`,内置渲染路径显示完全正
133
133
 
134
134
  ## 配置
135
135
 
136
- 设置项在 **scrapling** 卡片下。[`host.ts`](./host.ts) 里标了可编辑的字段都会出现在
137
- 设置卡上;部署级字段只能通过 [`cordis.patch.yml`](./cordis.patch.yml) 的 `config:` 块设置。
136
+ 设置项在插件详情页的 **scrapling** 卡片下(设置 → 插件 → 展开本包)。[`host.ts`](./host.ts)
137
+ 里标了可编辑的字段都会按用途分组出现在卡上,每项带一句「改了会怎样」;部署级字段只能通过
138
+ [`cordis.patch.yml`](./cordis.patch.yml) 的 `config:` 块设置。卡片把值改回默认值时保存的是
139
+ 「清除覆盖」而不是写入同一个值——将来默认值变了,你这一项就会跟着变。
138
140
 
139
141
  主要开关:
140
142
 
@@ -151,15 +153,24 @@ Scrapling 的 Markdown 输出报成 `text`,内置渲染路径显示完全正
151
153
  因此**拿到的不是字节级原始 HTML**。
152
154
  - `allowedHosts` —— **安全相关的放行名单**,默认空(最严)。指向内网等于主动放弃那些目标
153
155
  的 SSRF 防护,抓自建/内网站点时才会需要。它同时喂 Host 闸门与 Python 侧守卫,两边同解。
154
- - `allowSyntheticDns` —— 放行代理软件返回的**合成** DNS 答案。fake-IP 代理把**每个**域名都
155
- 解析进 `198.18.0.0/15`(TUN 客户端常落在 `100.64.0.0/10`),两段都不是公网 unicast,
156
- 闸门于是把每个 URL 都判成私网——开了代理反而全都抓不了。**默认关**:不开代理没有理由
157
- 放宽判据,开了也不该由模型自己决定。放宽只作用于**解析结果**,URL 里写死的 IP 字面量
158
- 照旧严格判定,`http://169.254.169.254/` 仍然被拒。
156
+ - `syntheticDnsRanges` —— 合成 DNS 的**手动兜底**:填你那个代理的 fake-IP 段(CIDR,逗号
157
+ 分隔)。主路径是自动检测(见下「代理」),这一项留给「解析器把合成地址和真地址混着返回」
158
+ 的场合。写错的段直接丢掉而不是让插件起不来——它只是兜底,坏了退化成「维持最严」,不该崩。
159
+ - `proxyUrl` —— 抓取从哪儿出去,形如 `http://127.0.0.1:7890` 或
160
+ `socks5://127.0.0.1:7890`,可带 `user:pass@`。**留空表示跟随环境。** 它是部署级能力,
161
+ 绝不进入任何工具 schema。判定顺序见下面的「代理」。
162
+ - `proxyBypass` —— 直连、不走代理的主机名,对应 `NO_PROXY`。
159
163
  - `*MaxOutputChars` —— 各工具的输出封顶。宿主侧的封顶只影响进模型上下文的那一段,
160
164
  真正的字节上限在 Python 侧(`maxContentChars`)。`crawlMaxItems` 是部署侧给模型可传的
161
165
  `maxPages` 封的顶;越界的 `maxPages`(0、负数、NaN)**原样透传**,让 sidecar 报参数错误,
162
166
  而不是由本插件悄悄夹成一个看起来合法的值。
167
+ - `stripInlineImages` —— 默认**开**:剥掉正文里内嵌的 `data:` URI 图片,只留 alt 文本。
168
+ 这类图片对模型是纯 token 噪音 —— 一段 base64 SVG 编码进上下文要按字符计费,而模型从里面
169
+ 读不出任何图形信息。**不含内联图的页面逐字节不变**;含内联的页面省掉的量取决于内联图有多少 ——
170
+ 站点要么一个都不内联,要么就是一整面 logo 墙,中间态很少。所以默认开启对不含内联图的站点零
171
+ 风险,收益全部出现在命中时。代码围栏内的 `data:` URI 原样保留:那里多半是教程在教的示例代码,
172
+ 剥掉等于把文档改了。要把内联图当**原始数据**读(取配色或指纹之类)时才关掉。剥图发生在封顶
173
+ **之前**:顺序反了等于白剥,那些体积早就把 `maxContentChars` 的额度吃光了。
163
174
  - `answerProvider` / `answerModel` / `answerMaxTokens` —— `scrapling_answer` 走哪条模型路由
164
175
  以及单次回答的 token 上限。provider 与 model 都留空表示「跟随当前会话的模型」,
165
176
  `answerMaxTokens` 封住回答长度。
@@ -183,6 +194,81 @@ Scrapling 的 Markdown 输出报成 `text`,内置渲染路径显示完全正
183
194
  关掉某个工具时,它的 system prompt 指引段**也一并消失**,
184
195
  不会出现「工具没了但提示词还在」的割裂。
185
196
 
197
+ ## 代理
198
+
199
+ 抓取从哪儿出去,按这个顺序判定,**先命中先算**:
200
+
201
+ 1. `proxyBypass` 或环境里的 `NO_PROXY` 命中这个主机 → 直连。
202
+ 2. 设置卡上的 `proxyUrl` 非空 → 用它。
203
+ 3. 环境里的 `http_proxy` / `https_proxy` / `all_proxy`(大写同样认)→ 用对应那一栏。
204
+ 4. 操作系统的**代理设置**(macOS 的 `scutil --proxy`、Windows 的 Internet Settings)→ 用它。
205
+ 5. 都没有 → 直连。
206
+
207
+ 四档各自的存在理由,少一档就有一类用户抓不到东西:设置卡是显式意图;环境变量覆盖 CI、
208
+ ssh 会话、docker exec 这类只在 shell 里 export 过的地方;系统代理是 macOS 与 Windows 上
209
+ 代理软件的默认形态——Clash Verge、Surge 等把「系统代理」一勾,浏览器立刻就上,而
210
+ curl_cffi 对系统设置一无所知,不补这一档就会发现「浏览器能上、本插件的静态抓取抓不到」。
211
+
212
+ 设置卡里显式填了 `proxyUrl`,后面两档整份被忽略:显式设置就该压过隐式来源,否则「我明明
213
+ 填了 7890 却还在走 1080」这类问题无从查起。绕过清单横切在最前,命中即直连。
214
+
215
+ 各环境下的实际表现 —— **开或不开代理,工具都直接可用,一行设置都不用填**:
216
+
217
+ | 你的环境 | 静态抓取 | 渲染 / 反检测 / XHR |
218
+ | --- | --- | --- |
219
+ | 不开代理 | 直连 | 直连 |
220
+ | TUN 模式(系统层接管) | 直连,由 TUN 转发 | 同左 |
221
+ | 只在 shell 里 export `HTTPS_PROXY` | 自动跟随 | 自动跟随 |
222
+ | macOS / Windows「系统代理」开关 | **自动跟随** | 自动跟随 |
223
+ | 内网站点 / 内网 Git | 用 `proxyBypass` 排除 | 用 `proxyBypass` 排除 |
224
+ | PAC(自动配置脚本) | 不跟随,stderr 会提示填 `proxyUrl` | 浏览器原生跟随 |
225
+
226
+ PAC 是唯一还需要手工填一次的形态:求值 PAC 要下载脚本并跑 JavaScript,各平台格式互不相同,
227
+ 本包不猜。见到 PAC 时会在 stderr 指明该填 `proxyUrl`,而不是静默直连。
228
+
229
+ 几条容易踩的细节:
230
+
231
+ - **代理与合成 DNS 是两件事。** fake-IP 模式的代理把**每个**域名都解析进 `198.18.0.0/15`
232
+ (TUN 客户端常落在 `100.64.0.0/10`),两段都不是公网 unicast,闸门于是把每个 URL 都判成
233
+ 私网——开了代理反而全都抓不了。插件**自动识别**这一类解析器:从 `example.com` /
234
+ `www.iana.org` / `www.wikipedia.org` 三个与用户目标无关的控制域名各解析一次,全部落在同一段
235
+ 非公网小池且互不相同,即判为 fake-ip,并**从控制域名的答案里学出那个池子的边界**,只放行
236
+ 落在池内的地址。`syntheticDnsRanges` 是识别失灵时的手动兜底。识别不出来时**维持最严**,
237
+ 不会误放行;URL 里写死的 IP 字面量照旧严格判定,`http://169.254.169.254/` 始终被拒。
238
+ - **走了代理时,本机解析不出来的主机名不再判否。** 请求不从本机网络命名空间出去,域名由代理
239
+ 解析,本机解析器答不上来什么也说明不了——而在 DNS 被按地区过滤的网络里,恰恰是这些站点才
240
+ 需要代理才能访问。IP 字面量、本地能解析出来的私网答案、单标签主机名(`intranet`、`router`
241
+ 这类)三条**照旧拒**。
242
+ - **走代理时会关掉 WebRTC 泄露。** Chromium 的 WebRTC 能绕过 HTTP/SOCKS 代理直接发 UDP,页面
243
+ 里一句 STUN 就能问出访客的真实出口 IP。patchright 用 `block_webrtc`,Playwright 没有对应
244
+ 开关,走 Chromium 的启动 flag。
245
+ - **浏览器会话的代理在 `session_open` 时定一次**,之后整个会话共用。绕过清单因此对浏览器会话
246
+ 不逐 URL 生效——Playwright 的代理挂在 BrowserContext 上,中途换只能新建 context,而新建就
247
+ 等于丢掉这个会话攒下的 cookie 与登录态,那恰恰是会话存在的理由。静态会话没有这个限制,
248
+ 每个请求各自判一次。
249
+ - **凭据不会出现在任何日志或能力协商帧里。** 能力上报只回显 `scheme://host:port`,并用 `source`
250
+ 标出代理来自 `settings` / `environment` / `system` / `none` 哪一档——用户报「抓不到东西」时,
251
+ 这一栏就是第一眼的答案。
252
+ - **填错的代理地址不会让插件起不来。** 协议不在 `http` / `https` / `socks4` / `socks5` /
253
+ `socks5h` 之内时记一行 stderr 并按「未配置」处理,而不是抛出去让插件加载失败。
254
+
255
+ ## 渲染还是静态:怎么选
256
+
257
+ `scrapling_render` 比 `scrapling_fetch` 慢得多、也更吃内存,所以默认从静态抓取起步。两条
258
+ 判据:
259
+
260
+ **结果比静态抓取短,就退回静态抓取。** 不少页面在服务端下发了可读文字(给搜索引擎与无 JS 用户),
261
+ 加载后又用自己的脚本把那些容器清空、换成 canvas / Lottie 动画 —— 对这类页面渲染后只会更少。
262
+ `networkIdle`、`waitSelector`、`mainContentOnly`、`headless` 都补不回来:直接看渲染后的 DOM,
263
+ 那些容器已经是空的。这是站点行为,不是插件缺陷。
264
+
265
+ **`waitSelector` 等的是「元素出现」,不是「内容稳定」。** 元素可能在匹配成功之后才被脚本移除,
266
+ 所以「选择器匹配上了」不代表那部分内容还在结果里。
267
+
268
+ 还有一条不成判据但值得知道:SVG 里的坐标轴刻度**抓不到**。markdown 转换不输出 `<svg>` /
269
+ `<text>` 节点,所以「图表上的数字」用哪个工具都拿不到 —— 示例卡片上那些文案是服务端就有的,不是
270
+ 渲染产物。真要数值就走 `scrapling_capture_xhr` 找 JSON 接口。
271
+
186
272
  ## 安全说明
187
273
 
188
274
  - **SSRF。** Scrapling 自身**没有任何** SSRF 防护。本插件在 Host 侧加一层 URL 策略