1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! 命令行子命令与参数定义。
use crate::*;
/// 细粒度内网放行:HTTP 抓取和浏览器子请求共用同一份名单。
#[derive(clap::Args)]
pub(crate) struct AllowCidrs {
/// Allow private CIDR subnets (e.g. 10.0.0.0/8, 198.18.0.0/15) for HTTP fetching and browser rendering
#[arg(long = "allow-cidr", value_name = "CIDR", value_parser = parse_cidr)]
allow_cidrs: Vec<pagesift_net::IpCidr>,
}
impl AllowCidrs {
pub(crate) fn apply(&self, config: &mut pagesift::ScraperConfig) {
config
.fetcher
.ssrf
.allow_cidrs
.extend(self.allow_cidrs.iter().cloned());
}
}
pub(crate) fn parse_cidr(raw: &str) -> Result<pagesift_net::IpCidr, String> {
raw.parse()
.map_err(|error| format!("invalid CIDR {raw:?}: {error}"))
}
#[derive(Subcommand)]
pub(crate) enum Command {
/// Inspect local form metadata and redacted technology/secret indicators
Inspect {
file: Option<PathBuf>,
#[arg(long)]
base_url: Url,
},
/// Find a likely element after layout changes, with explicit abstention
Relocate {
file: PathBuf,
#[arg(long)]
original: PathBuf,
#[arg(long)]
selector: String,
},
/// Discover same-origin external JS modules and static navigation candidates
DiscoverJs {
url: Url,
#[arg(long, default_value_t = 32)]
max_files: usize,
#[arg(long, default_value_t = 3)]
max_depth: usize,
},
/// Extract original media URLs with an explicit site/protocol adapter
MediaExtract {
file: Option<PathBuf>,
#[arg(long)]
base_url: Url,
#[arg(long)]
adapter: pagesift::media::MediaAdapter,
},
/// Archive a media API response, then publish named files and metadata
MediaArchive {
url: Url,
#[arg(long)]
adapter: pagesift::media::MediaAdapter,
#[arg(long)]
out: PathBuf,
#[arg(long, default_value = "{id}-{hash}.{ext}")]
template: String,
#[arg(long, default_value_t = 268_435_456)]
max_bytes: u64,
},
/// Plan finite unencrypted HLS/DASH without downloading segments
StreamPlan {
file: Option<PathBuf>,
#[arg(long)]
base_url: Url,
},
/// Archive finite HLS/DASH locally (no A/V remux)
StreamDownload {
url: Url,
#[arg(long)]
out: PathBuf,
/// Select this zero-based HLS master variant; no automatic selection
#[arg(long)]
variant: Option<usize>,
#[arg(long, default_value_t = 268_435_456)]
max_bytes: u64,
},
/// Convert a local HTML file (or stdin) to Markdown
Convert {
/// HTML file to read; reads stdin when omitted or "-"
file: Option<PathBuf>,
/// URL the HTML was fetched from, used to make links absolute
#[arg(long)]
base_url: Option<Url>,
/// Keep the whole page instead of only the main content
#[arg(long)]
full: bool,
/// Decode the input with this charset (e.g. Shift_JIS, GBK) instead of detecting it
#[arg(long)]
charset: Option<String>,
},
/// Incrementally collect a JSON API; emit acknowledged upsert/delete events
Collect {
url: Url,
#[arg(long)]
source: String,
#[arg(long)]
data_dir: PathBuf,
/// Durable append-only event log (replays may contain duplicate events)
#[arg(long)]
out: PathBuf,
/// Abandon an incomplete scan and restart; committed inventory is retained
#[arg(long)]
restart: bool,
/// Adapter JSON file; omitted for a WordPress REST collection
#[arg(long)]
adapter: Option<PathBuf>,
/// Declare the listing complete and authoritative for deletion sync
#[arg(long)]
authoritative: bool,
#[arg(long, default_value_t = 100)]
max_pages: usize,
#[command(flatten)]
allow: AllowCidrs,
},
/// Archive one direct media URL with validated resume and a stable identity
Download {
url: Url,
#[arg(long)]
source: String,
#[arg(long)]
id: String,
#[arg(long, default_value = "1")]
revision: String,
#[arg(long)]
out: PathBuf,
#[arg(long, default_value_t = 268_435_456)]
max_bytes: u64,
#[arg(long)]
expected_blake3: Option<String>,
/// Discard an incomplete transfer; completed items remain immutable
#[arg(long)]
restart: bool,
#[command(flatten)]
allow: AllowCidrs,
},
/// Fetch one HTTP page with an explicit persistent, origin-isolated cookie session
SessionFetch {
url: Url,
#[arg(long)]
state: PathBuf,
/// Explicitly replace stored cookies and retired/expired session state
#[arg(long)]
reset_session: bool,
#[arg(long, default_value = "md", value_parser = ["md", "json"])]
format: String,
#[arg(long)]
full: bool,
#[command(flatten)]
allow: AllowCidrs,
},
/// Start the Firecrawl-compatible HTTP API
Serve {
/// Port (default: server.port in the config, else 3002)
#[arg(long)]
port: Option<u16>,
/// Address to listen on (default: server.host in the config, else 127.0.0.1)
#[arg(long)]
host: Option<String>,
/// Require `Authorization: Bearer <key>` (repeatable); also read from PAGESIFT_API_KEYS (comma separated)
#[arg(long = "api-key")]
api_keys: Vec<String>,
/// Public base URL used in job links (default: from the Host header)
#[arg(long)]
public_url: Option<String>,
/// Where the cache lives (default: $PAGESIFT_DATA_DIR, else ~/.local/share/pagesift)
#[arg(long)]
data_dir: Option<PathBuf>,
/// Disable the result cache
#[arg(long)]
no_cache: bool,
#[command(flatten)]
allow: AllowCidrs,
},
/// Scrape a single URL
Scrape {
url: String,
/// Output: md prints the Markdown, json prints the Firecrawl response
#[arg(long, default_value = "md", value_parser = ["md", "json"])]
format: String,
/// http, browser or auto
#[arg(long, default_value = "auto", value_parser = ["http", "browser", "auto"])]
engine: String,
/// Keep the whole page instead of only the main content
#[arg(long)]
full: bool,
/// Ignore robots.txt for this page
#[arg(long)]
ignore_robots: bool,
/// Egress proxy URL (e.g. http://127.0.0.1:7890, socks5://127.0.0.1:1080)
#[arg(long)]
proxy: Option<String>,
#[command(flatten)]
allow: AllowCidrs,
},
/// Crawl a site; writes one Markdown file per page with --out, else JSON lines to stdout
Crawl {
url: String,
#[arg(long, default_value_t = 100)]
limit: usize,
/// Directory for the Markdown files, an index.jsonl (one line per page) and a summary.json
/// (why the crawl stopped; coverage with --adaptive)
#[arg(long)]
out: Option<PathBuf>,
/// Maximum discovery depth from the start page
#[arg(long)]
depth: Option<usize>,
/// Only follow paths matching this regex (repeatable)
#[arg(long)]
include: Vec<String>,
/// Skip paths matching this regex (repeatable)
#[arg(long)]
exclude: Vec<String>,
/// Crawl the whole domain, not just pages under the start path (same as --scope domain)
#[arg(long)]
entire_domain: bool,
/// Path scope: auto (start directory, widened one level when the start page is a leaf
/// such as a blog post), exact (never widen), parent (one level up) or domain
/// (default: auto; domain with --query)
#[arg(long, value_parser = ["auto", "exact", "parent", "domain"])]
scope: Option<String>,
/// Follow links to subdomains
#[arg(long)]
subdomains: bool,
/// include, skip or only
#[arg(long, default_value = "include", value_parser = ["include", "skip", "only"])]
sitemap: String,
/// http, browser or auto
#[arg(long, default_value = "auto", value_parser = ["http", "browser", "auto"])]
engine: String,
/// Ignore robots.txt. On the command line this flag is the permission itself; the HTTP API
/// also needs `allow_crawl_ignore_robots = true` in the config (the operator's switch)
#[arg(long)]
ignore_robots: bool,
/// Follow bounded same-origin external JS candidates (requires --engine http)
#[arg(long)]
external_scripts: bool,
/// Crawl the pages most relevant to this query first
#[arg(long)]
query: Option<String>,
/// With --query: stop early once the collected pages cover the query
#[arg(long, requires = "query")]
adaptive: bool,
/// With --query: also fetch and convert PDF/Office documents linked from relevant pages
#[arg(long, requires = "query")]
attachments: bool,
/// Maximum number of attachments (default: same as --limit)
#[arg(long, requires = "attachments")]
attachment_limit: Option<usize>,
/// Tuning mode: adaptive (senses hardware and dynamically scales) or conservative
#[arg(long, default_value = "adaptive")]
tuning_mode: String,
#[command(flatten)]
allow: AllowCidrs,
},
/// Proactively explore host machine's performance curve and output an autonomous tuning profile
Tune {
/// Target profile intensity: standard, aggressive, or extreme
#[arg(long, default_value = "extreme", value_parser = ["standard", "aggressive", "extreme"])]
profile: String,
/// File to write the JSON profile to (prints to stdout if omitted)
#[arg(long)]
out: Option<PathBuf>,
},
/// Diagnose current performance bottlenecks, latency distributions, and hardware saturation
Diagnose {
/// Include deep performance saturation and latency quantile analysis
#[arg(long)]
performance: bool,
/// Output diagnosis report in JSON format
#[arg(long)]
json: bool,
},
/// List the URLs of a site
Map {
url: String,
#[arg(long, default_value_t = 5000)]
limit: usize,
/// Rank URLs matching these words first
#[arg(long)]
search: Option<String>,
/// include, skip or only
#[arg(long, default_value = "include", value_parser = ["include", "skip", "only"])]
sitemap: String,
/// Ignore robots.txt. On the command line this flag is the permission itself; the HTTP API
/// also needs `allow_crawl_ignore_robots = true` in the config (the operator's switch)
#[arg(long)]
ignore_robots: bool,
#[command(flatten)]
allow: AllowCidrs,
},
/// Run the MCP server (stdio by default)
Mcp {
/// stdio or http
#[arg(default_value = "stdio")]
transport: String,
/// Port for the http transport (listens on 127.0.0.1)
#[arg(long, default_value_t = 3003)]
port: u16,
},
/// Check the environment
Doctor,
/// Evaluate content extraction on local dataset snapshots
Bench {
/// Directory holding 1-0-0.csv and snapshots/ (see benchmarks/common/fetch_snapshots.py)
dir: PathBuf,
/// Write per-page results as JSON lines
#[arg(long)]
out: Option<PathBuf>,
/// Timing runs per page; the fastest is reported
#[arg(long, default_value_t = 3)]
runs: usize,
/// Only evaluate the first N dataset rows
#[arg(long)]
limit: Option<usize>,
/// Simulate `engine: auto` using browser-rendered snapshots (see bench-render)
#[arg(long)]
auto: bool,
/// Write the HTML each page was evaluated on, plus a manifest, for other tools
#[arg(long)]
emit_inputs: Option<PathBuf>,
/// Score another tool's output (JSON lines with id, markdown, ms) instead of pagesift
#[arg(long)]
external: Option<PathBuf>,
/// Threads for the throughput run; 0 uses every core
#[arg(long, default_value_t = 0)]
jobs: usize,
},
/// Render thin snapshots with a browser for `bench --auto`
#[command(hide = true)]
BenchRender {
dir: PathBuf,
#[arg(long)]
limit: Option<usize>,
#[arg(long, default_value_t = 8)]
concurrency: usize,
},
}