recker 1.0.95-next.0a5359d → 1.0.95-next.42384f4
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 +1 -9
- package/dist/browser/scrape/spider.js +5 -53
- package/dist/browser/utils/block-detector.js +40 -83
- package/dist/scrape/spider.d.ts +1 -9
- package/dist/scrape/spider.js +5 -53
- package/dist/utils/block-detector.js +40 -83
- 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;
|
|
15
15
|
concurrency?: number;
|
|
16
16
|
timeout?: number;
|
|
17
17
|
delay?: number;
|
|
@@ -63,11 +63,6 @@ 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
|
-
};
|
|
71
66
|
deduplicateContent?: boolean;
|
|
72
67
|
resume?: boolean;
|
|
73
68
|
strategy?: 'bfs' | 'dfs';
|
|
@@ -196,14 +191,12 @@ export declare class Spider {
|
|
|
196
191
|
private _queueSize;
|
|
197
192
|
private _resultCount;
|
|
198
193
|
private baseHost;
|
|
199
|
-
private baseRootDomain;
|
|
200
194
|
private running;
|
|
201
195
|
private aborted;
|
|
202
196
|
private abortController;
|
|
203
197
|
private pendingCount;
|
|
204
198
|
private domainRequestTimestamps;
|
|
205
199
|
private contentHashes;
|
|
206
|
-
private domainAvgResponseTime;
|
|
207
200
|
private blockedDomains;
|
|
208
201
|
private curlTransport;
|
|
209
202
|
private curlAvailable;
|
|
@@ -232,7 +225,6 @@ export declare class Spider {
|
|
|
232
225
|
private waitForDomainPenalty;
|
|
233
226
|
private registerDomainBlock;
|
|
234
227
|
private registerDomainSuccess;
|
|
235
|
-
private updateAutoThrottle;
|
|
236
228
|
private getCaptchaRetryMultiplier;
|
|
237
229
|
private registerDomainChallenge;
|
|
238
230
|
private getRetryWait;
|
|
@@ -108,30 +108,15 @@ function normalizeUrl(urlStr) {
|
|
|
108
108
|
return urlStr;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
function
|
|
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) {
|
|
111
|
+
function shouldCrawl(url, baseHost, options) {
|
|
119
112
|
try {
|
|
120
113
|
const parsed = new URL(url);
|
|
121
114
|
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
122
115
|
return false;
|
|
123
116
|
}
|
|
124
117
|
const hostname = parsed.hostname.replace(/^www\./, '');
|
|
125
|
-
|
|
126
|
-
|
|
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;
|
|
118
|
+
if (options.sameDomain !== false && hostname !== baseHost) {
|
|
119
|
+
return false;
|
|
135
120
|
}
|
|
136
121
|
const skipExtensions = [
|
|
137
122
|
'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico',
|
|
@@ -208,14 +193,12 @@ export class Spider {
|
|
|
208
193
|
_queueSize = 0;
|
|
209
194
|
_resultCount = 0;
|
|
210
195
|
baseHost = '';
|
|
211
|
-
baseRootDomain = '';
|
|
212
196
|
running = false;
|
|
213
197
|
aborted = false;
|
|
214
198
|
abortController = new AbortController();
|
|
215
199
|
pendingCount = 0;
|
|
216
200
|
domainRequestTimestamps = new Map();
|
|
217
201
|
contentHashes = new Map();
|
|
218
|
-
domainAvgResponseTime = new Map();
|
|
219
202
|
blockedDomains = new Set();
|
|
220
203
|
curlTransport = null;
|
|
221
204
|
curlAvailable = false;
|
|
@@ -302,7 +285,6 @@ export class Spider {
|
|
|
302
285
|
extract: extractSchema,
|
|
303
286
|
parserOptions: options.parserOptions,
|
|
304
287
|
domainRateLimit: options.domainRateLimit,
|
|
305
|
-
autoThrottle: options.autoThrottle ?? false,
|
|
306
288
|
deduplicateContent: options.deduplicateContent ?? false,
|
|
307
289
|
resume: options.resume ?? false,
|
|
308
290
|
strategy: options.strategy ?? 'bfs',
|
|
@@ -344,7 +326,6 @@ export class Spider {
|
|
|
344
326
|
const normalizedStart = normalizeUrl(startUrl);
|
|
345
327
|
const baseUrl = new URL(normalizedStart).origin;
|
|
346
328
|
this.baseHost = new URL(normalizedStart).hostname.replace(/^www\./, '');
|
|
347
|
-
this.baseRootDomain = getRootDomain(this.baseHost);
|
|
348
329
|
if (!this.options.resume) {
|
|
349
330
|
await this.crawlQueue.clear();
|
|
350
331
|
await this.crawlStorage.clear();
|
|
@@ -353,7 +334,6 @@ export class Spider {
|
|
|
353
334
|
this._resultCount = 0;
|
|
354
335
|
this.domainRequestTimestamps.clear();
|
|
355
336
|
this.contentHashes.clear();
|
|
356
|
-
this.domainAvgResponseTime.clear();
|
|
357
337
|
}
|
|
358
338
|
else {
|
|
359
339
|
this._queueSize = await this.crawlQueue.size();
|
|
@@ -484,14 +464,6 @@ export class Spider {
|
|
|
484
464
|
: new Set(pages.map(r => r.url));
|
|
485
465
|
await this.crawlQueue.close?.();
|
|
486
466
|
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();
|
|
495
467
|
return {
|
|
496
468
|
startUrl: normalizedStart,
|
|
497
469
|
pages,
|
|
@@ -817,9 +789,6 @@ export class Spider {
|
|
|
817
789
|
const isHighQualitySuccess = response.status < 400 && !detection.blocked && !hasCaptchaSignal;
|
|
818
790
|
if (isHighQualitySuccess) {
|
|
819
791
|
this.registerDomainSuccess(hostname);
|
|
820
|
-
if (timings?.total) {
|
|
821
|
-
this.updateAutoThrottle(hostname, timings.total);
|
|
822
|
-
}
|
|
823
792
|
}
|
|
824
793
|
if (!shouldRetry || attempt === maxAttempts - 1) {
|
|
825
794
|
if (proxyUrl && this.proxyAdapter?.reportResult) {
|
|
@@ -1113,7 +1082,7 @@ export class Spider {
|
|
|
1113
1082
|
if (!link.href)
|
|
1114
1083
|
continue;
|
|
1115
1084
|
const normalized = normalizeUrl(link.href);
|
|
1116
|
-
if (!shouldCrawl(normalized, this.baseHost, this.options
|
|
1085
|
+
if (!shouldCrawl(normalized, this.baseHost, this.options))
|
|
1117
1086
|
continue;
|
|
1118
1087
|
candidateUrls.push(normalized);
|
|
1119
1088
|
candidates.push({ url: normalized, depth: item.depth + 1 });
|
|
@@ -1224,7 +1193,6 @@ export class Spider {
|
|
|
1224
1193
|
consecutiveUndiciFailures: 0,
|
|
1225
1194
|
lastTransport: 'undici',
|
|
1226
1195
|
lastCaptchaConfidence: 0,
|
|
1227
|
-
autoThrottleDelay: 0,
|
|
1228
1196
|
};
|
|
1229
1197
|
this.domainStates.set(hostname, next);
|
|
1230
1198
|
return next;
|
|
@@ -1327,9 +1295,7 @@ export class Spider {
|
|
|
1327
1295
|
async waitForDomainPenalty(hostname) {
|
|
1328
1296
|
const state = this.getOrCreateDomainState(hostname);
|
|
1329
1297
|
const now = Date.now();
|
|
1330
|
-
const
|
|
1331
|
-
const throttleDelay = state.autoThrottleDelay ?? 0;
|
|
1332
|
-
const delay = Math.max(penaltyDelay, throttleDelay);
|
|
1298
|
+
const delay = Math.max(state.penaltyUntil, state.challengeCooldownUntil) - now;
|
|
1333
1299
|
if (delay > 0) {
|
|
1334
1300
|
await sleep(delay, this.abortController.signal);
|
|
1335
1301
|
}
|
|
@@ -1357,20 +1323,6 @@ export class Spider {
|
|
|
1357
1323
|
state.lastCaptchaProvider = undefined;
|
|
1358
1324
|
}
|
|
1359
1325
|
}
|
|
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
|
-
}
|
|
1374
1326
|
getCaptchaRetryMultiplier(provider) {
|
|
1375
1327
|
if (!provider)
|
|
1376
1328
|
return 1.2;
|
|
@@ -526,42 +526,18 @@ 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
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
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
|
-
}
|
|
529
|
+
const challengeHint = /just a moment|attention required|access denied|checking your browser|security check|human verification|suspicious activity|captcha|recaptcha|hcaptcha|turnstile|datadome|cloudflare/i.test(checkBody.slice(0, 8000));
|
|
530
|
+
for (const { pattern, reason, confidence, description } of BLOCK_PATTERNS) {
|
|
531
|
+
if (isLongBody && response.status === 200 && confidence < 0.85 && !challengeHint) {
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (pattern.test(checkBody)) {
|
|
535
|
+
results.push({
|
|
536
|
+
blocked: true,
|
|
537
|
+
reason,
|
|
538
|
+
confidence,
|
|
539
|
+
description,
|
|
540
|
+
});
|
|
565
541
|
}
|
|
566
542
|
}
|
|
567
543
|
if (body.length < 5000 &&
|
|
@@ -627,25 +603,8 @@ export function detectCaptcha(response, body) {
|
|
|
627
603
|
if (body) {
|
|
628
604
|
const checkBody = body.length < 120_000 ? body : body.slice(0, 120_000);
|
|
629
605
|
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');
|
|
644
606
|
const hasHtmlTags = /<html|<head|<body|<script|<meta/i.test(checkBody);
|
|
645
|
-
const challengeTitleOrText =
|
|
646
|
-
lowerSnippet.includes('attention required') ||
|
|
647
|
-
lowerSnippet.includes('verify you are human') ||
|
|
648
|
-
lowerSnippet.includes('security check');
|
|
607
|
+
const challengeTitleOrText = /just a moment|attention required|human verification|verify you are human|security check/i.test(checkBody);
|
|
649
608
|
if (isTinyBody && challengeTitleOrText && hasHtmlTags) {
|
|
650
609
|
addMatch(matches, 'generic', 0.6, 'Tiny HTML response with challenge-like text');
|
|
651
610
|
}
|
|
@@ -657,38 +616,24 @@ export function detectCaptcha(response, body) {
|
|
|
657
616
|
addMatch(matches, provider, confidence, description);
|
|
658
617
|
}
|
|
659
618
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
addMatch(matches, provider, confidence, description);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
667
|
-
if (pattern.test(checkBody)) {
|
|
668
|
-
addMatch(matches, provider, confidence, description);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
672
|
-
if (pattern.test(checkBody)) {
|
|
673
|
-
addMatch(matches, provider, confidence, description);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
677
|
-
if (pattern.test(checkBody)) {
|
|
678
|
-
addMatch(matches, provider, confidence, description);
|
|
679
|
-
}
|
|
619
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_SCRIPT_PATTERNS) {
|
|
620
|
+
if (pattern.test(checkBody)) {
|
|
621
|
+
addMatch(matches, provider, confidence, description);
|
|
680
622
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
623
|
+
}
|
|
624
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
625
|
+
if (pattern.test(checkBody)) {
|
|
626
|
+
addMatch(matches, provider, confidence, description);
|
|
685
627
|
}
|
|
686
|
-
|
|
687
|
-
|
|
628
|
+
}
|
|
629
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
630
|
+
if (pattern.test(checkBody)) {
|
|
631
|
+
addMatch(matches, provider, confidence, description);
|
|
688
632
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
633
|
+
}
|
|
634
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
635
|
+
if (pattern.test(checkBody)) {
|
|
636
|
+
addMatch(matches, provider, confidence, description);
|
|
692
637
|
}
|
|
693
638
|
}
|
|
694
639
|
if (response.status === 403 || response.status === 503) {
|
|
@@ -708,6 +653,18 @@ export function detectCaptcha(response, body) {
|
|
|
708
653
|
/refresh|challenge|verify|captcha|javascript challenge|cloudflare|bot/i.test((location || ''))) {
|
|
709
654
|
addMatch(matches, 'generic', 0.67, 'Redirect location indicates bot challenge flow');
|
|
710
655
|
}
|
|
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
|
+
}
|
|
711
668
|
}
|
|
712
669
|
const result = scoreByProvider(matches);
|
|
713
670
|
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;
|
|
15
15
|
concurrency?: number;
|
|
16
16
|
timeout?: number;
|
|
17
17
|
delay?: number;
|
|
@@ -63,11 +63,6 @@ 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
|
-
};
|
|
71
66
|
deduplicateContent?: boolean;
|
|
72
67
|
resume?: boolean;
|
|
73
68
|
strategy?: 'bfs' | 'dfs';
|
|
@@ -196,14 +191,12 @@ export declare class Spider {
|
|
|
196
191
|
private _queueSize;
|
|
197
192
|
private _resultCount;
|
|
198
193
|
private baseHost;
|
|
199
|
-
private baseRootDomain;
|
|
200
194
|
private running;
|
|
201
195
|
private aborted;
|
|
202
196
|
private abortController;
|
|
203
197
|
private pendingCount;
|
|
204
198
|
private domainRequestTimestamps;
|
|
205
199
|
private contentHashes;
|
|
206
|
-
private domainAvgResponseTime;
|
|
207
200
|
private blockedDomains;
|
|
208
201
|
private curlTransport;
|
|
209
202
|
private curlAvailable;
|
|
@@ -232,7 +225,6 @@ export declare class Spider {
|
|
|
232
225
|
private waitForDomainPenalty;
|
|
233
226
|
private registerDomainBlock;
|
|
234
227
|
private registerDomainSuccess;
|
|
235
|
-
private updateAutoThrottle;
|
|
236
228
|
private getCaptchaRetryMultiplier;
|
|
237
229
|
private registerDomainChallenge;
|
|
238
230
|
private getRetryWait;
|
package/dist/scrape/spider.js
CHANGED
|
@@ -108,30 +108,15 @@ function normalizeUrl(urlStr) {
|
|
|
108
108
|
return urlStr;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
function
|
|
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) {
|
|
111
|
+
function shouldCrawl(url, baseHost, options) {
|
|
119
112
|
try {
|
|
120
113
|
const parsed = new URL(url);
|
|
121
114
|
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
122
115
|
return false;
|
|
123
116
|
}
|
|
124
117
|
const hostname = parsed.hostname.replace(/^www\./, '');
|
|
125
|
-
|
|
126
|
-
|
|
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;
|
|
118
|
+
if (options.sameDomain !== false && hostname !== baseHost) {
|
|
119
|
+
return false;
|
|
135
120
|
}
|
|
136
121
|
const skipExtensions = [
|
|
137
122
|
'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico',
|
|
@@ -208,14 +193,12 @@ export class Spider {
|
|
|
208
193
|
_queueSize = 0;
|
|
209
194
|
_resultCount = 0;
|
|
210
195
|
baseHost = '';
|
|
211
|
-
baseRootDomain = '';
|
|
212
196
|
running = false;
|
|
213
197
|
aborted = false;
|
|
214
198
|
abortController = new AbortController();
|
|
215
199
|
pendingCount = 0;
|
|
216
200
|
domainRequestTimestamps = new Map();
|
|
217
201
|
contentHashes = new Map();
|
|
218
|
-
domainAvgResponseTime = new Map();
|
|
219
202
|
blockedDomains = new Set();
|
|
220
203
|
curlTransport = null;
|
|
221
204
|
curlAvailable = false;
|
|
@@ -302,7 +285,6 @@ export class Spider {
|
|
|
302
285
|
extract: extractSchema,
|
|
303
286
|
parserOptions: options.parserOptions,
|
|
304
287
|
domainRateLimit: options.domainRateLimit,
|
|
305
|
-
autoThrottle: options.autoThrottle ?? false,
|
|
306
288
|
deduplicateContent: options.deduplicateContent ?? false,
|
|
307
289
|
resume: options.resume ?? false,
|
|
308
290
|
strategy: options.strategy ?? 'bfs',
|
|
@@ -344,7 +326,6 @@ export class Spider {
|
|
|
344
326
|
const normalizedStart = normalizeUrl(startUrl);
|
|
345
327
|
const baseUrl = new URL(normalizedStart).origin;
|
|
346
328
|
this.baseHost = new URL(normalizedStart).hostname.replace(/^www\./, '');
|
|
347
|
-
this.baseRootDomain = getRootDomain(this.baseHost);
|
|
348
329
|
if (!this.options.resume) {
|
|
349
330
|
await this.crawlQueue.clear();
|
|
350
331
|
await this.crawlStorage.clear();
|
|
@@ -353,7 +334,6 @@ export class Spider {
|
|
|
353
334
|
this._resultCount = 0;
|
|
354
335
|
this.domainRequestTimestamps.clear();
|
|
355
336
|
this.contentHashes.clear();
|
|
356
|
-
this.domainAvgResponseTime.clear();
|
|
357
337
|
}
|
|
358
338
|
else {
|
|
359
339
|
this._queueSize = await this.crawlQueue.size();
|
|
@@ -484,14 +464,6 @@ export class Spider {
|
|
|
484
464
|
: new Set(pages.map(r => r.url));
|
|
485
465
|
await this.crawlQueue.close?.();
|
|
486
466
|
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();
|
|
495
467
|
return {
|
|
496
468
|
startUrl: normalizedStart,
|
|
497
469
|
pages,
|
|
@@ -817,9 +789,6 @@ export class Spider {
|
|
|
817
789
|
const isHighQualitySuccess = response.status < 400 && !detection.blocked && !hasCaptchaSignal;
|
|
818
790
|
if (isHighQualitySuccess) {
|
|
819
791
|
this.registerDomainSuccess(hostname);
|
|
820
|
-
if (timings?.total) {
|
|
821
|
-
this.updateAutoThrottle(hostname, timings.total);
|
|
822
|
-
}
|
|
823
792
|
}
|
|
824
793
|
if (!shouldRetry || attempt === maxAttempts - 1) {
|
|
825
794
|
if (proxyUrl && this.proxyAdapter?.reportResult) {
|
|
@@ -1113,7 +1082,7 @@ export class Spider {
|
|
|
1113
1082
|
if (!link.href)
|
|
1114
1083
|
continue;
|
|
1115
1084
|
const normalized = normalizeUrl(link.href);
|
|
1116
|
-
if (!shouldCrawl(normalized, this.baseHost, this.options
|
|
1085
|
+
if (!shouldCrawl(normalized, this.baseHost, this.options))
|
|
1117
1086
|
continue;
|
|
1118
1087
|
candidateUrls.push(normalized);
|
|
1119
1088
|
candidates.push({ url: normalized, depth: item.depth + 1 });
|
|
@@ -1224,7 +1193,6 @@ export class Spider {
|
|
|
1224
1193
|
consecutiveUndiciFailures: 0,
|
|
1225
1194
|
lastTransport: 'undici',
|
|
1226
1195
|
lastCaptchaConfidence: 0,
|
|
1227
|
-
autoThrottleDelay: 0,
|
|
1228
1196
|
};
|
|
1229
1197
|
this.domainStates.set(hostname, next);
|
|
1230
1198
|
return next;
|
|
@@ -1327,9 +1295,7 @@ export class Spider {
|
|
|
1327
1295
|
async waitForDomainPenalty(hostname) {
|
|
1328
1296
|
const state = this.getOrCreateDomainState(hostname);
|
|
1329
1297
|
const now = Date.now();
|
|
1330
|
-
const
|
|
1331
|
-
const throttleDelay = state.autoThrottleDelay ?? 0;
|
|
1332
|
-
const delay = Math.max(penaltyDelay, throttleDelay);
|
|
1298
|
+
const delay = Math.max(state.penaltyUntil, state.challengeCooldownUntil) - now;
|
|
1333
1299
|
if (delay > 0) {
|
|
1334
1300
|
await sleep(delay, this.abortController.signal);
|
|
1335
1301
|
}
|
|
@@ -1357,20 +1323,6 @@ export class Spider {
|
|
|
1357
1323
|
state.lastCaptchaProvider = undefined;
|
|
1358
1324
|
}
|
|
1359
1325
|
}
|
|
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
|
-
}
|
|
1374
1326
|
getCaptchaRetryMultiplier(provider) {
|
|
1375
1327
|
if (!provider)
|
|
1376
1328
|
return 1.2;
|
|
@@ -526,42 +526,18 @@ 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
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
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
|
-
}
|
|
529
|
+
const challengeHint = /just a moment|attention required|access denied|checking your browser|security check|human verification|suspicious activity|captcha|recaptcha|hcaptcha|turnstile|datadome|cloudflare/i.test(checkBody.slice(0, 8000));
|
|
530
|
+
for (const { pattern, reason, confidence, description } of BLOCK_PATTERNS) {
|
|
531
|
+
if (isLongBody && response.status === 200 && confidence < 0.85 && !challengeHint) {
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (pattern.test(checkBody)) {
|
|
535
|
+
results.push({
|
|
536
|
+
blocked: true,
|
|
537
|
+
reason,
|
|
538
|
+
confidence,
|
|
539
|
+
description,
|
|
540
|
+
});
|
|
565
541
|
}
|
|
566
542
|
}
|
|
567
543
|
if (body.length < 5000 &&
|
|
@@ -627,25 +603,8 @@ export function detectCaptcha(response, body) {
|
|
|
627
603
|
if (body) {
|
|
628
604
|
const checkBody = body.length < 120_000 ? body : body.slice(0, 120_000);
|
|
629
605
|
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');
|
|
644
606
|
const hasHtmlTags = /<html|<head|<body|<script|<meta/i.test(checkBody);
|
|
645
|
-
const challengeTitleOrText =
|
|
646
|
-
lowerSnippet.includes('attention required') ||
|
|
647
|
-
lowerSnippet.includes('verify you are human') ||
|
|
648
|
-
lowerSnippet.includes('security check');
|
|
607
|
+
const challengeTitleOrText = /just a moment|attention required|human verification|verify you are human|security check/i.test(checkBody);
|
|
649
608
|
if (isTinyBody && challengeTitleOrText && hasHtmlTags) {
|
|
650
609
|
addMatch(matches, 'generic', 0.6, 'Tiny HTML response with challenge-like text');
|
|
651
610
|
}
|
|
@@ -657,38 +616,24 @@ export function detectCaptcha(response, body) {
|
|
|
657
616
|
addMatch(matches, provider, confidence, description);
|
|
658
617
|
}
|
|
659
618
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
addMatch(matches, provider, confidence, description);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
667
|
-
if (pattern.test(checkBody)) {
|
|
668
|
-
addMatch(matches, provider, confidence, description);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
672
|
-
if (pattern.test(checkBody)) {
|
|
673
|
-
addMatch(matches, provider, confidence, description);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
677
|
-
if (pattern.test(checkBody)) {
|
|
678
|
-
addMatch(matches, provider, confidence, description);
|
|
679
|
-
}
|
|
619
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_SCRIPT_PATTERNS) {
|
|
620
|
+
if (pattern.test(checkBody)) {
|
|
621
|
+
addMatch(matches, provider, confidence, description);
|
|
680
622
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
623
|
+
}
|
|
624
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_FORM_PATTERNS) {
|
|
625
|
+
if (pattern.test(checkBody)) {
|
|
626
|
+
addMatch(matches, provider, confidence, description);
|
|
685
627
|
}
|
|
686
|
-
|
|
687
|
-
|
|
628
|
+
}
|
|
629
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_DOM_MARKERS) {
|
|
630
|
+
if (pattern.test(checkBody)) {
|
|
631
|
+
addMatch(matches, provider, confidence, description);
|
|
688
632
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
633
|
+
}
|
|
634
|
+
for (const { pattern, provider, confidence, description } of CAPTCHA_NOSCRIPT_PATTERNS) {
|
|
635
|
+
if (pattern.test(checkBody)) {
|
|
636
|
+
addMatch(matches, provider, confidence, description);
|
|
692
637
|
}
|
|
693
638
|
}
|
|
694
639
|
if (response.status === 403 || response.status === 503) {
|
|
@@ -708,6 +653,18 @@ export function detectCaptcha(response, body) {
|
|
|
708
653
|
/refresh|challenge|verify|captcha|javascript challenge|cloudflare|bot/i.test((location || ''))) {
|
|
709
654
|
addMatch(matches, 'generic', 0.67, 'Redirect location indicates bot challenge flow');
|
|
710
655
|
}
|
|
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
|
+
}
|
|
711
668
|
}
|
|
712
669
|
const result = scoreByProvider(matches);
|
|
713
670
|
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.95-next.
|
|
3
|
+
"version": "1.0.95-next.42384f4",
|
|
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",
|