| 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
|
|
78085
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
83
|
|
|
4
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
3376
|
|
|
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
|
|
|
|
|
|
|
my $RE_JS = qr/enable\s+javascript|please\s+enable\s+js|requires?\s+javascript/i; |
|
16
|
|
|
|
|
|
|
my $RE_BLOCK = qr/access\s+denied|checking\s+your\s+browser|are\s+you\s+(?:a\s+)?human|verify\s+you\s+are\s+human|unusual\s+traffic/i; |
|
17
|
|
|
|
|
|
|
my $RE_CAPTCHA = qr/(?:\b(?:re)?captcha\b|hcaptcha|g-recaptcha|cf-turnstile)/i; |
|
18
|
|
|
|
|
|
|
my $RE_WALL = qr/cf-chl|cf_chl|__cf_|datadome|perimeterx|px-captcha|akamai|incapsula|imperva/i; |
|
19
|
|
|
|
|
|
|
my $RE_TITLE = qr/^\s*just\s+a\s+moment|^\s*attention\s+required|^\s*access\s+denied/i; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# A WAF / bot-management gate (Cloudflare, DataDome, PerimeterX, Akamai) often |
|
22
|
|
|
|
|
|
|
# does not embed a widget into the requested page -- it REDIRECTS to a dedicated |
|
23
|
|
|
|
|
|
|
# challenge URL. reCAPTCHA / hCaptcha redirects land on the provider's own |
|
24
|
|
|
|
|
|
|
# verification endpoint. We key purely on the final (post-redirect) URL's |
|
25
|
|
|
|
|
|
|
# host+path matching a known challenge endpoint: a real content page's |
|
26
|
|
|
|
|
|
|
# final_url never contains /cdn-cgi/challenge etc., so URL equality with the |
|
27
|
|
|
|
|
|
|
# requested URL is irrelevant (and checking it would false-positive on cosmetic |
|
28
|
|
|
|
|
|
|
# http->https / www<->apex / trailing-slash redirects). |
|
29
|
|
|
|
|
|
|
my $RE_CHALLENGE_CAPTCHA = qr{ |
|
30
|
|
|
|
|
|
|
(?:www\.)?google\.com/recaptcha # reCAPTCHA verification endpoint |
|
31
|
|
|
|
|
|
|
| /recaptcha/api # reCAPTCHA api2/anchor frame |
|
32
|
|
|
|
|
|
|
| \bhcaptcha\.com\b # hCaptcha challenge host |
|
33
|
|
|
|
|
|
|
}ix; |
|
34
|
|
|
|
|
|
|
my $RE_CHALLENGE_WALL = qr{ |
|
35
|
|
|
|
|
|
|
/cdn-cgi/challenge # Cloudflare managed challenge |
|
36
|
|
|
|
|
|
|
| __cf_chl # Cloudflare challenge query/path token |
|
37
|
|
|
|
|
|
|
| /challenge-platform/ # Cloudflare challenge-platform asset |
|
38
|
|
|
|
|
|
|
| datadome # DataDome (host or path) |
|
39
|
|
|
|
|
|
|
| geo\.captcha-delivery\.com # DataDome captcha delivery host |
|
40
|
|
|
|
|
|
|
| /px/captcha # PerimeterX captcha path |
|
41
|
|
|
|
|
|
|
| perimeterx # PerimeterX (host or path) |
|
42
|
|
|
|
|
|
|
}ix; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
45
|
|
|
|
|
|
|
# Content classification |
|
46
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub signals { |
|
49
|
129
|
|
|
129
|
1
|
11447
|
my ( $page, %opt ) = @_; |
|
50
|
129
|
100
|
|
|
|
210
|
my $min = defined $opt{min_markdown} ? $opt{min_markdown} : $MIN_MARKDOWN; |
|
51
|
129
|
|
50
|
|
|
229
|
$page ||= {}; |
|
52
|
129
|
|
50
|
|
|
231
|
my $md = $page->{markdown} // ''; |
|
53
|
129
|
|
100
|
|
|
431
|
my $html = ( $page->{raw_html} // $page->{html} // '' ); |
|
|
|
|
100
|
|
|
|
|
|
54
|
129
|
|
100
|
|
|
255
|
my $title = $page->{title} // ''; |
|
55
|
129
|
|
50
|
|
|
209
|
my $code = $page->{status_code} // 0; |
|
56
|
|
|
|
|
|
|
# The post-redirect URL, falling back to the requested URL when the normalized |
|
57
|
|
|
|
|
|
|
# page omits it. Either may be absent (signals() is also called on bare test |
|
58
|
|
|
|
|
|
|
# hashes) -- when so, the challenge-URL checks below simply find no match and |
|
59
|
|
|
|
|
|
|
# no signal is raised. Never warns/dies on missing keys. |
|
60
|
129
|
|
66
|
|
|
290
|
my $final = $page->{final_url} // $page->{url} // ''; |
|
|
|
|
50
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Content volume is the master signal. A bot-wall / JS-shell / captcha gate |
|
63
|
|
|
|
|
|
|
# REPLACES the page content -- it is, by definition, thin. So every signal |
|
64
|
|
|
|
|
|
|
# derived from VISIBLE rendered text (the markdown) is only trustworthy on a |
|
65
|
|
|
|
|
|
|
# thin page: on a content-rich page those same words are incidental mentions |
|
66
|
|
|
|
|
|
|
# (a footer "enable JavaScript", an article quoting "unusual traffic", a |
|
67
|
|
|
|
|
|
|
# privacy note about reCAPTCHA) and must NOT discard a successful scrape -- |
|
68
|
|
|
|
|
|
|
# body words can never prove a scrape was impossible once we hold the content. |
|
69
|
|
|
|
|
|
|
# STRUCTURAL fingerprints are exempt: WAF tokens in the HTML markup |
|
70
|
|
|
|
|
|
|
# (__cf_chl, datadome), a "Just a moment" , or a redirect whose |
|
71
|
|
|
|
|
|
|
# final_url is a known challenge endpoint -- a real content page never carries |
|
72
|
|
|
|
|
|
|
# those, regardless of size. |
|
73
|
129
|
|
|
|
|
158
|
my $thin = length($md) < $min; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# 'blocked' is a bot-wall fingerprint, not HTTP status (status lives on the |
|
76
|
|
|
|
|
|
|
# http_error axis, so a bare 403 reads http_403 while a Cloudflare body reads |
|
77
|
|
|
|
|
|
|
# bot_wall_detected). The visible-text match ($RE_BLOCK) only counts on a thin |
|
78
|
|
|
|
|
|
|
# page; the structural arms (WAF tokens in HTML, "Just a moment" title, |
|
79
|
|
|
|
|
|
|
# redirect to a challenge URL) stand alone. |
|
80
|
129
|
|
100
|
|
|
1427
|
my $blocked = |
|
81
|
|
|
|
|
|
|
( $thin && $md =~ $RE_BLOCK ) |
|
82
|
|
|
|
|
|
|
|| ( $html =~ $RE_WALL ) |
|
83
|
|
|
|
|
|
|
|| ( $title =~ $RE_TITLE ) |
|
84
|
|
|
|
|
|
|
|| ( $final =~ $RE_CHALLENGE_WALL ); |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# 'captcha' is a captcha *wall*, not an incidental widget or mention: |
|
87
|
|
|
|
|
|
|
# * thin page + any marker (markdown OR html) -> wall. A near-empty page that |
|
88
|
|
|
|
|
|
|
# carries a captcha marker is a JS-rendered gate (real content never loaded). |
|
89
|
|
|
|
|
|
|
# * redirect to a CAPTCHA provider's own verification endpoint |
|
90
|
|
|
|
|
|
|
# (google.com/recaptcha, hcaptcha.com) -> wall. The final_url left the |
|
91
|
|
|
|
|
|
|
# origin and landed on the captcha provider; size-independent. |
|
92
|
|
|
|
|
|
|
# * rich page + marker (markdown OR html-only) -> NOT a wall. A cookie-banner |
|
93
|
|
|
|
|
|
|
# reCAPTCHA note, an embedded comment-form widget, a Turnstile login box -- |
|
94
|
|
|
|
|
|
|
# the real content is present, so the marker is incidental. |
|
95
|
129
|
|
100
|
|
|
881
|
my $captcha = |
|
96
|
|
|
|
|
|
|
( $thin && ( $md =~ $RE_CAPTCHA || $html =~ $RE_CAPTCHA ) ) |
|
97
|
|
|
|
|
|
|
|| ( $final =~ $RE_CHALLENGE_CAPTCHA ); |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
return { |
|
100
|
|
|
|
|
|
|
# A thin JS shell whose only text is "enable JavaScript" -- the real content |
|
101
|
|
|
|
|
|
|
# never rendered. A rich page that merely mentions JavaScript is already |
|
102
|
|
|
|
|
|
|
# rendered, so the match is incidental. |
|
103
|
|
|
|
|
|
|
js_required => ( $thin && $md =~ $RE_JS ) ? 1 : 0, |
|
104
|
|
|
|
|
|
|
blocked => $blocked ? 1 : 0, |
|
105
|
|
|
|
|
|
|
captcha => $captcha ? 1 : 0, |
|
106
|
|
|
|
|
|
|
thin_html => $thin ? 1 : 0, |
|
107
|
129
|
100
|
100
|
|
|
1005
|
http_error => ( $code >= 500 || $SOFT_FAIL{$code} ) ? 1 : 0, |
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
}; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub is_good { |
|
113
|
53
|
|
|
53
|
1
|
268766
|
my ( $page, %opt ) = @_; |
|
114
|
53
|
50
|
|
|
|
108
|
return 0 unless ref $page eq 'HASH'; |
|
115
|
53
|
100
|
100
|
|
|
134
|
return 0 if defined $page->{success} && !$page->{success}; |
|
116
|
52
|
|
50
|
|
|
102
|
my $code = $page->{status_code} // 0; |
|
117
|
52
|
100
|
66
|
|
|
210
|
return 0 if $code && ( $code >= 500 || $SOFT_FAIL{$code} ); |
|
|
|
|
66
|
|
|
|
|
|
118
|
51
|
|
|
|
|
76
|
my $sig = signals( $page, %opt ); |
|
119
|
51
|
100
|
66
|
|
|
251
|
return 0 if $sig->{js_required} || $sig->{blocked} || $sig->{captcha} || $sig->{thin_html}; |
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
120
|
33
|
|
|
|
|
89
|
return 1; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Most specific reason first. |
|
125
|
|
|
|
|
|
|
sub why_failed { |
|
126
|
26
|
|
|
26
|
1
|
386
|
my ( $page, %opt ) = @_; |
|
127
|
26
|
50
|
|
|
|
78
|
return 'empty' unless ref $page eq 'HASH'; |
|
128
|
26
|
|
|
|
|
44
|
my $sig = signals( $page, %opt ); |
|
129
|
26
|
100
|
|
|
|
73
|
return 'captcha' if $sig->{captcha}; |
|
130
|
23
|
100
|
|
|
|
93
|
return 'bot_wall_detected' if $sig->{blocked}; |
|
131
|
17
|
100
|
|
|
|
35
|
return 'js_required' if $sig->{js_required}; |
|
132
|
16
|
|
50
|
|
|
27
|
my $code = $page->{status_code} // 0; |
|
133
|
16
|
100
|
100
|
|
|
73
|
return "http_$code" if $code && ( $code >= 500 || $SOFT_FAIL{$code} ); |
|
|
|
|
66
|
|
|
|
|
|
134
|
14
|
100
|
|
|
|
166
|
return 'thin_content' if $sig->{thin_html}; |
|
135
|
3
|
|
|
|
|
19
|
return undef; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
140
|
|
|
|
|
|
|
# Service detection |
|
141
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _probe_ua { |
|
144
|
0
|
|
|
0
|
|
0
|
my ( $ua, $timeout ) = @_; |
|
145
|
0
|
0
|
|
|
|
0
|
return $ua if $ua; |
|
146
|
0
|
|
|
|
|
0
|
require LWP::UserAgent; |
|
147
|
0
|
|
0
|
|
|
0
|
return LWP::UserAgent->new( agent => "WWW-Crawl4AI/$VERSION", timeout => ( $timeout // 5 ) ); |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub probe_cloakbrowser { |
|
151
|
1
|
|
|
1
|
1
|
989
|
my ( $cdp_url, %opt ) = @_; |
|
152
|
1
|
50
|
33
|
|
|
8
|
return 0 unless defined $cdp_url && length $cdp_url; |
|
153
|
0
|
|
|
|
|
0
|
( my $base = $cdp_url ) =~ s{/+$}{}; |
|
154
|
0
|
|
|
|
|
0
|
$base =~ s{\?.*$}{}; # strip CloakBrowser query params (fingerprint=...) |
|
155
|
0
|
|
|
|
|
0
|
my $ua = _probe_ua( $opt{ua}, $opt{timeout} ); |
|
156
|
0
|
|
|
|
|
0
|
my $res = $ua->get( $base . '/json/version' ); |
|
157
|
0
|
0
|
|
|
|
0
|
return $res->is_success ? 1 : 0; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub detect_proxy_env { |
|
162
|
1
|
|
50
|
1
|
1
|
8
|
return $ENV{CRAWL4AI_PROXY_URL} || undef; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
__END__ |