Curl-Impersonate
view release on metacpan or search on metacpan
lib/Curl/Impersonate.pm view on Meta::CPAN
# asynchronous (concurrent upstreams)
my $m = Curl::Impersonate->multi;
for my $url (@urls) {
my $h = Curl::Impersonate->new(impersonate => 'chrome131');
$m->add($h, { url => $url }, sub {
my ($res, $err) = @_;
$err ? warn($err) : print $res->{status}, "\n";
});
}
$m->perform_blocking;
=head1 DESCRIPTION
Wraps C<libcurl-impersonate> (a patched libcurl built against BoringSSL, via
L<Alien::curlimpersonate>) so a request carries a chosen real browser's TLS
(JA3/JA4) and HTTP/2 (Akamai) connection fingerprint. Origin certificate
verification stays on by default; impersonation changes the handshake shape,
not whether the peer is verified.
This is an HTTP client, not a full browser. It reproduces the B<connection>
fingerprint (TLS + HTTP/2); it does not run JavaScript, and HTTP/3 and
WebSockets are out of scope in this release.
=head1 REQUIREMENTS
Requires L<Alien::curlimpersonate> 0.02 or newer, which builds
C<libcurl-impersonate> (a patched curl plus BoringSSL) from source at install
time. That build needs a C/C++ toolchain, cmake, ninja, go and patch -- see
that module for the details. No system C<libcurl-impersonate> is used.
=head1 METHODS
=head2 new
my $c = Curl::Impersonate->new(%opt);
Creates a client (one reusable connection handle). Options:
=over 4
=item impersonate => $target
A browser profile name (see L</targets>), e.g. C<'chrome131'>. Applies that
browser's TLS/HTTP2 fingerprint and, unless C<default_headers> is false, its
default header set. An unknown target croaks.
=item default_headers => $bool
Whether to also install the target's default request headers. Default true.
=item timeout => $seconds
Whole-request timeout. There is no default: libcurl waits indefinitely, so a
blackholed address or a server that accepts and never answers will hang the
caller. Set one for anything talking to the open internet.
=item verify => $bool
TLS peer/host verification. Default true. Set false only for testing against
self-signed endpoints.
=item follow_redirects => $bool
Follow C<3xx> redirects. Default false. libcurl bounds the chain itself, so a
redirect loop ends with C<Number of redirects hit maximum amount> rather than
spinning.
=item decode => $bool
Decompress the response body. libcurl decodes whatever it was built with --
here gzip, deflate, br and zstd -- and C<< $res->{body} >> is the decoded
bytes. Off by default, so the body arrives exactly as the origin sent it.
This does not change the request on the wire. The C<Accept-Encoding> the
impersonate target installs is a custom header, and libcurl lets a custom
header replace the one it would generate itself, so the fingerprint is
untouched; only the response side differs.
C<< $res->{headers} >> still reports the origin's C<Content-Encoding> and
C<Content-Length>, which now describe the bytes before decoding. Anything
forwarding this response onward must drop both.
=item proxy => $url
Route requests through a proxy, e.g. C<'http://127.0.0.1:8080'> or
C<'socks5h://host:1080'>. Credentials go in the URL. Note that a proxy which
terminates TLS presents its own fingerprint, not the impersonated one; to keep
the fingerprint intact the proxy must tunnel with C<CONNECT>.
=back
=head2 get
my $res = $c->get($url);
Convenience for a GET C<request>.
=head2 request
my $res = $c->request(
method => 'GET', # default GET
url => $url, # required
headers => \%headers, # optional; values are strings
body => $bytes, # optional request body
);
A header name or value containing CR, LF or NUL croaks: libcurl would pass such
a line through verbatim and the origin would read it as extra headers. An undef
value removes a header the impersonation profile would otherwise send.
Performs the request and returns a hashref. On success:
{ status => $int, headers => \%response_headers, body => $bytes,
url => $effective_url }
C<url> is where the request actually ended up, which differs from the one asked
for when C<follow_redirects> sent it elsewhere.
Response header names are lower-cased; a header that appears more than once
(e.g. C<set-cookie>) is kept as an arrayref of its values. On a transport-level
( run in 1.592 second using v1.01-cache-2.11-cpan-eb134014566 )