Impersonate.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Impersonate/Cert.pm ↑ Summary
1 package Proxy::Impersonate;
2 2 ▶use v5.10; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Curl::Impersonate 0.03;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Proxy::Impersonate::Cert;
10 use Proxy::Impersonate::Connection;
11  
12 our $VERSION = '0.03';
13  
14 sub new {
15 my ($class, %o) = @_;
16 # A client that resets mid-response makes a write raise SIGPIPE, whose default
17 # disposition terminates the process -- and since this proxy runs in-process
18 # with EV::WebKit, that would take the browser down before the graceful abort
19 # path runs. Ignore it (standard for a network server).
20 $SIG{PIPE} = 'IGNORE';
21 my $target = $o{impersonate} or Carp::croak('new: impersonate target required');
22 my ($host, $port) = ($o{listen} // '127.0.0.1:0') =~ /^(.+):(\d+)$/
23 or Carp::croak('new: listen must be host:port');
24 my $temp_dir = defined $o{cert_dir} ? undef : File::Temp->newdir;
25 my $cert_dir = $o{cert_dir} // "$temp_dir";
26  
27 socket(my $srv, PF_INET, SOCK_STREAM, 0) or Carp::croak("socket: $!");
28 setsockopt($srv, SOL_SOCKET, SO_REUSEADDR, 1);
29 bind($srv, sockaddr_in($port, inet_aton($host))) or Carp::croak("bind: $!");
30 listen($srv, 128) or Carp::croak("listen: $!");
31 $srv->blocking(0);
32 my $bound = (sockaddr_in(getsockname($srv)))[0];
33  
34 my $self = bless {
35 srv => $srv,
36 port => $bound,
37 cert => Proxy::Impersonate::Cert->new(cert_dir => $cert_dir),
38 cert_dir=> $cert_dir,
39 temp_dir=> $temp_dir,
40 target => $target,
41 # Is the impersonate target a Chromium family? Decides whether the per-
42 # request Accept is synthesized to Chrome's value or WebKit's own (Safari-
43 # correct) value is forwarded -- so a Safari target does not present a Chrome
44 # Accept. Chromium engines (Chrome/Edge/Chromium) send the same Accept; a
45 # non-matching Chromium target name can force it with an explicit chrome => .
46 chrome => (($o{chrome} // ($target =~ /chrome|chromium|edge/i)) ? 1 : 0),
47 timeout => $o{timeout} // 30,
48 verify => defined $o{verify} ? $o{verify} : 1,
49 follow => $o{follow_redirects} // 0,
50 override => $o{override_headers} // {}, # forced on every upstream request
51 on_request => $o{on_request}, # optional per-request interception hook
52 on_response => $o{on_response}, # optional per-response hook
53 high_entropy => $o{high_entropy_headers} // {}, # added per-host after Accept-CH
54 hints => {}, # host -> { hint-name => 1 } (Accept-CH seen)
55 multi => Curl::Impersonate->multi,
56 multi_state => {},
57 conns => {},
58 cio => {}, # curl fd => EV::io watcher
59 }, $class;
60  
61 $self->_wire_multi;
62 # weaken $self in the accept watcher: otherwise $self -> {aw} -> closure -> $self
63 # is a cycle that keeps the daemon (and its listen socket) alive -- still
64 # accepting -- after the caller drops its reference without stop()/shutdown().
65 # The documented usage (hold the ref, then stop/shutdown) is unaffected.
66 Scalar::Util::weaken(my $wself = $self);
67 $self->{aw} = EV::io(fileno($srv), EV::READ, sub { $wself && $wself->_accept });
68 return $self;
69 }
70  
71 sub port { $_[0]{port} }
72 sub cert_dir { $_[0]{cert_dir} }
73  
74 # Drive Curl::Impersonate::Multi from the EV loop via its socket-action surface.
75 # The stored callbacks close over $self and the multi; weaken both so they do NOT
76 # form a reference cycle (multi -> callback -> $self -> $self->{multi} = multi)
77 # that would keep the multi alive and prevent its DESTROY (curl_multi_cleanup)
78 # from ever running -- a per-session leak of the handle + its upstream sockets.
79 sub _wire_multi {
80 my ($self) = @_;
81 my $m = $self->{multi};
82 my $state = $self->{multi_state};
83 Scalar::Util::weaken(my $wself = $self);
84 Scalar::Util::weaken(my $wm = $m);
85 $m->set_socket_callback(sub {
86 my ($fd, $what) = @_;
87 return unless $wself && $wm && !$wself->{_shutdown};
88 if ($what == 4) { delete $wself->{cio}{$fd}; return } # CURL_POLL_REMOVE
89 my $events = 0;
90 $events |= EV::READ if $what & 1; # POLL_IN
91 $events |= EV::WRITE if $what & 2; # POLL_OUT
92 $wself->{cio}{$fd} = EV::io($fd, $events, sub {
93 my (undef, $revents) = @_;
94 return unless $wm;
95 my $ev = 0;
96 $ev |= 1 if $revents & EV::READ;
97 $ev |= 2 if $revents & EV::WRITE;
98 $ev = 3 unless $ev; # fallback: try both
99 local $state->{busy} = 1;
100 $wm->socket_action($fd, $ev);
101 });
102 });
103 $m->set_timer_callback(sub {
104 my ($ms) = @_;
105 return unless $wself && $wm && !$wself->{_shutdown};
106 if ($ms < 0) { undef $wself->{ctimer}; return }
107 $wself->{ctimer} = EV::timer($ms / 1000, 0, sub {
108 local $state->{busy} = 1;
109 $wm->socket_action(-1, 0) if $wm;
110 });
111 });
112 }
113  
114 sub _accept {
115 my ($self) = @_;
116 Scalar::Util::weaken(my $wself = $self);
117 while (accept(my $cli, $self->{srv})) {
118 my $conn = Proxy::Impersonate::Connection->new(
119 fd => $cli,
120 cert => $self->{cert},
121 multi => $self->{multi},
122 multi_state => $self->{multi_state},
123 override => $self->{override},
124 on_request => $self->{on_request},
125 on_response => $self->{on_response},
126 chrome => $self->{chrome},
127 high_entropy => $self->{high_entropy},
128 hints => $self->{hints},
129 make_handle => sub {
130 Curl::Impersonate->new(
131 impersonate => $wself->{target},
132 verify => $wself->{verify},
133 timeout => $wself->{timeout},
134 decode => 1,
135 ($wself->{follow} ? (follow_redirects => 1) : ()),
136 );
137 },
138 on_close => sub { delete $wself->{conns}{ $_[0] } if $wself },
139 );
140 $self->{conns}{$conn} = $conn;
141 $conn->start;
142 }
143 }
144  
145 sub run { EV::run }
146 sub stop {
147 my ($self) = @_;
148 undef $self->{aw};
149 close delete $self->{srv} if $self->{srv};
150 EV::break(EV::BREAK_ALL);
151 }
152  
153 # In-process teardown: stop accepting, close active connections, release the
154 # curl_multi wiring. Unlike stop(), does NOT EV::break -- the caller's shared
155 # loop keeps running (e.g. EV::WebKit's browser loop).
156 sub shutdown {
157 my ($self) = @_;
158 $self->{_shutdown} = 1;
159 undef $self->{aw};
160 close delete $self->{srv} if $self->{srv};
161 my @conns = values %{ $self->{conns} || {} }; # snapshot: _close mutates {conns} via on_close
162 $self->{conns} = {};
163 for my $c (@conns) { eval { $c->_close } }
164 $self->{cio} = {};
165 undef $self->{ctimer};
166 undef $self->{multi};
167 delete @{$self}{qw(on_request on_response)};
168 return $self;
169 }
170  
171 sub DESTROY { $_[0]->shutdown }
172  
173 1;
174  
175 __END__
« Impersonate/Cert.pm ↑ Summary