recker 1.0.95-next.42384f4 → 1.0.96-next.402197e
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/dist/browser/scrape/spider.d.ts +9 -1
- package/dist/browser/scrape/spider.js +53 -5
- package/dist/browser/utils/block-detector.js +83 -40
- package/dist/scrape/spider.d.ts +9 -1
- package/dist/scrape/spider.js +53 -5
- package/dist/utils/block-detector.js +83 -40
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -11,7 +11,7 @@ type CaptchaProvider = CaptchaDetectionResult['provider'];
|
|
|
11
11
|
export interface SpiderOptions {
|
|
12
12
|
maxDepth?: number;
|
|
13
13
|
maxPages?: number;
|
|
14
|
-
sameDomain?: boolean;
|
|
14
|
+
sameDomain?: boolean | 'exact' | 'subdomain';
|
|
15
15
|
concurrency?: number;
|
|
16
16
|
timeout?: number;
|
|
17
17
|
delay?: number;
|
|
@@ -63,6 +63,11 @@ export interface SpiderOptions {
|
|
|
63
63
|
domainRateLimit?: {
|
|
64
64
|
maxPerSecond?: number;
|
|
65
65
|
};
|
|
66
|
+
autoThrottle?: boolean | {
|
|
67
|
+
targetMs?: number;
|
|
68
|
+
minDelay?: number;
|
|
69
|
+
maxDelay?: number;
|
|
70
|
+
};
|
|
66
71
|
deduplicateContent?: boolean;
|
|
67
72
|
resume?: boolean;
|
|
68
73
|
strategy?: 'bfs' | 'dfs';
|
|
@@ -191,12 +196,14 @@ export declare class Spider {
|
|
|
191
196
|
private _queueSize;
|
|
192
197
|
private _resultCount;
|
|
193
198
|
private baseHost;
|
|
199
|
+
private baseRootDomain;
|
|
194
200
|
private running;
|
|
195
201
|
private aborted;
|
|
196
202
|
private abortController;
|
|
197
203
|
private pendingCount;
|
|
198
204
|
private domainRequestTimestamps;
|
|
199
205
|
private contentHashes;
|
|
206
|
+
private domainAvgResponseTime;
|
|
200
207
|
private blockedDomains;
|
|
201
208
|
private curlTransport;
|
|
202
209
|
private curlAvailable;
|
|
@@ -225,6 +232,7 @@ export declare class Spider {
|
|
|
225
232
|
private waitForDomainPenalty;
|
|
226
233
|
private registerDomainBlock;
|
|
227
234
|
private registerDomainSuccess;
|
|
235
|
+
private updateAutoThrottle;
|
|
228
236
|
private getCaptchaRetryMultiplier;
|
|
229
237
|
private registerDomainChallenge;
|
|
230
238
|
private getRetryWait;
|
|
@@ -108,15 +108,30 @@ function normalizeUrl(urlStr) {
|
|
|
108
108
|
return urlStr;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
function
|
|
111
|
+
function getRootDomain(hostname) {
|
|
112
|
+
const parts = hostname.replace(/^www\./, '').split('.');
|
|
113
|
+
if (parts.length >= 3 && parts[parts.length - 2].length <= 3) {
|
|
114
|
+
return parts.slice(-3).join('.');
|
|
115
|
+
}
|
|
116
|
+
return parts.slice(-2).join('.');
|
|
117
|
+
}
|
|
118
|
+
function shouldCrawl(url, baseHost, options, baseRootDomain) {
|
|
112
119
|
try {
|
|
113
120
|
const parsed = new URL(url);
|
|
114
121
|
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
115
122
|
return false;
|
|
116
123
|
}
|
|
117
124
|
const hostname = parsed.hostname.replace(/^www\./, '');
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
const sameDomain = options.sameDomain;
|
|
126
|
+
if (sameDomain === 'subdomain') {
|
|
127
|
+
const pageRoot = getRootDomain(hostname);
|
|
128
|
+
const rootDomain = baseRootDomain ?? getRootDomain(baseHost);
|
|
129
|
+
if (pageRoot !== rootDomain)
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
else if (sameDomain !== false) {
|
|
133
|
+
if (hostname !== baseHost)
|
|
134
|
+
return false;
|
|
120
135
|
}
|
|
121
136
|
const skipExtensions = [
|
|
122
137
|
'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico',
|
|
@@ -193,12 +208,14 @@ export class Spider {
|
|
|
193
208
|
_queueSize = 0;
|
|
194
209
|
_resultCount = 0;
|
|
195
210
|
baseHost = '';
|
|
211
|
+
baseRootDomain = '';
|
|
196
212
|
running = false;
|
|
197
213
|
aborted = false;
|
|
198
214
|
abortController = new AbortController();
|
|
199
215
|
pendingCount = 0;
|
|
200
216
|
domainRequestTimestamps = new Map();
|
|
201
217
|
contentHashes = new Map();
|
|
218
|
+
domainAvgResponseTime = new Map();
|
|
202
219
|
blockedDomains = new Set();
|
|
203
220
|
curlTransport = null;
|
|
204
221
|
curlAvailable = false;
|
|
@@ -285,6 +302,7 @@ export class Spider {
|
|
|
285
302
|
extract: extractSchema,
|
|
286
303
|
parserOptions: options.parserOptions,
|
|
287
304
|
domainRateLimit: options.domainRateLimit,
|
|
305
|
+
autoThrottle: options.autoThrottle ?? false,
|
|
288
306
|
deduplicateContent: options.deduplicateContent ?? false,
|
|
289
307
|
resume: options.resume ?? false,
|
|
290
308
|
strategy: options.strategy ?? 'bfs',
|
|
@@ -326,6 +344,7 @@ export class Spider {
|
|
|
326
344
|
const normalizedStart = normalizeUrl(startUrl);
|
|
327
345
|
const baseUrl = new URL(normalizedStart).origin;
|
|
328
346
|
this.baseHost = new URL(normalizedStart).hostname.replace(/^www\./, '');
|
|
347
|
+
this.baseRootDomain = getRootDomain(this.baseHost);
|
|
329
348
|
if (!this.options.resume) {
|
|
330
349
|
await this.crawlQueue.clear();
|
|
331
350
|
await this.crawlStorage.clear();
|
|
@@ -334,6 +353,7 @@ export class Spider {
|
|
|
334
353
|
this._resultCount = 0;
|
|
335
354
|
this.domainRequestTimestamps.clear();
|
|
336
355
|
this.contentHashes.clear();
|
|
356
|
+
this.domainAvgResponseTime.clear();
|
|
337
357
|
}
|
|
338
358
|
else {
|
|
339
359
|
this._queueSize = await this.crawlQueue.size();
|
|
@@ -464,6 +484,14 @@ export class Spider {
|
|
|
464
484
|
: new Set(pages.map(r => r.url));
|
|
465
485
|
await this.crawlQueue.close?.();
|
|
466
486
|
await this.crawlStorage.close?.();
|
|
487
|
+
for (const client of this.proxyClients.values()) {
|
|
488
|
+
if (typeof client.destroy === 'function') {
|
|
489
|
+
client.destroy();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
this.proxyClients.clear();
|
|
493
|
+
await this.proxyAdapter?.close?.();
|
|
494
|
+
this.domainAvgResponseTime.clear();
|
|
467
495
|
return {
|
|
468
496
|
startUrl: normalizedStart,
|
|
469
497
|
pages,
|
|
@@ -789,6 +817,9 @@ export class Spider {
|
|
|
789
817
|
const isHighQualitySuccess = response.status < 400 && !detection.blocked && !hasCaptchaSignal;
|
|
790
818
|
if (isHighQualitySuccess) {
|
|
791
819
|
this.registerDomainSuccess(hostname);
|
|
820
|
+
if (timings?.total) {
|
|
821
|
+
this.updateAutoThrottle(hostname, timings.total);
|
|
822
|
+
}
|
|
792
823
|
}
|
|
793
824
|
if (!shouldRetry || attempt === maxAttempts - 1) {
|
|
794
825
|
if (proxyUrl && this.proxyAdapter?.reportResult) {
|
|
@@ -1082,7 +1113,7 @@ export class Spider {
|
|
|
1082
1113
|
if (!link.href)
|
|
1083
1114
|
continue;
|
|
1084
1115
|
const normalized = normalizeUrl(link.href);
|
|
1085
|
-
if (!shouldCrawl(normalized, this.baseHost, this.options))
|
|
1116
|
+
if (!shouldCrawl(normalized, this.baseHost, this.options, this.baseRootDomain))
|
|
1086
1117
|
continue;
|
|
1087
1118
|
candidateUrls.push(normalized);
|
|
1088
1119
|
candidates.push({ url: normalized, depth: item.depth + 1 });
|
|
@@ -1193,6 +1224,7 @@ export class Spider {
|
|
|
1193
1224
|
consecutiveUndiciFailures: 0,
|
|
1194
1225
|
lastTransport: 'undici',
|
|
1195
1226
|
lastCaptchaConfidence: 0,
|
|
1227
|
+
autoThrottleDelay: 0,
|
|
1196
1228
|
};
|
|
1197
1229
|
this.domainStates.set(hostname, next);
|
|
1198
1230
|
return next;
|
|
@@ -1295,7 +1327,9 @@ export class Spider {
|
|
|
1295
1327
|
async waitForDomainPenalty(hostname) {
|
|
1296
1328
|
const state = this.getOrCreateDomainState(hostname);
|
|
1297
1329
|
const now = Date.now();
|
|
1298
|
-
const
|
|
1330
|
+
const penaltyDelay = Math.max(state.penaltyUntil, state.challengeCooldownUntil) - now;
|
|
1331
|
+
const throttleDelay = state.autoThrottleDelay ?? 0;
|
|
1332
|
+
const delay = Math.max(penaltyDelay, throttleDelay);
|
|
1299
1333
|
if (delay > 0) {
|
|
1300
1334
|
await sleep(delay, this.abortController.signal);
|
|
1301
1335
|
}
|
|
@@ -1323,6 +1357,20 @@ export class Spider {
|
|
|
1323
1357
|
state.lastCaptchaProvider = undefined;
|
|
1324
1358
|
}
|
|
1325
1359
|
}
|
|
1360
|
+
updateAutoThrottle(hostname, responseTimeMs) {
|
|
1361
|
+
const config = this.options.autoThrottle;
|
|
1362
|
+
if (!config)
|
|
1363
|
+
return;
|
|
1364
|
+
const target = (typeof config === 'object' ? config.targetMs : undefined) ?? 500;
|
|
1365
|
+
const minDelay = (typeof config === 'object' ? config.minDelay : undefined) ?? 50;
|
|
1366
|
+
const maxDelay = (typeof config === 'object' ? config.maxDelay : undefined) ?? 30000;
|
|
1367
|
+
const prev = this.domainAvgResponseTime.get(hostname) ?? responseTimeMs;
|
|
1368
|
+
const avg = prev * 0.7 + responseTimeMs * 0.3;
|
|
1369
|
+
this.domainAvgResponseTime.set(hostname, avg);
|
|
1370
|
+
const ratio = avg / target;
|
|
1371
|
+
const state = this.getOrCreateDomainState(hostname);
|
|
1372
|
+
state.autoThrottleDelay = Math.max(minDelay, Math.min(maxDelay, Math.round(this.options.delay * ratio)));
|
|
1373
|
+
}
|
|
1326
1374
|
getCaptchaRetryMultiplier(provider) {
|
|
1327
1375
|
if (!provider)
|
|
1328
1376
|
return 1.2;
|
|
@@ -526,18 +526,42 @@ export function detectBlock(response, body) {
|
|
|
526
526
|
if (body) {
|
|
527
527
|
const isLongBody = body.length > 100_000;
|
|
528
528
|
const checkBody = isLongBody ? body.slice(0, 30_000) : body;
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
529
|
+
const lowerBody = checkBody.slice(0, 8000).toLowerCase();
|
|
530
|
+
const hasAnyChallengeKeyword = lowerBody.includes('captcha') ||
|
|
531
|
+
lowerBody.includes('cloudflare') ||
|
|
532
|
+
lowerBody.includes('datadome') ||
|
|
533
|
+
lowerBody.includes('blocked') ||
|
|
534
|
+
lowerBody.includes('denied') ||
|
|
535
|
+
lowerBody.includes('forbidden') ||
|
|
536
|
+
lowerBody.includes('too many') ||
|
|
537
|
+
lowerBody.includes('rate limit') ||
|
|
538
|
+
lowerBody.includes('checking your browser') ||
|
|
539
|
+
lowerBody.includes('just a moment') ||
|
|
540
|
+
lowerBody.includes('security check') ||
|
|
541
|
+
lowerBody.includes('human verification') ||
|
|
542
|
+
lowerBody.includes('perimeterx') ||
|
|
543
|
+
lowerBody.includes('incapsula') ||
|
|
544
|
+
lowerBody.includes('imperva') ||
|
|
545
|
+
lowerBody.includes('akamai') ||
|
|
546
|
+
lowerBody.includes('httpservice') ||
|
|
547
|
+
lowerBody.includes('enablejs') ||
|
|
548
|
+
lowerBody.includes('verify') ||
|
|
549
|
+
lowerBody.includes('suspicious') ||
|
|
550
|
+
lowerBody.includes('bot') ||
|
|
551
|
+
lowerBody.includes('access denied');
|
|
552
|
+
if (hasAnyChallengeKeyword || response.status !== 200) {
|
|
553
|
+
for (const { pattern, reason, confidence, description } of BLOCK_PATTERNS) {
|
|
554
|
+
if (isLongBody && response.status === 200 && confidence < 0.85 && !hasAnyChallengeKeyword) {
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
if (pattern.test(checkBody)) {
|
|
558
|
+
results.push({
|
|
559
|
+
blocked: true,
|
|
560
|
+
reason,
|
|
561
|
+
confidence,
|
|
562
|
+
description,
|
|
563
|
+
});
|
|
564
|
+
}
|
|
541
565
|
}
|
|
542
566
|
}
|
|
543
567
|
if (body.length < 5000 &&
|
|
@@ -603,8 +627,25 @@ export function detectCaptcha(response, body) {
|
|
|
603
627
|
if (body) {
|
|
604
628
|
const checkBody = body.length < 120_000 ? body : body.slice(0, 120_000);
|
|
605
629
|
const isTinyBody = checkBody.length > 0 && checkBody.length < 12000;
|
|
630
|
+
const lowerSnippet = checkBody.slice(0, 8000).toLowerCase();
|
|
631
|
+
const hasAnyCaptchaKeyword = lowerSnippet.includes('captcha') ||
|
|
632
|
+
lowerSnippet.includes('recaptcha') ||
|
|
633
|
+
lowerSnippet.includes('hcaptcha') ||
|
|
634
|
+
lowerSnippet.includes('turnstile') ||
|
|
635
|
+
lowerSnippet.includes('challenge') ||
|
|
636
|
+
lowerSnippet.includes('cloudflare') ||
|
|
637
|
+
lowerSnippet.includes('datadome') ||
|
|
638
|
+
lowerSnippet.includes('perimeterx') ||
|
|
639
|
+
lowerSnippet.includes('funcaptcha') ||
|
|
640
|
+
lowerSnippet.includes('arkose') ||
|
|
641
|
+
lowerSnippet.includes('sitekey') ||
|
|
642
|
+
lowerSnippet.includes('just a moment') ||
|
|
643
|
+
lowerSnippet.includes('human verification');
|
|
606
644
|
const hasHtmlTags = /<html|<head|<body|<script|<meta/i.test(checkBody);
|
|
607
|
-
const challengeTitleOrText =
|
|
645
|
+
const challengeTitleOrText = hasAnyCaptchaKeyword ||
|
|
646
|
+
lowerSnippet.includes('attention required') ||
|
|
647
|
+
lowerSnippet.includes('verify you are human') ||
|
|
648
|
+
lowerSnippet.includes('security check');
|
|
608
649
|
if (isTinyBody && challengeTitleOrText && hasHtmlTags) {
|
|
609
650
|
addMatch(matches, 'generic', 0.6, 'Tiny HTML response with challenge-like text');
|
|
610
651
|
}
|
|
@@ -616,24 +657,38 @@ export function detectCaptcha(response, body) {
|
|
|
616
657
|
addMatch(matches, provider, confidence, description);
|
|
617
658
|
}
|
|
618
659
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
660
|
+
if (hasAnyCaptchaKeyword || !(response.status >= 200 && response.status < 300)) {
|
|
661
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_SCRIPT_PATTERNS) {
|
|
662
|
+
if (pattern.test(checkBody)) {
|
|
663
|
+
addMatch(matches, provider, confidence, description);
|
|
664
|
+
}
|
|
622
665
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
666
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
667
|
+
if (pattern.test(checkBody)) {
|
|
668
|
+
addMatch(matches, provider, confidence, description);
|
|
669
|
+
}
|
|
627
670
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
671
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
672
|
+
if (pattern.test(checkBody)) {
|
|
673
|
+
addMatch(matches, provider, confidence, description);
|
|
674
|
+
}
|
|
632
675
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
676
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
677
|
+
if (pattern.test(checkBody)) {
|
|
678
|
+
addMatch(matches, provider, confidence, description);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_PATTERNS) {
|
|
682
|
+
if (pattern.test(checkBody)) {
|
|
683
|
+
addMatch(matches, provider, confidence, description);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (/<script[^>]*src=["'][^"']*(?:challenges\.cloudflare\.com|recaptcha\/api\.js|hcaptcha\.com\/1\/api\.js|challenges\.perimeterx\.)[^"']*["'][^>]*>/i.test(checkBody.slice(0, 120_000))) {
|
|
687
|
+
addMatch(matches, 'cloudflare', 0.6, 'Known challenge script reference found in HTML');
|
|
688
|
+
}
|
|
689
|
+
if ((response.status >= 500 && response.status < 600) &&
|
|
690
|
+
/cloudflare|challenge|verification|blocked|security/i.test(checkBody.slice(0, 3000))) {
|
|
691
|
+
addMatch(matches, 'generic', 0.2, 'Server challenge-like text on error response');
|
|
637
692
|
}
|
|
638
693
|
}
|
|
639
694
|
if (response.status === 403 || response.status === 503) {
|
|
@@ -653,18 +708,6 @@ export function detectCaptcha(response, body) {
|
|
|
653
708
|
/refresh|challenge|verify|captcha|javascript challenge|cloudflare|bot/i.test((location || ''))) {
|
|
654
709
|
addMatch(matches, 'generic', 0.67, 'Redirect location indicates bot challenge flow');
|
|
655
710
|
}
|
|
656
|
-
if (/<script[^>]*src=["'][^"']*(?:challenges\.cloudflare\.com|recaptcha\/api\.js|hcaptcha\.com\/1\/api\.js|challenges\.perimeterx\.)[^"']*["'][^>]*>/i.test(checkBody.slice(0, 120_000))) {
|
|
657
|
-
addMatch(matches, 'cloudflare', 0.6, 'Known challenge script reference found in HTML');
|
|
658
|
-
}
|
|
659
|
-
if ((response.status >= 500 && response.status < 600) &&
|
|
660
|
-
/cloudflare|challenge|verification|blocked|security/i.test(checkBody.slice(0, 3000))) {
|
|
661
|
-
addMatch(matches, 'generic', 0.2, 'Server challenge-like text on error response');
|
|
662
|
-
}
|
|
663
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_PATTERNS) {
|
|
664
|
-
if (pattern.test(checkBody)) {
|
|
665
|
-
addMatch(matches, provider, confidence, description);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
711
|
}
|
|
669
712
|
const result = scoreByProvider(matches);
|
|
670
713
|
if (result.confidence >= 0.5 || hasCaptchaHeader) {
|
package/dist/scrape/spider.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ type CaptchaProvider = CaptchaDetectionResult['provider'];
|
|
|
11
11
|
export interface SpiderOptions {
|
|
12
12
|
maxDepth?: number;
|
|
13
13
|
maxPages?: number;
|
|
14
|
-
sameDomain?: boolean;
|
|
14
|
+
sameDomain?: boolean | 'exact' | 'subdomain';
|
|
15
15
|
concurrency?: number;
|
|
16
16
|
timeout?: number;
|
|
17
17
|
delay?: number;
|
|
@@ -63,6 +63,11 @@ export interface SpiderOptions {
|
|
|
63
63
|
domainRateLimit?: {
|
|
64
64
|
maxPerSecond?: number;
|
|
65
65
|
};
|
|
66
|
+
autoThrottle?: boolean | {
|
|
67
|
+
targetMs?: number;
|
|
68
|
+
minDelay?: number;
|
|
69
|
+
maxDelay?: number;
|
|
70
|
+
};
|
|
66
71
|
deduplicateContent?: boolean;
|
|
67
72
|
resume?: boolean;
|
|
68
73
|
strategy?: 'bfs' | 'dfs';
|
|
@@ -191,12 +196,14 @@ export declare class Spider {
|
|
|
191
196
|
private _queueSize;
|
|
192
197
|
private _resultCount;
|
|
193
198
|
private baseHost;
|
|
199
|
+
private baseRootDomain;
|
|
194
200
|
private running;
|
|
195
201
|
private aborted;
|
|
196
202
|
private abortController;
|
|
197
203
|
private pendingCount;
|
|
198
204
|
private domainRequestTimestamps;
|
|
199
205
|
private contentHashes;
|
|
206
|
+
private domainAvgResponseTime;
|
|
200
207
|
private blockedDomains;
|
|
201
208
|
private curlTransport;
|
|
202
209
|
private curlAvailable;
|
|
@@ -225,6 +232,7 @@ export declare class Spider {
|
|
|
225
232
|
private waitForDomainPenalty;
|
|
226
233
|
private registerDomainBlock;
|
|
227
234
|
private registerDomainSuccess;
|
|
235
|
+
private updateAutoThrottle;
|
|
228
236
|
private getCaptchaRetryMultiplier;
|
|
229
237
|
private registerDomainChallenge;
|
|
230
238
|
private getRetryWait;
|
package/dist/scrape/spider.js
CHANGED
|
@@ -108,15 +108,30 @@ function normalizeUrl(urlStr) {
|
|
|
108
108
|
return urlStr;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
function
|
|
111
|
+
function getRootDomain(hostname) {
|
|
112
|
+
const parts = hostname.replace(/^www\./, '').split('.');
|
|
113
|
+
if (parts.length >= 3 && parts[parts.length - 2].length <= 3) {
|
|
114
|
+
return parts.slice(-3).join('.');
|
|
115
|
+
}
|
|
116
|
+
return parts.slice(-2).join('.');
|
|
117
|
+
}
|
|
118
|
+
function shouldCrawl(url, baseHost, options, baseRootDomain) {
|
|
112
119
|
try {
|
|
113
120
|
const parsed = new URL(url);
|
|
114
121
|
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
115
122
|
return false;
|
|
116
123
|
}
|
|
117
124
|
const hostname = parsed.hostname.replace(/^www\./, '');
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
const sameDomain = options.sameDomain;
|
|
126
|
+
if (sameDomain === 'subdomain') {
|
|
127
|
+
const pageRoot = getRootDomain(hostname);
|
|
128
|
+
const rootDomain = baseRootDomain ?? getRootDomain(baseHost);
|
|
129
|
+
if (pageRoot !== rootDomain)
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
else if (sameDomain !== false) {
|
|
133
|
+
if (hostname !== baseHost)
|
|
134
|
+
return false;
|
|
120
135
|
}
|
|
121
136
|
const skipExtensions = [
|
|
122
137
|
'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico',
|
|
@@ -193,12 +208,14 @@ export class Spider {
|
|
|
193
208
|
_queueSize = 0;
|
|
194
209
|
_resultCount = 0;
|
|
195
210
|
baseHost = '';
|
|
211
|
+
baseRootDomain = '';
|
|
196
212
|
running = false;
|
|
197
213
|
aborted = false;
|
|
198
214
|
abortController = new AbortController();
|
|
199
215
|
pendingCount = 0;
|
|
200
216
|
domainRequestTimestamps = new Map();
|
|
201
217
|
contentHashes = new Map();
|
|
218
|
+
domainAvgResponseTime = new Map();
|
|
202
219
|
blockedDomains = new Set();
|
|
203
220
|
curlTransport = null;
|
|
204
221
|
curlAvailable = false;
|
|
@@ -285,6 +302,7 @@ export class Spider {
|
|
|
285
302
|
extract: extractSchema,
|
|
286
303
|
parserOptions: options.parserOptions,
|
|
287
304
|
domainRateLimit: options.domainRateLimit,
|
|
305
|
+
autoThrottle: options.autoThrottle ?? false,
|
|
288
306
|
deduplicateContent: options.deduplicateContent ?? false,
|
|
289
307
|
resume: options.resume ?? false,
|
|
290
308
|
strategy: options.strategy ?? 'bfs',
|
|
@@ -326,6 +344,7 @@ export class Spider {
|
|
|
326
344
|
const normalizedStart = normalizeUrl(startUrl);
|
|
327
345
|
const baseUrl = new URL(normalizedStart).origin;
|
|
328
346
|
this.baseHost = new URL(normalizedStart).hostname.replace(/^www\./, '');
|
|
347
|
+
this.baseRootDomain = getRootDomain(this.baseHost);
|
|
329
348
|
if (!this.options.resume) {
|
|
330
349
|
await this.crawlQueue.clear();
|
|
331
350
|
await this.crawlStorage.clear();
|
|
@@ -334,6 +353,7 @@ export class Spider {
|
|
|
334
353
|
this._resultCount = 0;
|
|
335
354
|
this.domainRequestTimestamps.clear();
|
|
336
355
|
this.contentHashes.clear();
|
|
356
|
+
this.domainAvgResponseTime.clear();
|
|
337
357
|
}
|
|
338
358
|
else {
|
|
339
359
|
this._queueSize = await this.crawlQueue.size();
|
|
@@ -464,6 +484,14 @@ export class Spider {
|
|
|
464
484
|
: new Set(pages.map(r => r.url));
|
|
465
485
|
await this.crawlQueue.close?.();
|
|
466
486
|
await this.crawlStorage.close?.();
|
|
487
|
+
for (const client of this.proxyClients.values()) {
|
|
488
|
+
if (typeof client.destroy === 'function') {
|
|
489
|
+
client.destroy();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
this.proxyClients.clear();
|
|
493
|
+
await this.proxyAdapter?.close?.();
|
|
494
|
+
this.domainAvgResponseTime.clear();
|
|
467
495
|
return {
|
|
468
496
|
startUrl: normalizedStart,
|
|
469
497
|
pages,
|
|
@@ -789,6 +817,9 @@ export class Spider {
|
|
|
789
817
|
const isHighQualitySuccess = response.status < 400 && !detection.blocked && !hasCaptchaSignal;
|
|
790
818
|
if (isHighQualitySuccess) {
|
|
791
819
|
this.registerDomainSuccess(hostname);
|
|
820
|
+
if (timings?.total) {
|
|
821
|
+
this.updateAutoThrottle(hostname, timings.total);
|
|
822
|
+
}
|
|
792
823
|
}
|
|
793
824
|
if (!shouldRetry || attempt === maxAttempts - 1) {
|
|
794
825
|
if (proxyUrl && this.proxyAdapter?.reportResult) {
|
|
@@ -1082,7 +1113,7 @@ export class Spider {
|
|
|
1082
1113
|
if (!link.href)
|
|
1083
1114
|
continue;
|
|
1084
1115
|
const normalized = normalizeUrl(link.href);
|
|
1085
|
-
if (!shouldCrawl(normalized, this.baseHost, this.options))
|
|
1116
|
+
if (!shouldCrawl(normalized, this.baseHost, this.options, this.baseRootDomain))
|
|
1086
1117
|
continue;
|
|
1087
1118
|
candidateUrls.push(normalized);
|
|
1088
1119
|
candidates.push({ url: normalized, depth: item.depth + 1 });
|
|
@@ -1193,6 +1224,7 @@ export class Spider {
|
|
|
1193
1224
|
consecutiveUndiciFailures: 0,
|
|
1194
1225
|
lastTransport: 'undici',
|
|
1195
1226
|
lastCaptchaConfidence: 0,
|
|
1227
|
+
autoThrottleDelay: 0,
|
|
1196
1228
|
};
|
|
1197
1229
|
this.domainStates.set(hostname, next);
|
|
1198
1230
|
return next;
|
|
@@ -1295,7 +1327,9 @@ export class Spider {
|
|
|
1295
1327
|
async waitForDomainPenalty(hostname) {
|
|
1296
1328
|
const state = this.getOrCreateDomainState(hostname);
|
|
1297
1329
|
const now = Date.now();
|
|
1298
|
-
const
|
|
1330
|
+
const penaltyDelay = Math.max(state.penaltyUntil, state.challengeCooldownUntil) - now;
|
|
1331
|
+
const throttleDelay = state.autoThrottleDelay ?? 0;
|
|
1332
|
+
const delay = Math.max(penaltyDelay, throttleDelay);
|
|
1299
1333
|
if (delay > 0) {
|
|
1300
1334
|
await sleep(delay, this.abortController.signal);
|
|
1301
1335
|
}
|
|
@@ -1323,6 +1357,20 @@ export class Spider {
|
|
|
1323
1357
|
state.lastCaptchaProvider = undefined;
|
|
1324
1358
|
}
|
|
1325
1359
|
}
|
|
1360
|
+
updateAutoThrottle(hostname, responseTimeMs) {
|
|
1361
|
+
const config = this.options.autoThrottle;
|
|
1362
|
+
if (!config)
|
|
1363
|
+
return;
|
|
1364
|
+
const target = (typeof config === 'object' ? config.targetMs : undefined) ?? 500;
|
|
1365
|
+
const minDelay = (typeof config === 'object' ? config.minDelay : undefined) ?? 50;
|
|
1366
|
+
const maxDelay = (typeof config === 'object' ? config.maxDelay : undefined) ?? 30000;
|
|
1367
|
+
const prev = this.domainAvgResponseTime.get(hostname) ?? responseTimeMs;
|
|
1368
|
+
const avg = prev * 0.7 + responseTimeMs * 0.3;
|
|
1369
|
+
this.domainAvgResponseTime.set(hostname, avg);
|
|
1370
|
+
const ratio = avg / target;
|
|
1371
|
+
const state = this.getOrCreateDomainState(hostname);
|
|
1372
|
+
state.autoThrottleDelay = Math.max(minDelay, Math.min(maxDelay, Math.round(this.options.delay * ratio)));
|
|
1373
|
+
}
|
|
1326
1374
|
getCaptchaRetryMultiplier(provider) {
|
|
1327
1375
|
if (!provider)
|
|
1328
1376
|
return 1.2;
|
|
@@ -526,18 +526,42 @@ export function detectBlock(response, body) {
|
|
|
526
526
|
if (body) {
|
|
527
527
|
const isLongBody = body.length > 100_000;
|
|
528
528
|
const checkBody = isLongBody ? body.slice(0, 30_000) : body;
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
529
|
+
const lowerBody = checkBody.slice(0, 8000).toLowerCase();
|
|
530
|
+
const hasAnyChallengeKeyword = lowerBody.includes('captcha') ||
|
|
531
|
+
lowerBody.includes('cloudflare') ||
|
|
532
|
+
lowerBody.includes('datadome') ||
|
|
533
|
+
lowerBody.includes('blocked') ||
|
|
534
|
+
lowerBody.includes('denied') ||
|
|
535
|
+
lowerBody.includes('forbidden') ||
|
|
536
|
+
lowerBody.includes('too many') ||
|
|
537
|
+
lowerBody.includes('rate limit') ||
|
|
538
|
+
lowerBody.includes('checking your browser') ||
|
|
539
|
+
lowerBody.includes('just a moment') ||
|
|
540
|
+
lowerBody.includes('security check') ||
|
|
541
|
+
lowerBody.includes('human verification') ||
|
|
542
|
+
lowerBody.includes('perimeterx') ||
|
|
543
|
+
lowerBody.includes('incapsula') ||
|
|
544
|
+
lowerBody.includes('imperva') ||
|
|
545
|
+
lowerBody.includes('akamai') ||
|
|
546
|
+
lowerBody.includes('httpservice') ||
|
|
547
|
+
lowerBody.includes('enablejs') ||
|
|
548
|
+
lowerBody.includes('verify') ||
|
|
549
|
+
lowerBody.includes('suspicious') ||
|
|
550
|
+
lowerBody.includes('bot') ||
|
|
551
|
+
lowerBody.includes('access denied');
|
|
552
|
+
if (hasAnyChallengeKeyword || response.status !== 200) {
|
|
553
|
+
for (const { pattern, reason, confidence, description } of BLOCK_PATTERNS) {
|
|
554
|
+
if (isLongBody && response.status === 200 && confidence < 0.85 && !hasAnyChallengeKeyword) {
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
if (pattern.test(checkBody)) {
|
|
558
|
+
results.push({
|
|
559
|
+
blocked: true,
|
|
560
|
+
reason,
|
|
561
|
+
confidence,
|
|
562
|
+
description,
|
|
563
|
+
});
|
|
564
|
+
}
|
|
541
565
|
}
|
|
542
566
|
}
|
|
543
567
|
if (body.length < 5000 &&
|
|
@@ -603,8 +627,25 @@ export function detectCaptcha(response, body) {
|
|
|
603
627
|
if (body) {
|
|
604
628
|
const checkBody = body.length < 120_000 ? body : body.slice(0, 120_000);
|
|
605
629
|
const isTinyBody = checkBody.length > 0 && checkBody.length < 12000;
|
|
630
|
+
const lowerSnippet = checkBody.slice(0, 8000).toLowerCase();
|
|
631
|
+
const hasAnyCaptchaKeyword = lowerSnippet.includes('captcha') ||
|
|
632
|
+
lowerSnippet.includes('recaptcha') ||
|
|
633
|
+
lowerSnippet.includes('hcaptcha') ||
|
|
634
|
+
lowerSnippet.includes('turnstile') ||
|
|
635
|
+
lowerSnippet.includes('challenge') ||
|
|
636
|
+
lowerSnippet.includes('cloudflare') ||
|
|
637
|
+
lowerSnippet.includes('datadome') ||
|
|
638
|
+
lowerSnippet.includes('perimeterx') ||
|
|
639
|
+
lowerSnippet.includes('funcaptcha') ||
|
|
640
|
+
lowerSnippet.includes('arkose') ||
|
|
641
|
+
lowerSnippet.includes('sitekey') ||
|
|
642
|
+
lowerSnippet.includes('just a moment') ||
|
|
643
|
+
lowerSnippet.includes('human verification');
|
|
606
644
|
const hasHtmlTags = /<html|<head|<body|<script|<meta/i.test(checkBody);
|
|
607
|
-
const challengeTitleOrText =
|
|
645
|
+
const challengeTitleOrText = hasAnyCaptchaKeyword ||
|
|
646
|
+
lowerSnippet.includes('attention required') ||
|
|
647
|
+
lowerSnippet.includes('verify you are human') ||
|
|
648
|
+
lowerSnippet.includes('security check');
|
|
608
649
|
if (isTinyBody && challengeTitleOrText && hasHtmlTags) {
|
|
609
650
|
addMatch(matches, 'generic', 0.6, 'Tiny HTML response with challenge-like text');
|
|
610
651
|
}
|
|
@@ -616,24 +657,38 @@ export function detectCaptcha(response, body) {
|
|
|
616
657
|
addMatch(matches, provider, confidence, description);
|
|
617
658
|
}
|
|
618
659
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
660
|
+
if (hasAnyCaptchaKeyword || !(response.status >= 200 && response.status < 300)) {
|
|
661
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_SCRIPT_PATTERNS) {
|
|
662
|
+
if (pattern.test(checkBody)) {
|
|
663
|
+
addMatch(matches, provider, confidence, description);
|
|
664
|
+
}
|
|
622
665
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
666
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
667
|
+
if (pattern.test(checkBody)) {
|
|
668
|
+
addMatch(matches, provider, confidence, description);
|
|
669
|
+
}
|
|
627
670
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
671
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
672
|
+
if (pattern.test(checkBody)) {
|
|
673
|
+
addMatch(matches, provider, confidence, description);
|
|
674
|
+
}
|
|
632
675
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
676
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
677
|
+
if (pattern.test(checkBody)) {
|
|
678
|
+
addMatch(matches, provider, confidence, description);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_PATTERNS) {
|
|
682
|
+
if (pattern.test(checkBody)) {
|
|
683
|
+
addMatch(matches, provider, confidence, description);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (/<script[^>]*src=["'][^"']*(?:challenges\.cloudflare\.com|recaptcha\/api\.js|hcaptcha\.com\/1\/api\.js|challenges\.perimeterx\.)[^"']*["'][^>]*>/i.test(checkBody.slice(0, 120_000))) {
|
|
687
|
+
addMatch(matches, 'cloudflare', 0.6, 'Known challenge script reference found in HTML');
|
|
688
|
+
}
|
|
689
|
+
if ((response.status >= 500 && response.status < 600) &&
|
|
690
|
+
/cloudflare|challenge|verification|blocked|security/i.test(checkBody.slice(0, 3000))) {
|
|
691
|
+
addMatch(matches, 'generic', 0.2, 'Server challenge-like text on error response');
|
|
637
692
|
}
|
|
638
693
|
}
|
|
639
694
|
if (response.status === 403 || response.status === 503) {
|
|
@@ -653,18 +708,6 @@ export function detectCaptcha(response, body) {
|
|
|
653
708
|
/refresh|challenge|verify|captcha|javascript challenge|cloudflare|bot/i.test((location || ''))) {
|
|
654
709
|
addMatch(matches, 'generic', 0.67, 'Redirect location indicates bot challenge flow');
|
|
655
710
|
}
|
|
656
|
-
if (/<script[^>]*src=["'][^"']*(?:challenges\.cloudflare\.com|recaptcha\/api\.js|hcaptcha\.com\/1\/api\.js|challenges\.perimeterx\.)[^"']*["'][^>]*>/i.test(checkBody.slice(0, 120_000))) {
|
|
657
|
-
addMatch(matches, 'cloudflare', 0.6, 'Known challenge script reference found in HTML');
|
|
658
|
-
}
|
|
659
|
-
if ((response.status >= 500 && response.status < 600) &&
|
|
660
|
-
/cloudflare|challenge|verification|blocked|security/i.test(checkBody.slice(0, 3000))) {
|
|
661
|
-
addMatch(matches, 'generic', 0.2, 'Server challenge-like text on error response');
|
|
662
|
-
}
|
|
663
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_PATTERNS) {
|
|
664
|
-
if (pattern.test(checkBody)) {
|
|
665
|
-
addMatch(matches, provider, confidence, description);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
711
|
}
|
|
669
712
|
const result = scoreByProvider(matches);
|
|
670
713
|
if (result.confidence >= 0.5 || hasCaptchaHeader) {
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "recker",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.96-next.402197e",
|
|
4
4
|
"description": "Multi-Protocol SDK for the AI Era - HTTP, WebSocket, DNS, FTP, SFTP, Telnet, HLS unified with AI providers and MCP tools",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|