| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WWW::Crawl4AI::Detect; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: service detection and content-quality classification for Crawl4AI |
|
3
|
3
|
|
|
3
|
|
77142
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
110
|
|
|
4
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
|
3
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
2447
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.001'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Default: a result needs at least this many markdown characters to count. |
|
10
|
|
|
|
|
|
|
our $MIN_MARKDOWN = 500; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# HTTP status codes that mean "the target pushed back", not "transport broke". |
|
13
|
|
|
|
|
|
|
my %SOFT_FAIL = map { $_ => 1 } ( 401, 403, 429 ); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# A WAF / bot-management gate (Cloudflare, DataDome, PerimeterX, Akamai) often |
|
16
|
|
|
|
|
|
|
# does not embed a widget into the requested page -- it REDIRECTS to a dedicated |
|
17
|
|
|
|
|
|
|
# challenge URL. reCAPTCHA / hCaptcha redirects land on the provider's own |
|
18
|
|
|
|
|
|
|
# verification endpoint. We key purely on the final (post-redirect) URL's |
|
19
|
|
|
|
|
|
|
# host+path matching a known challenge endpoint: a real content page's |
|
20
|
|
|
|
|
|
|
# final_url never contains /cdn-cgi/challenge etc., so URL equality with the |
|
21
|
|
|
|
|
|
|
# requested URL is irrelevant (and checking it would false-positive on cosmetic |
|
22
|
|
|
|
|
|
|
# http->https / www<->apex / trailing-slash redirects). |
|
23
|
|
|
|
|
|
|
my $RE_CHALLENGE_CAPTCHA = qr{ |
|
24
|
|
|
|
|
|
|
(?:www\.)?google\.com/recaptcha # reCAPTCHA verification endpoint |
|
25
|
|
|
|
|
|
|
| /recaptcha/api # reCAPTCHA api2/anchor frame |
|
26
|
|
|
|
|
|
|
| \bhcaptcha\.com\b # hCaptcha challenge host |
|
27
|
|
|
|
|
|
|
}ix; |
|
28
|
|
|
|
|
|
|
my $RE_CHALLENGE_WALL = qr{ |
|
29
|
|
|
|
|
|
|
/cdn-cgi/challenge # Cloudflare managed challenge |
|
30
|
|
|
|
|
|
|
| __cf_chl # Cloudflare challenge query/path token |
|
31
|
|
|
|
|
|
|
| /challenge-platform/ # Cloudflare challenge-platform asset |
|
32
|
|
|
|
|
|
|
| datadome # DataDome (host or path) |
|
33
|
|
|
|
|
|
|
| geo\.captcha-delivery\.com # DataDome captcha delivery host |
|
34
|
|
|
|
|
|
|
| /px/captcha # PerimeterX captcha path |
|
35
|
|
|
|
|
|
|
| perimeterx # PerimeterX (host or path) |
|
36
|
|
|
|
|
|
|
}ix; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
39
|
|
|
|
|
|
|
# Content classification |
|
40
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub signals { |
|
43
|
132
|
|
|
132
|
1
|
9616
|
my ( $page, %opt ) = @_; |
|
44
|
132
|
100
|
|
|
|
206
|
my $min = defined $opt{min_markdown} ? $opt{min_markdown} : $MIN_MARKDOWN; |
|
45
|
132
|
|
50
|
|
|
199
|
$page ||= {}; |
|
46
|
132
|
|
50
|
|
|
209
|
my $md = $page->{markdown} // ''; |
|
47
|
132
|
|
50
|
|
|
176
|
my $code = $page->{status_code} // 0; |
|
48
|
|
|
|
|
|
|
# The post-redirect URL, falling back to the requested URL when the normalized |
|
49
|
|
|
|
|
|
|
# page omits it. Either may be absent (signals() is also called on bare test |
|
50
|
|
|
|
|
|
|
# hashes) -- when so, the challenge-URL checks below simply find no match and |
|
51
|
|
|
|
|
|
|
# no signal is raised. Never warns/dies on missing keys. |
|
52
|
132
|
|
66
|
|
|
359
|
my $final = $page->{final_url} // $page->{url} // ''; |
|
|
|
|
50
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Content volume is the master signal. A bot-wall / JS-shell / captcha gate |
|
55
|
|
|
|
|
|
|
# REPLACES the page content -- it is, by definition, thin. So a content-rich |
|
56
|
|
|
|
|
|
|
# page (>= $min markdown chars) that came back 200 IS the scrape: nothing in |
|
57
|
|
|
|
|
|
|
# its body text or may discard it. The body/title phrase heuristics |
|
58
|
|
|
|
|
|
|
# ($RE_BLOCK, $RE_JS, $RE_CAPTCHA body arms, $RE_WALL HTML-token, $RE_TITLE) |
|
59
|
|
|
|
|
|
|
# were removed in 0.005: on a thin page they were redundant (thin_html already |
|
60
|
|
|
|
|
|
|
# fails the page), and on a full page they were pure false-positives -- a |
|
61
|
|
|
|
|
|
|
# 386 KB article carrying Cloudflare's passive __cf_ beacon, or a legit |
|
62
|
|
|
|
|
|
|
# "Access Denied" , was wrongly thrown away. The ONLY size-independent |
|
63
|
|
|
|
|
|
|
# block signals kept are the fingerprints a real content page can never carry: |
|
64
|
|
|
|
|
|
|
# an HTTP push-back status, or a redirect whose final_url is a known WAF / |
|
65
|
|
|
|
|
|
|
# captcha challenge endpoint (the page physically left the origin). |
|
66
|
132
|
|
|
|
|
174
|
my $thin = length($md) < $min; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# 'blocked' / 'captcha' fire only when the post-redirect final_url is a known |
|
69
|
|
|
|
|
|
|
# WAF / captcha challenge endpoint. Not HTTP status -- that lives on the |
|
70
|
|
|
|
|
|
|
# http_error axis. A site that soft-blocks us by serving one identical |
|
71
|
|
|
|
|
|
|
# interstitial for every URL (200, no redirect) is caught one level up, by the |
|
72
|
|
|
|
|
|
|
# caller comparing markdown across the fetched pages -- not here, per-page. |
|
73
|
132
|
100
|
|
|
|
681
|
my $blocked = ( $final =~ $RE_CHALLENGE_WALL ) ? 1 : 0; |
|
74
|
132
|
100
|
|
|
|
668
|
my $captcha = ( $final =~ $RE_CHALLENGE_CAPTCHA ) ? 1 : 0; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return { |
|
77
|
|
|
|
|
|
|
blocked => $blocked, |
|
78
|
|
|
|
|
|
|
captcha => $captcha, |
|
79
|
|
|
|
|
|
|
thin_html => $thin ? 1 : 0, |
|
80
|
132
|
100
|
100
|
|
|
651
|
http_error => ( $code >= 500 || $SOFT_FAIL{$code} ) ? 1 : 0, |
|
|
|
100
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
}; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub is_good { |
|
86
|
57
|
|
|
57
|
1
|
241232
|
my ( $page, %opt ) = @_; |
|
87
|
57
|
50
|
|
|
|
119
|
return 0 unless ref $page eq 'HASH'; |
|
88
|
57
|
100
|
100
|
|
|
141
|
return 0 if defined $page->{success} && !$page->{success}; |
|
89
|
56
|
|
50
|
|
|
78
|
my $code = $page->{status_code} // 0; |
|
90
|
56
|
100
|
66
|
|
|
200
|
return 0 if $code && ( $code >= 500 || $SOFT_FAIL{$code} ); |
|
|
|
|
66
|
|
|
|
|
|
91
|
55
|
|
|
|
|
78
|
my $sig = signals( $page, %opt ); |
|
92
|
55
|
100
|
100
|
|
|
211
|
return 0 if $sig->{blocked} || $sig->{captcha} || $sig->{thin_html}; |
|
|
|
|
100
|
|
|
|
|
|
93
|
34
|
|
|
|
|
109
|
return 1; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Most specific reason first. |
|
98
|
|
|
|
|
|
|
sub why_failed { |
|
99
|
26
|
|
|
26
|
1
|
38
|
my ( $page, %opt ) = @_; |
|
100
|
26
|
50
|
|
|
|
63
|
return 'empty' unless ref $page eq 'HASH'; |
|
101
|
26
|
|
|
|
|
43
|
my $sig = signals( $page, %opt ); |
|
102
|
26
|
100
|
|
|
|
50
|
return 'captcha' if $sig->{captcha}; |
|
103
|
25
|
100
|
|
|
|
108
|
return 'bot_wall_detected' if $sig->{blocked}; |
|
104
|
20
|
|
50
|
|
|
46
|
my $code = $page->{status_code} // 0; |
|
105
|
20
|
100
|
100
|
|
|
93
|
return "http_$code" if $code && ( $code >= 500 || $SOFT_FAIL{$code} ); |
|
|
|
|
66
|
|
|
|
|
|
106
|
18
|
100
|
|
|
|
178
|
return 'thin_content' if $sig->{thin_html}; |
|
107
|
4
|
|
|
|
|
17
|
return undef; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
112
|
|
|
|
|
|
|
# Service detection |
|
113
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _probe_ua { |
|
116
|
0
|
|
|
0
|
|
0
|
my ( $ua, $timeout ) = @_; |
|
117
|
0
|
0
|
|
|
|
0
|
return $ua if $ua; |
|
118
|
0
|
|
|
|
|
0
|
require LWP::UserAgent; |
|
119
|
0
|
|
0
|
|
|
0
|
return LWP::UserAgent->new( agent => "WWW-Crawl4AI/$VERSION", timeout => ( $timeout // 5 ) ); |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub probe_cloakbrowser { |
|
123
|
1
|
|
|
1
|
1
|
719
|
my ( $cdp_url, %opt ) = @_; |
|
124
|
1
|
50
|
33
|
|
|
16
|
return 0 unless defined $cdp_url && length $cdp_url; |
|
125
|
0
|
|
|
|
|
0
|
( my $base = $cdp_url ) =~ s{/+$}{}; |
|
126
|
0
|
|
|
|
|
0
|
$base =~ s{\?.*$}{}; # strip CloakBrowser query params (fingerprint=...) |
|
127
|
0
|
|
|
|
|
0
|
my $ua = _probe_ua( $opt{ua}, $opt{timeout} ); |
|
128
|
0
|
|
|
|
|
0
|
my $res = $ua->get( $base . '/json/version' ); |
|
129
|
0
|
0
|
|
|
|
0
|
return $res->is_success ? 1 : 0; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub detect_proxy_env { |
|
134
|
1
|
|
50
|
1
|
1
|
4
|
return $ENV{CRAWL4AI_PROXY_URL} || undef; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |