VPNDetection.pm

filter: Statementstmt 99.7% 402 / 403 Branchbran 92.2% 179 / 194 Conditioncond 82.3% 121 / 147 MC/DCmcdc 69.6% 55 / 79 Subroutinesub 98.7% 80 / 81 Podpod 66.6% 8 / 12 totaltotal 92.2% 845 / 916 CC 154 SCAR 22.7
CC
154
Cov
94%
_nonconforming
31.4
new
19.0
lookup_batch_p
13.1
CRAP 9.7
↑ Summary VPNDetection/Oauth.pm »
1 package VPNDetection;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Mojo::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use VPNDetection::Bogon ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use VPNDetection::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use VPNDetection::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use VPNDetection::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use VPNDetection::Oauth;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use VPNDetection::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 our $VERSION = '3.16.0';
23 7 our @EXPORT_OK = ('is_bogon');
24  
25 7 ▶use constant DEFAULT_BASE_URL => 'https://api.vpndetection.io';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # The most addresses POST /batch takes in one call; a larger batch is sent in
28 # chunks of this size.
29 7 ▶use constant BATCH_MAX => 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Seconds before the first retry; each later one waits twice the one before.
32 7 ▶use constant BACKOFF_BASE => 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # The longest Retry-After honored, in seconds: 2**31 - 1 ms, about 24.8 days,
35 # the same bound as the .NET SDK's.
36 7 ▶use constant LONGEST_WAIT => 2_147_483.647;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 7 my %OPTIONS = map { $_ => 1 } qw(
39 api_key base_url cache_size cache_ttl concurrency retries timeout ua
40 );
41  
42 # The shapes of the answers, from the pinned spec's schemas: a member it requires
43 # and never sends as null is required, a nullable or optional one may be absent
44 # or null, and every one present has the spec's type at every depth, a `date` or
45 # `date-time` RFC 3339's on a day that exists. A detail is as optional as the plan
46 # makes it, so `{}` is one.
47 my $VPN_DETAIL = [fields => [
48 7 (map { [$_ => ['string']] } qw(provider provider_name confidence method)), [last_seen => ['date']],
49 ]];
50 7 my $CLASS_DETAIL = [fields => [(map { [$_ => ['string']] } qw(provider provider_name confidence)), [last_seen => ['date']]]];
51 my $PROXY_DETAIL = [fields => [
52 14 [provider => ['string']], [provider_name => ['string']], (map { [$_ => ['date']] } qw(first_seen last_seen)),
53 7 (map { [$_ => ['integer']] } qw(hits hits_days_pct providers_num)),
54 ]];
55 7 my $PUBPROXY_DETAIL = [fields => [
56 [entry => ['boolean']], [exit => ['boolean']], [protocols => [list => ['string']]],
57 [num_ports => ['integer']], [num_proxies => ['integer']], [last_seen => ['date']],
58 [last_usable => ['string']],
59 ]];
60 # Where the address is and the AS announcing it carry every member whenever they
61 # are present.
62 7 my $LOCATION = [fields => [map { [$_ => ['string'], 1] } @VPNDetection::Result::LOCATION]];
63 7 my $ASN = [fields => [[asn => ['integer'], 1], map { [$_ => ['string'], 1] } qw(name domain type prefix)]];
64 our $LOOKUP = [fields => [
65 [ip => ['string'], 1], [is_vpn => ['boolean'], 1], [is_bogon => ['boolean']],
66 70 (map { [$_ => ['boolean']] } qw(is_hosting is_relay is_tor is_cdn is_resproxy is_dcproxy is_mobproxy is_pubproxy trial_expired cap_reached)),
67 [vpn => $VPN_DETAIL],
68 28 (map { [$_ => $CLASS_DETAIL] } qw(hosting relay tor cdn)),
69 7 (map { [$_ => $PROXY_DETAIL] } qw(resproxy dcproxy mobproxy)),
70 [pubproxy => $PUBPROXY_DETAIL],
71 [location => $LOCATION], [asn => $ASN],
72 ]];
73 7 our $BATCH_ERROR = [fields => [[status => ['integer'], 1], [error => ['string'], 1]]];
74 # Each entry is judged alone, by _lookup_chunk_p.
75 7 our $BATCH = [fields => [[results => [map_of => ['any']], 1], [errors => [map_of => ['any']], 1]]];
76 7 our $ENTITLEMENT = [fields => [
77 [org_id => ['string'], 1],
78 [apikey => [fields => [[id => ['string'], 1], [expires => ['date-time']], [allowed_cidrs => [list => ['string']], 1]]], 1],
79 [plan => [fields => [[key => ['string'], 1], [name => ['string'], 1], [tier => ['string'], 1]]], 1],
80 [usage => [fields => [
81 [requests => ['integer'], 1], [quota => ['integer'], 1], [hard_limit => ['integer']],
82 [window_start => ['date-time'], 1], [window_end => ['date-time'], 1],
83 ]], 1],
84 ]];
85  
86 799 ▶sub new {
Subroutine new: called CC=19 SCAR=29.4
Pod documented
87 799 my ($class, %args) = @_;
88 799 my @unknown = sort grep { !$OPTIONS{$_} } keys %args;
89 799 ▶ Carp::croak("VPNDetection->new: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 798 799
90  
91 798 ▶ my $concurrency = defined $args{concurrency} ? $args{concurrency} : 8;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'concurrency'} ? : 11 787 798
92 798 ▶ my $retries = defined $args{retries} ? $args{retries} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'retries'} ? : 591 207 798
93 798 ▶ my $cache_size = defined $args{cache_size} ? $args{cache_size} : 10_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'cache_size'} ? : 518 280 798
94 798 ▶ Carp::croak('VPNDetection->new: concurrency must be a whole number, 1 or more') unless _is_count($concurrency, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($concurrency, 1) 9 789 798
95 789 ▶ Carp::croak('VPNDetection->new: retries must be a whole number, 0 or more') unless _is_count($retries, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($retries, 0) 7 782 789
96 # Mojo arms a negative or non-numeric bound as a timer that fires at once, so
97 # every call would fail as a network error.
98 782 ▶ my $timeout = defined $args{timeout} ? $args{timeout} : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout'} ? : 41 741 782
99 782 ▶ Carp::croak('VPNDetection->new: timeout must be a number of seconds, 0 or more')
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($timeout) and $timeout >= 0 2 780 782
Condition100%3/3
&Scalar::Util::looks_like_number($timeout) and $timeout >= 0
!ll&&!rl&&r
11780
MC/DC100%2/2
&Scalar::Util::looks_like_number($timeout) and $timeout >= 0
&Scalar::Util::looks_like_number($timeout) $timeout >= 0
Truth table100%3/3
A and B
A= &Scalar::Util::looks_like_number($timeout)B= $timeout >= 0
ABresult
0X0
100
111
100 unless Scalar::Util::looks_like_number($timeout) && $timeout >= 0;
101  
102 # Trimmed, so a key read from a file loses its newline, and blanks alone are
103 # no key. Mojo refuses a CR or LF in a header with a raw string from every
104 # call and sends any other control character, so one left inside is refused
105 # here, naming the cause and not the key.
106 780 my $api_key = $args{api_key};
107 780 ▶ if (defined $api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api_key) 525 255 780
108 525 $api_key =~ s/\A\s+|\s+\z//g;
109 525 ▶ $api_key = undef unless length $api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $api_key 3 522 525
110 }
111 780 ▶ Carp::croak('VPNDetection->new: api_key holds a control character')
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/ 3 777 780
Condition100%3/3
defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/
!ll&&!rl&&r
2585193
MC/DC100%2/2
defined $api_key and $api_key =~ /[\x00-\x1f\x7f]/
defined $api_key $api_key =~ /[\x00-\x1f\x7f]/
Truth table100%3/3
A and B
A= defined $api_keyB= $api_key =~ /[\x00-\x1f\x7f]/
ABresult
0X0
100
111
112 if defined $api_key && $api_key =~ /[\x00-\x1f\x7f]/;
113 # Mojo writes a header as bytes, so a character past U+00FF dies in the
114 # reactor and every call fails after its whole timeout with nothing sent.
115 777 ▶ Carp::croak('VPNDetection->new: api_key holds a character past U+00FF')
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key and $api_key =~ /[^\x00-\xff]/ 2 775 777
Condition100%3/3
defined $api_key and $api_key =~ /[^\x00-\xff]/
!ll&&!rl&&r
2585172
MC/DC100%2/2
defined $api_key and $api_key =~ /[^\x00-\xff]/
defined $api_key $api_key =~ /[^\x00-\xff]/
Truth table100%3/3
A and B
A= defined $api_keyB= $api_key =~ /[^\x00-\xff]/
ABresult
0X0
100
111
116 if defined $api_key && $api_key =~ /[^\x00-\xff]/;
117  
118 my $self = bless {
119 api_key => $api_key,
120 base_url => _base_url($args{base_url}),
121 concurrency => 0 + $concurrency,
122 retries => 0 + $retries,
123 # Numified: Mojo arms a bound only when it is true, and '0.0', '00', '0e0'
124 # and '0 but true' are true strings, each a timer that fires at once.
125 timeout => 0 + $timeout,
126 cache => $cache_size > 0 ? VPNDetection::Cache->new(
127 max => $cache_size,
128 ttl => defined $args{cache_ttl} ? $args{cache_ttl} : 3600,
129 ) : undef,
130 # The addresses with a request in flight, each a promise its waiters
131 # take the answer from (see _board).
132 flights => {},
133 775 ▶ ua => $args{ua} || Mojo::UserAgent->new,
Branch: 2 branches
BranchTrueFalseTotal
$cache_size > 0 ? : 258 517 775
defined $args{'cache_ttl'} ? : 1 257 258
Condition66%2/3
$args{'ua'} || 'Mojo::UserAgent'->new
l!l&&r!l&&!r
47710
MC/DC0%0/2
$args{'ua'} || 'Mojo::UserAgent'->new
$args{'ua'} 'Mojo::UserAgent'->new
Truth table66%2/3
A || B
A= $args{'ua'}B= 'Mojo::UserAgent'->new
ABresult
1X1
011
000
134 }, $class;
135  
136 # Mojo::UserAgent does not follow redirects by default, but MOJO_MAX_REDIRECTS
137 # in the environment turns that on for every agent in the process. Setting it
138 # here is what stops the database download's 302 being chased into a
139 # multi-gigabyte transfer, on a machine whose environment we do not own.
140 775 $self->{ua}->max_redirects(0);
141 775 $self->{ua}->request_timeout($self->{timeout});
142 775 $self->{ua}->transactor->name("vpndetection-perl/$VERSION");
143 775 return $self;
144 }
145  
146 # Both a method and an exportable function, so the address is the LAST argument
147 # either way: is_bogon($ip) and $client->is_bogon($ip) reach the same code.
148 565 ▶sub is_bogon {
Subroutine is_bogon: called CC=3 SCAR=12.5
Pod documented
149 565 my $ip = pop;
150 565 ▶ Carp::croak('is_bogon: expected an IP address') if !defined $ip || ref $ip;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip or ref $ip 0 565 565
Condition33%1/3
not defined $ip or ref $ip
l!l&&r!l&&!r
00565
MC/DC0%0/2
not defined $ip or ref $ip
not defined $ip ref $ip
Truth table33%1/3
not(A) or B
A= not defined $ipB= ref $ip
ABresult
1X1
011
000
151 565 return VPNDetection::Bogon::is_bogon($ip);
152 }
153  
154 # Classify one address. A bogon is answered locally and never reaches the
155 # network; everything else is served, then cached for this client.
156 365 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
Pod documented
157 365 my $self = shift;
158 365 $self->_assert_blocking_ok('lookup');
159 364 return $self->_wait($self->lookup_p(@_));
160 }
161  
162 401 ▶sub lookup_p {
Subroutine lookup_p: called CC=9 SCAR=22.0
Pod undocumented
163 401 my ($self, $ip, %options) = @_;
164 401 ▶ Carp::croak('lookup: expected an IP address') if !defined $ip || !length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 0 401 401
Condition33%1/3
defined $ip and length $ip
!ll&&!rl&&r
00401
MC/DC0%0/2
defined $ip and length $ip
defined $ip length $ip
Truth table33%1/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
165 401 $self->_check_options('lookup', \%options, 'retries', 'timeout');
166 # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
167 378 $ip = VPNDetection::Bogon::unmapped($ip);
168  
169 378 ▶ return Mojo::Promise->resolve(VPNDetection::Bogon::bogon_result($ip))
Branch: 1 branch
BranchTrueFalseTotal
if VPNDetection::Bogon::is_bogon($ip) 4 374 378
170 if VPNDetection::Bogon::is_bogon($ip);
171  
172 374 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 78 296 374
173 78 my $hit = $self->{cache}->get($ip);
174 78 ▶ return Mojo::Promise->resolve($hit) if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 4 74 78
175 # A miss for an address already in flight, for a lookup or a batch,
176 # takes that request's answer rather than sending its own.
177 74 ▶ return $self->{flights}{$ip}->then(sub { shift }) if $self->{flights}{$ip};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flights"}{$ip} 24 50 74
178 }
179  
180 346 my $flight = $self->_board($ip);
181 346 my $url = $self->_url('/' . Mojo::Util::url_escape($ip, '^A-Za-z0-9\-._~:'));
182 346 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 2 344 346
183 # Anything that dies before a request is under way lands the flight too, or
184 # every later lookup of the address would wait on it forever (3.8.0).
185 346 ▶ my $sent = eval { $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $LOOKUP) }) };
Subroutine __ANON__: called CC=1 SCAR=0.0
186 346 ▶ if (!$sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 2 344 346
187 2 my $error = $@;
188 2 $self->_land($ip, $flight, 0, $error);
189 2 die $error;
190 }
191 return $sent->then(
192 104 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
193 104 my $result = VPNDetection::Result->from_wire(shift);
194 # Only a served answer is cached, and before it lands, so a miss
195 # after the flight is gone finds it. Errors never are, and bogons
196 # never reach this far.
197 104 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 33 71 104
198 104 $self->_land($ip, $flight, 1, $result);
199 104 return $result;
200 },
201 240 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
202 240 my $error = shift;
203 240 $self->_land($ip, $flight, 0, $error);
204 240 return Mojo::Promise->reject($error);
205 },
206 344 );
207 }
208  
209 # Concurrent misses for one address share ONE request: 3.3.2 sent one per
210 # caller. The first miss boards the address, and
211 # every miss after it, a batch's included, waits for that request instead. The
212 # request runs under the options of the call that led it; a failure reaches
213 # every waiter and is cached for none. Nothing is shared without a cache, since
214 # every lookup is then served. A Mojo::Promise cannot be canceled, so no waiter
215 # is ever left behind by a leader that gave up.
216 27895 ▶sub _board {
Subroutine _board: called CC=2 SCAR=6.9
217 27895 my ($self, $ip) = @_;
218 27895 ▶ return undef unless $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cache"} 27828 67 27895
219 67 my $flight = Mojo::Promise->new;
220 # Handled even when no waiter joined, so a failure is not warned about.
221 67 ▶ $flight->catch(sub { });
Subroutine __ANON__: called CC=1 SCAR=0.0
222 67 return $self->{flights}{$ip} = $flight;
223 }
224  
225 # Takes an address off the board and settles its flight for every waiter.
226 27895 ▶sub _land {
Subroutine _land: called CC=5 SCAR=16.7
227 27895 my ($self, $ip, $flight, $ok, $value) = @_;
228 27895 ▶ return unless $flight;
Branch: 1 branch
BranchTrueFalseTotal
unless $flight 27828 67 27895
229 delete $self->{flights}{$ip}
230 67 ▶ if $self->{flights}{$ip} && $self->{flights}{$ip} == $flight;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight 67 0 67
Condition33%1/3
$self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight
!ll&&!rl&&r
0067
MC/DC0%0/2
$self->{"flights"}{$ip} and $self->{"flights"}{$ip} == $flight
$self->{"flights"}{$ip} $self->{"flights"}{$ip} == $flight
Truth table33%1/3
A and B
A= $self->{"flights"}{$ip}B= $self->{"flights"}{$ip} == $flight
ABresult
0X0
100
111
231 67 ▶ return $ok ? $flight->resolve($value) : $flight->reject($value);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 45 22 67
232 }
233  
234 # The address our edge observed this client calling from. Deliberately not
235 # cached: the cache is keyed by address, and which address this is IS the
236 # question.
237 31 ▶sub my_ip {
Subroutine my_ip: called CC=1 SCAR=0.0
Pod documented
238 31 my $self = shift;
239 31 $self->_assert_blocking_ok('my_ip');
240 31 return $self->_wait($self->my_ip_p(@_));
241 }
242  
243 31 ▶sub my_ip_p {
Subroutine my_ip_p: called CC=2 SCAR=7.0
Pod undocumented
244 31 my ($self, %options) = @_;
245 31 $self->_check_options('my_ip', \%options, 'retries', 'timeout');
246  
247 24 my $url = $self->_url('/myip');
248 24 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 0 24 24
249 26 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $LOOKUP) })
Subroutine __ANON__: called CC=1 SCAR=0.0
250 24 ▶ ->then(sub { VPNDetection::Result->from_wire(shift) });
Subroutine __ANON__: called CC=1 SCAR=0.0
251 }
252  
253 # What this key is entitled to and what it has spent. Deliberately not cached:
254 # the whole point is the number, and a cached one is wrong within seconds.
255 89 ▶sub my_entitlement {
Subroutine my_entitlement: called CC=1 SCAR=0.0
Pod documented
256 89 my $self = shift;
257 89 $self->_assert_blocking_ok('my_entitlement');
258 89 return $self->_wait($self->my_entitlement_p(@_));
259 }
260  
261 89 ▶sub my_entitlement_p {
Subroutine my_entitlement_p: called CC=2 SCAR=7.0
Pod undocumented
262 89 my ($self, %options) = @_;
263 89 $self->_check_options('my_entitlement', \%options, 'retries', 'timeout');
264  
265 82 my $url = $self->_url('/api/v1/entitlement');
266 82 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 0 82 82
267 82 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, undef, $options{timeout}, undef, $ENTITLEMENT) });
Subroutine __ANON__: called CC=1 SCAR=0.0
268 }
269  
270 # Classify many addresses concurrently, keyed by address rather than positional,
271 # so duplicates collapse to one request and the caller never lines two lists up.
272 53 ▶sub lookup_batch {
Subroutine lookup_batch: called CC=1 SCAR=0.0
Pod documented
273 53 my $self = shift;
274 53 $self->_assert_blocking_ok('lookup_batch');
275 53 return $self->_wait($self->lookup_batch_p(@_));
276 }
277  
278 71 ▶sub lookup_batch_p {
Subroutine lookup_batch_p: called CC=13 SCAR=25.7
Pod undocumented
279 71 my ($self, $ips, %options) = @_;
280 71 ▶ Carp::croak('lookup_batch: expected an array reference of addresses')
Branch: 1 branch
BranchTrueFalseTotal
if ref $ips ne "ARRAY" 0 71 71
281 if ref $ips ne 'ARRAY';
282 71 $self->_check_options('lookup_batch', \%options, 'retries', 'concurrency', 'timeout');
283  
284 40 ▶ my $limit = defined $options{concurrency} ? $options{concurrency} : $self->{concurrency};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'concurrency'} ? : 3 37 40
285 40 ▶ my $retries = defined $options{retries} ? $options{retries} : $self->{retries};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'retries'} ? : 0 40 40
286  
287 # Bogons are answered locally and cached answers are reused; everything else
288 # goes to the batch endpoint in chunks of up to 1000 addresses, with at most
289 # `limit` chunks in flight. Keyed by address rather than positional, so
290 # duplicates in the input collapse to a single entry and the caller never has
291 # to line two lists up.
292 # An IPv4-mapped address is sent as the address it carries, once however
293 # many of its spellings were asked, and answered under each one asked.
294 40 ▶ my @asked = grep { defined && length } @$ips;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0027554
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
295 40 my %seen;
296 40 my @unique = grep { !$seen{$_}++ } map { VPNDetection::Bogon::unmapped($_) } @asked;
297 40 my %answers;
298 my @pending;
299 40 my %joined;
300 40 for my $ip (@unique) {
301 27553 ▶ if (VPNDetection::Bogon::is_bogon($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if (VPNDetection::Bogon::is_bogon($ip)) 2 27551 27553
302 2 $answers{$ip} = VPNDetection::Bogon::bogon_result($ip);
303 2 next;
304 }
305 27551 ▶ my $hit = $self->{cache} ? $self->{cache}->get($ip) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache'} ? : 19 27532 27551
306 27551 ▶ if ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit) 1 27550 27551
307 1 $answers{$ip} = $hit;
308 1 next;
309 }
310 # Already in flight, for a lookup or another batch: this batch takes
311 # that request's answer rather than sending the address again.
312 27550 ▶ if ($self->{cache} && $self->{flights}{$ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and $self->{"flights"}{$ip}) 1 27549 27550
Condition100%3/3
$self->{"cache"} and $self->{"flights"}{$ip}
!ll&&!rl&&r
27532171
MC/DC100%2/2
$self->{"cache"} and $self->{"flights"}{$ip}
$self->{"cache"} $self->{"flights"}{$ip}
Truth table100%3/3
A and B
A= $self->{"cache"}B= $self->{"flights"}{$ip}
ABresult
0X0
100
111
313 1 $joined{$ip} = $self->{flights}{$ip};
314 1 next;
315 }
316 27549 push @pending, $ip;
317 }
318 # Boarded before any chunk is built, so a lookup of one of these arriving
319 # meanwhile waits for this batch's answer.
320 40 my %boarded = map { ($_ => $self->_board($_)) } @pending;
321 40 my @queue;
322 40 ▶ push @queue, [splice @pending, 0, BATCH_MAX] while @pending;
Branch: 1 branch
BranchTrueFalseTotal
@pending and do { push @queue, [splice(@pending, 0, 1000)] } 66 40 106
323  
324 my $batch = {
325 queue => \@queue, answers => \%answers, active => 0, limit => $limit,
326 40 retries => $retries, timeout => $options{timeout}, promise => Mojo::Promise->new,
327 boarded => \%boarded,
328 };
329 40 $self->_dispatch($batch);
330 my $done = %joined ? Mojo::Promise->all(
331 $batch->{promise},
332 map {
333 1 my $ip = $_;
334 $joined{$ip}->then(
335 1 ▶ sub { $answers{$ip} = shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
336 0 ▶ sub { $answers{$ip} = VPNDetection::Error->wrap(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
337 1 );
338 } sort keys %joined,
339 40 ▶ ) : $batch->{promise};
Branch: 1 branch
BranchTrueFalseTotal
%joined ? : 1 39 40
340 40 ▶ return $done->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
341 40 return { map { ($_ => $answers{ VPNDetection::Bogon::unmapped($_) }) } @asked };
342 40 });
343 }
344  
345 # The licensed dataset downloads. Built per call rather than held, so the client
346 # and its sub-API never form a reference cycle.
347 195 ▶sub database {
Subroutine database: called CC=1 SCAR=0.0
Pod documented
348 195 my ($self) = @_;
349 195 return VPNDetection::Database->_new($self);
350 }
351  
352 # The OAuth device-flow sign-in, built per call for the same reason.
353 164 ▶sub oauth {
Subroutine oauth: called CC=1 SCAR=0.0
Pod documented
354 164 my ($self) = @_;
355 164 return VPNDetection::Oauth->_new($self);
356 }
357  
358 # Keeps exactly `limit` chunks in flight: every answer starts the next one, so a
359 # per-call concurrency is a real ceiling rather than an option that was accepted
360 # and ignored. A failing address carries its error as its value, so one bad
361 # entry cannot lose the rest of the answers: the API reports a per-entry failure
362 # with the status the single lookup would have answered, and a chunk that fails
363 # as a whole marks every address in it.
364 106 ▶sub _dispatch {
Subroutine _dispatch: called CC=5 SCAR=16.1
365 106 my ($self, $batch) = @_;
366 106 ▶ if (!@{$batch->{queue}} && !$batch->{active}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$batch->{"queue"};} or $batch->{"active"}) 40 66 106
Condition100%3/3
@{$batch->{"queue"};} or $batch->{"active"}
l!l&&r!l&&!r
57940
MC/DC100%2/2
@{$batch->{"queue"};} or $batch->{"active"}
@{$batch->{"queue"};} $batch->{"active"}
Truth table100%3/3
A or B
A= @{$batch->{"queue"};}B= $batch->{"active"}
ABresult
1X1
011
000
367 40 $batch->{promise}->resolve($batch->{answers});
368 40 return;
369 }
370 66 ▶ while ($batch->{active} < $batch->{limit} && @{$batch->{queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}) 66 66 132
Condition100%3/3
$batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}
!ll&&!rl&&r
224466
MC/DC100%2/2
$batch->{"active"} < $batch->{"limit"} and @{$batch->{"queue"};}
$batch->{"active"} < $batch->{"limit"} @{$batch->{"queue"};}
Truth table100%3/3
A and B
A= $batch->{"active"} < $batch->{"limit"}B= @{$batch->{"queue"};}
ABresult
0X0
100
111
371 66 my $chunk = shift @{$batch->{queue}};
372 66 $batch->{active}++;
373 66 ▶ $self->_lookup_chunk_p($chunk, $batch->{retries}, $batch->{timeout})->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
374 66 my $answers = shift;
375 66 for my $ip (keys %$answers) {
376 27549 my $answer = $answers->{$ip};
377 27549 $batch->{answers}{$ip} = $answer;
378 27549 ▶ my $failed = Scalar::Util::blessed($answer) && $answer->isa('VPNDetection::Error');
Condition66%2/3
builtin::blessed($answer) && $answer->isa('VPNDetection::Error')
!ll&&!rl&&r
02752029
MC/DC50%1/2
builtin::blessed($answer) && $answer->isa('VPNDetection::Error')
builtin::blessed($answer) $answer->isa('VPNDetection::Error')
Truth table66%2/3
A && B
A= builtin::blessed($answer)B= $answer->isa('VPNDetection::Error')
ABresult
0X0
100
111
379 27549 $self->_land($ip, $batch->{boarded}{$ip}, !$failed, $answer);
380 }
381 66 $self->_settled($batch);
382 66 });
383 }
384 }
385  
386 # One POST /batch, mapped back onto the addresses it was asked about. A
387 # chunk-level failure - the call refused, the transport failing, the retries
388 # exhausted - becomes every address's error, exactly as it would have been had
389 # each been looked up alone. Never rejects: the failure is the value.
390 66 ▶sub _lookup_chunk_p {
Subroutine _lookup_chunk_p: called CC=1 SCAR=0.0
391 66 my ($self, $chunk, $retries, $timeout) = @_;
392 66 my $url = $self->_url('/batch');
393 67 ▶ return $self->_retry_p($retries, sub { $self->_json_p($url, { ips => $chunk }, $timeout, undef, $BATCH) })->then(
Subroutine __ANON__: called CC=1 SCAR=0.0
394 62 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
395 62 my $body = shift;
396 62 my $results = $body->{results};
397 62 my $errors = $body->{errors};
398 62 my %answers;
399 # An entry is judged alone: one that is not an answer is that
400 # address's error, and the rest of the chunk keeps its answers.
401 62 for my $ip (@$chunk) {
402 27544 ▶ if (defined $results->{$ip} && !defined _nonconforming($LOOKUP, $results->{$ip}, [])) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $results->{$ip} and not defined _nonconforming($VPNDetection::LOOKUP, $results->{$ip}, scalar [])) { } 27520 24 27544
elsif (defined $errors->{$ip} and not defined _nonconforming($VPNDetection::BATCH_ERROR, $errors->{$ip}, scalar [])) { } 3 21 24
Condition100%3/3
defined $results->{$ip} and not defined _nonconforming($VPNDetection::LOOKUP, $results->{$ip}, scalar [])
!ll&&!rl&&r
51927520
MC/DC100%2/2
defined $results->{$ip} and not defined _nonconforming($VPNDetection::LOOKUP, $results->{$ip}, scalar [])
defined $results->{$ip} not defined _nonconforming($VPNDetection::LOOKUP, $results->{$ip}, scalar [])
Truth table100%3/3
A and not(B)
A= defined $results->{$ip}B= not defined _nonconforming($VPNDetection::LOOKUP, $results->{$ip}, scalar [])
ABresult
0X0
100
111
403 27520 my $result = VPNDetection::Result->from_wire($results->{$ip});
404 27520 ▶ $self->{cache}->set($ip, $result) if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 12 27508 27520
405 27520 $answers{$ip} = $result;
406 }
407 ▶ elsif (defined $errors->{$ip} && !defined _nonconforming($BATCH_ERROR, $errors->{$ip}, [])) {
Condition100%3/3
defined $errors->{$ip} and not defined _nonconforming($VPNDetection::BATCH_ERROR, $errors->{$ip}, scalar [])
!ll&&!rl&&r
1923
MC/DC100%2/2
defined $errors->{$ip} and not defined _nonconforming($VPNDetection::BATCH_ERROR, $errors->{$ip}, scalar [])
defined $errors->{$ip} not defined _nonconforming($VPNDetection::BATCH_ERROR, $errors->{$ip}, scalar [])
Truth table100%3/3
A and not(B)
A= defined $errors->{$ip}B= not defined _nonconforming($VPNDetection::BATCH_ERROR, $errors->{$ip}, scalar [])
ABresult
0X0
100
111
408 $answers{$ip} = VPNDetection::Error->from_entry(
409 $errors->{$ip}{status}, $errors->{$ip}{error},
410 3 );
411 }
412 else {
413 21 $answers{$ip} = VPNDetection::Error->new(
414 kind => 'server_error', status => 200,
415 message => "the batch answer did not include $ip",
416 );
417 }
418 }
419 62 return \%answers;
420 },
421 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
422 4 my $error = VPNDetection::Error->wrap(shift);
423 4 return { map { $_ => $error } @$chunk };
424 },
425 66 );
426 }
427  
428 # Releasing the slot happens in the SAME handler that records the answer. A
429 # second `then` would leave a gap in which a dying handler strands the slot, and
430 # a batch that never releases one never finishes.
431 66 ▶sub _settled {
Subroutine _settled: called CC=1 SCAR=0.0
432 66 my ($self, $batch) = @_;
433 66 $batch->{active}--;
434 66 $self->_dispatch($batch);
435 }
436  
437 # Recurses through $self rather than through a self-referential closure, which
438 # in Perl would be a reference cycle the interpreter never collects. $may_retry,
439 # when given, can veto a retry the error alone would allow; $tries counts the
440 # retries already made.
441 1012 ▶sub _retry_p {
Subroutine _retry_p: called CC=2 SCAR=6.9
442 1012 my ($self, $left, $attempt, $may_retry, $tries) = @_;
443 1012 ▶ $tries //= 0;
Condition100%2/2
$tries //= 0
l!l
91921
MC/DC100%1/1
$tries //= 0
$tries
Truth table100%2/2
A
A= $tries
Aresult
11
00
444 676 ▶ return $attempt->()->catch(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
445 676 my $error = VPNDetection::Error->wrap(shift);
446 676 ▶ die $error if $left <= 0 || !$error->retryable || ($may_retry && !$may_retry->());
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 0 or not $error->retryable or $may_retry and not &$may_retry() 585 91 676
Condition100%9/9
$left <= 0 or not $error->retryable or $may_retry and not &$may_retry()
l!l&&r!l&&!r
584191
$left <= 0 or not $error->retryable
l!l&&r!l&&!r
5691592
$may_retry and not &$may_retry()
!ll&&!rl&&r
9011
MC/DC100%4/4
$left <= 0 or not $error->retryable or $may_retry and not &$may_retry()
$left <= 0 not $error->retryable $may_retry not &$may_retry()
Truth table100%5/5
A or not(B) or C and not(D)
A= $left <= 0B= not $error->retryableC= $may_retryD= not &$may_retry()
ABCDresult
1XXX1
01XX1
00111
000X0
00100
447 # The wait is a TIMER, never a sleep: this promise shares an event loop
448 # with every other request in the batch, and sleeping here would stall
449 # all of them.
450 return Mojo::Promise->timer(_retry_delay($error->retry_after, $tries))
451 91 ▶ ->then(sub { $self->_retry_p($left - 1, $attempt, $may_retry, $tries + 1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
452 1012 });
453 }
454  
455 # The server's Retry-After when it gave a usable one, otherwise the backoff:
456 # 250 ms, doubling per retry, capped from the seventh. A Retry-After past
457 # LONGEST_WAIT is waited out on the backoff too, still rate_limited, rather than
458 # holding the call for as long as any server asks.
459 104 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=5 SCAR=16.1
460 104 my ($retry_after, $tries) = @_;
461 104 ▶ return $retry_after
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647" 3 101 104
Condition100%6/6
defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647"
!ll&&!rl&&r
9833
defined $retry_after and $retry_after > 0
!ll&&!rl&&r
9626
MC/DC100%3/3
defined $retry_after and $retry_after > 0 and $retry_after <= "2147483.647"
defined $retry_after $retry_after > 0 $retry_after <= "2147483.647"
Truth table100%4/4
A and B and C
A= defined $retry_afterB= $retry_after > 0C= $retry_after <= "2147483.647"
ABCresult
0XX0
10X0
1100
1111
462 if defined $retry_after && $retry_after > 0 && $retry_after <= LONGEST_WAIT;
463 101 ▶ return BACKOFF_BASE * 2**($tries < 6 ? $tries : 6);
Branch: 1 branch
BranchTrueFalseTotal
$tries < 6 ? : 99 2 101
464 }
465  
466 # $timeout is the call's own bound, or undef for the client's. $member, when
467 # given, is the one member of the answer the call returns; $shape is what the
468 # whole answer must be (see _nonconforming), members the call never reads
469 # included. An answer that is
470 # not fails inside the attempt as a retried server_error, like any other 2xx that
471 # cannot be read, rather than reaching the caller: through 3.8.0 a lookup's
472 # `"is_hosting": "yes"`, a version of "3" and a list entry of null came back as
473 # served, and `{}` as a lookup's answer.
474 766 ▶sub _json_p {
Subroutine _json_p: called CC=2 SCAR=6.9
475 766 my ($self, $url, $body, $timeout, $member, $shape) = @_;
476 766 ▶ my $sent = defined $body ? $self->_post_p($url, $body, $timeout) : $self->_get_p($url, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 67 699 766
477 744 ▶ return $sent->then(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
478 744 my $res = shift->res;
479 744 ▶ die VPNDetection::Error->from_response($res->code, $res->headers, $res->json)
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 43 701 744
480 unless $res->is_success;
481 701 my $decoded = $res->json;
482 466 ▶ my $unreadable = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
483 466 die VPNDetection::Error->new(kind => 'server_error', status => $res->code, message => shift);
484 701 };
485 701 ▶ $unreadable->('the API did not answer with a JSON object') unless ref $decoded eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $decoded eq "HASH" 16 685 701
486 685 ▶ return $decoded unless $shape;
Branch: 1 branch
BranchTrueFalseTotal
unless $shape 0 685 685
487 685 my $wrong = _nonconforming($shape, $decoded, []);
488 685 ▶ $unreadable->($wrong) if defined $wrong;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wrong 450 235 685
489 235 ▶ return defined $member ? $decoded->{$member} : $decoded;
Branch: 1 branch
BranchTrueFalseTotal
defined $member ? : 41 194 235
490 764 });
491 }
492  
493 # undef when the value has the shape, else what the API answered wrong, naming
494 # the member by its path. A string is told from a number by the scalar Mojo::JSON
495 # decoded it into, as VPNDetection::Oauth does, since nothing else in Perl tells
496 # `"7"` from `7`. No list entry or map value may be null.
497 120513 ▶sub _nonconforming {
Subroutine _nonconforming: called CC=31 SCAR=34.5
498 120513 my ($shape, $value, $at) = @_;
499 120513 my ($kind, $inner) = @$shape;
500 120513 ▶ my $wrong = sub { 'the API answered ' . (@$at ? join('.', @$at) : 'the answer') . ' of the wrong type' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@$at ? : 47 0 47
501 120513 ▶ return $wrong->() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 14 120499 120513
502 120499 ▶ return undef if $kind eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "any" 27544 92955 120499
503 92955 ▶ if ($kind eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "string") 31375 61580 92955
504 31375 ▶ return VPNDetection::Oauth::_is_string($value) ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
VPNDetection::Oauth::_is_string($value) ? : 31373 2 31375
505 }
506 61580 ▶ if ($kind eq 'date' || $kind eq 'date-time') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date" or $kind eq "date-time") 2471 59109 61580
Condition100%3/3
$kind eq "date" or $kind eq "date-time"
l!l&&r!l&&!r
200147059109
MC/DC100%2/2
$kind eq "date" or $kind eq "date-time"
$kind eq "date" $kind eq "date-time"
Truth table100%3/3
A or B
A= $kind eq "date"B= $kind eq "date-time"
ABresult
1X1
011
000
507 2471 ▶ return VPNDetection::Oauth::_is_string($value) && _is_rfc3339($kind, $value) ? undef
Branch: 1 branch
BranchTrueFalseTotal
VPNDetection::Oauth::_is_string($value) && _is_rfc3339($kind, $value) ? : 2073 398 2471
Condition66%2/3
VPNDetection::Oauth::_is_string($value) and _is_rfc3339($kind, $value)
!ll&&!rl&&r
03982073
MC/DC50%1/2
VPNDetection::Oauth::_is_string($value) and _is_rfc3339($kind, $value)
VPNDetection::Oauth::_is_string($value) _is_rfc3339($kind, $value)
Truth table66%2/3
A and B
A= VPNDetection::Oauth::_is_string($value)B= _is_rfc3339($kind, $value)
ABresult
0X0
100
111
508 : 'the API answered ' . join('.', @$at) . ' that is no RFC 3339 ' . $kind;
509 }
510 59109 ▶ if ($kind eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "integer") 213 58896 59109
511 213 ▶ return !ref $value && !VPNDetection::Oauth::_is_string($value) && $value =~ /\A-?[0-9]+\z/
Branch: 1 branch
BranchTrueFalseTotal
!(ref $value || VPNDetection::Oauth::_is_string($value)) && $value =~ /\A-?[0-9]+\z/ ? : 195 18 213
Condition66%4/6
not ref $value || VPNDetection::Oauth::_is_string($value) and $value =~ /\A-?[0-9]+\z/
!ll&&!rl&&r
180195
ref $value || VPNDetection::Oauth::_is_string($value)
l!l&&r!l&&!r
018195
MC/DC33%1/3
not ref $value || VPNDetection::Oauth::_is_string($value) and $value =~ /\A-?[0-9]+\z/
ref $value VPNDetection::Oauth::_is_string($value) $value =~ /\A-?[0-9]+\z/
Truth table50%2/4
not(A || B) and C
A= ref $valueB= VPNDetection::Oauth::_is_string($value)C= $value =~ /\A-?[0-9]+\z/
ABCresult
1XX0
01X0
0000
0011
512 ? undef : $wrong->();
513 }
514 58896 ▶ if ($kind eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "boolean") 28145 30751 58896
515 28145 ▶ return Scalar::Util::blessed($value) && $value->isa('JSON::PP::Boolean') ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa('JSON::PP::Boolean') ? : 28136 9 28145
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
9028136
MC/DC50%1/2
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
516 }
517 30751 ▶ if ($kind eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "object") 4 30747 30751
518 4 ▶ return ref $value eq 'HASH' ? undef : $wrong->();
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 4 0 4
519 }
520 30747 ▶ if ($kind eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "list") 300 30447 30747
521 300 ▶ return $wrong->() unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 2 298 300
522 298 for my $i (0 .. $#$value) {
523 234 my $w = _nonconforming($inner, $value->[$i], [@$at, $i]);
524 234 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 129 105 234
525 }
526 169 return undef;
527 }
528 30447 ▶ if ($kind eq 'map_of') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "map_of") 144 30303 30447
529 144 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 144 144
530 144 for my $key (sort keys %$value) {
531 27561 my $w = _nonconforming($inner, $value->{$key}, [@$at, $key]);
532 27561 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 6 27555 27561
533 }
534 138 return undef;
535 }
536 30303 ▶ return $wrong->() unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 2 30301 30303
537 30301 for my $field (@$inner) {
538 678495 my ($name, $member_shape, $required) = @$field;
539 678495 ▶ if (!defined $value->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value->{$name}) 614006 64489 678495
540 614006 ▶ return 'the API answered with no ' . join('.', @$at, $name) if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 26 613980 614006
541 613980 next;
542 }
543 64489 my $w = _nonconforming($member_shape, $value->{$name}, [@$at, $name]);
544 64489 ▶ return $w if defined $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w 861 63628 64489
545 }
546 29414 return undef;
547 }
548  
549 # RFC 3339's `full-date`, or its `date-time` with the `T` and `Z` in either case,
550 # an hour below 24, a minute and second below 60 and an offset of at most 23:59,
551 # on a day its month has. ASCII digits and `\z`, since Perl's `\d` takes any
552 # script's digits and `$` a trailing newline.
553 7 my $FULL_DATE = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/;
554 7 my $DATE_TIME = qr/\A([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.[0-9]+)?
555 (?:[Zz]|[+-]([0-9]{2}):([0-9]{2}))\z/x;
556  
557 2471 ▶sub _is_rfc3339 {
Subroutine _is_rfc3339: called CC=10 SCAR=23.0
558 2471 my ($kind, $value) = @_;
559 2471 ▶ if ($kind eq 'date') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "date") 2001 470 2471
560 2001 ▶ my @date = $value =~ $FULL_DATE or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@date) = $value =~ /$FULL_DATE/ or return 0 1833 168 2001
561 1833 return _is_day(@date);
562 }
563 470 ▶ my ($year, $month, $day, $hour, $minute, $second, $off_hour, $off_minute) = $value =~ $DATE_TIME
Branch: 1 branch
BranchTrueFalseTotal
() = $value =~ /$DATE_TIME/ or return 0 363 107 470
564 or return 0;
565 363 ▶ return _is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60
Condition100%18/18
_is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60 && (!defined($off_hour) || $off_hour < 24 && $off_minute < 60)
!ll&&!rl&&r
3216315
_is_day($year, $month, $day) and $hour < 24 and $minute < 60 and $second < 60
!ll&&!rl&&r
248331
_is_day($year, $month, $day) and $hour < 24 and $minute < 60
!ll&&!rl&&r
168339
_is_day($year, $month, $day) and $hour < 24
!ll&&!rl&&r
88347
not defined $off_hour or $off_hour < 24 and $off_minute < 60
l!l&&r!l&&!r
2991616
$off_hour < 24 and $off_minute < 60
!ll&&!rl&&r
8816
MC/DC100%7/7
_is_day($year, $month, $day) && $hour < 24 && $minute < 60 && $second < 60 && (!defined($off_hour) || $off_hour < 24 && $off_minute < 60)
_is_day($year, $month, $day) $hour < 24 $minute < 60 $second < 60 not defined $off_hour $off_hour < 24 $off_minute < 60
Truth table100%8/8
A and B and C and D && not(E) or F and G
A= _is_day($year, $month, $day)B= $hour < 24C= $minute < 60D= $second < 60E= not defined $off_hourF= $off_hour < 24G= $off_minute < 60
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
111100X0
11110100
11111XX1
11110111
566 && (!defined $off_hour || ($off_hour < 24 && $off_minute < 60));
567 }
568  
569 2196 ▶sub _is_day {
Subroutine _is_day: called CC=7 SCAR=19.5
570 2196 my ($year, $month, $day) = @_;
571 2196 ▶ return 0 if $month < 1 || $month > 12 || $day < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 or $day < 1 30 2166 2196
Condition83%5/6
$month < 1 or $month > 12 or $day < 1
l!l&&r!l&&!r
15152166
$month < 1 or $month > 12
l!l&&r!l&&!r
0152181
MC/DC66%2/3
$month < 1 or $month > 12 or $day < 1
$month < 1 $month > 12 $day < 1
Truth table75%3/4
A or B or C
A= $month < 1B= $month > 12C= $day < 1
ABCresult
1XX1
01X1
0011
0000
572 2166 ▶ my $leap = ($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0;
Condition100%6/6
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
l!l&&r!l&&!r
15152136
$year % 4 == 0 and $year % 100 != 0
!ll&&!rl&&r
21213015
MC/DC100%3/3
$year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
$year % 4 == 0 $year % 100 != 0 $year % 400 == 0
Truth table80%4/5
A and B || C
A= $year % 4 == 0B= $year % 100 != 0C= $year % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
573 2166 ▶ return $day <= (31, $leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[$month - 1];
Branch: 1 branch
BranchTrueFalseTotal
$leap ? : 30 2136 2166
574 }
575  
576 723 ▶sub _get_p {
Subroutine _get_p: called CC=1 SCAR=0.0
577 723 my ($self, $url, $timeout) = @_;
578 723 return $self->_start_p($self->{ua}->build_tx(GET => $url => $self->_headers), $timeout);
579 }
580  
581 # The one request with a body: the batch. Mojo encodes the JSON and sets the
582 # content type.
583 67 ▶sub _post_p {
Subroutine _post_p: called CC=1 SCAR=0.0
584 67 my ($self, $url, $body, $timeout) = @_;
585 67 my $tx = $self->{ua}->build_tx(POST => $url => $self->_headers => json => $body);
586 67 return $self->_start_p($tx, $timeout);
587 }
588  
589 # One dataset transfer. Every chunk is handed to $on_chunk and none is kept, so a
590 # body costs the same in memory whether it is 10 KB or 1.79 GB. Resolves with the
591 # number of bytes handed over.
592 15 ▶sub _stream_p {
Subroutine _stream_p: called CC=1 SCAR=0.0
593 15 my ($self, $url, $on_chunk) = @_;
594 # Built here rather than through _get_p, which is the only place the API key
595 # is ever attached: the presigned link authorizes itself, so carrying the key
596 # would hand it to a host with no business holding it.
597 15 my $tx = $self->{ua}->build_tx(GET => $url);
598  
599 # Mojo asks for gzip on every request it builds. A dataset file is already
600 # compressed, so the only thing that would buy is a Content-Length counting
601 # bytes that never reach the sink, and that length is the only evidence the
602 # transfer arrived whole.
603 15 $tx->req->headers->remove('Accept-Encoding');
604 # Mojo aborts a response past max_message_size, counting every byte it parses
605 # whether or not it keeps any of them. The default is 2 GiB, which the
606 # catalog is already within an order of magnitude of, and
607 # MOJO_MAX_MESSAGE_SIZE lowers it for every response in the process, on a
608 # machine whose environment we do not own.
609 15 $tx->res->max_message_size(0);
610  
611 15 my $received = 0;
612 # Unsubscribing Mojo's own reader is what stops the body being collected into
613 # the message. The subscriber hangs off the transaction, so holding the
614 # transaction strongly inside it would be a cycle the interpreter never
615 # collects; the user agent keeps it alive for as long as bytes are arriving.
616 15 my $weak = $tx;
617 15 Scalar::Util::weaken($weak);
618 208 ▶ $tx->res->content->unsubscribe('read')->on(read => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
619 208 my (undef, $bytes) = @_;
620 # An error body is neither written out nor held: the status is what
621 # separates a lapsed link from a refused one, and nothing bounds the size
622 # of what a storage host puts in the body of a refusal.
623 208 ▶ return unless $weak && ($weak->res->code || 0) == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and ($weak->res->code || 0) == 200 2 206 208
Condition60%3/5
$weak and ($weak->res->code || 0) == 200
!ll&&!rl&&r
02206
$weak->res->code || 0
l!l
2080
MC/DC0%0/2
$weak and ($weak->res->code || 0) == 200
$weak $weak->res->code
Truth table33%1/3
A and B
A= $weakB= $weak->res->code
ABresult
0X0
100
111
624 206 $received += length $bytes;
625 206 $on_chunk->($bytes);
626 15 });
627  
628 14 ▶ return $self->_start_p($tx, 0)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
629 14 my $res = shift->res;
630 14 ▶ die VPNDetection::Error->from_response($res->code, $res->headers, {
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 200 2 12 14
631 error => 'object storage refused the download link with status ' . $res->code,
632 }) unless $res->code == 200;
633  
634 # Before Mojolicious 9.54 a body that stops early reaches Perl as an
635 # ordinary end of stream: the status was 200 and Mojo reports no error,
636 # so without this a half transfer is a short file nobody notices.
637 12 my $short = _short_transfer($res, $received);
638 12 ▶ die $short if $short;
Branch: 1 branch
BranchTrueFalseTotal
if $short 0 12 12
639 12 return $received;
640 1 ▶ }, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
641 1 my $error = shift;
642 # From 9.54 the same body rejects as `Premature connection close`, which
643 # says nothing of how far it got, so the lengths are added to it.
644 1 my $short = _short_transfer($tx->res, $received, $error);
645 1 ▶ die $short // $error;
Condition33%1/3
$short // $error
l!l&&r!l&&!r
100
MC/DC0%0/2
$short // $error
$short $error
Truth table33%1/3
A // B
A= $shortB= $error
ABresult
1X1
011
000
646 15 });
647 }
648  
649 # The error for a 200 whose body stopped short of the length it declared, or
650 # undef when it did not. $cause is what Mojo rejected it with, when it did.
651 13 ▶sub _short_transfer {
Subroutine _short_transfer: called CC=8 SCAR=23.7
652 13 my ($res, $received, $cause) = @_;
653 13 my $declared = $res->headers->content_length;
654 return undef
655 13 ▶ unless ($res->code || 0) == 200 && defined $declared && length $declared && $declared != $received;
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received 12 1 13
Condition45%5/11
($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received
!ll&&!rl&&r
0121
($res->code || 0) == 200 and defined $declared and length $declared
!ll&&!rl&&r
0013
($res->code || 0) == 200 and defined $declared
!ll&&!rl&&r
0013
$res->code || 0
l!l
130
MC/DC25%1/4
($res->code || 0) == 200 and defined $declared and length $declared and $declared != $received
$res->code defined $declared length $declared $declared != $received
Truth table40%2/5
A and B and C and D
A= $res->codeB= defined $declaredC= length $declaredD= $declared != $received
ABCDresult
0XXX0
10XX0
110X0
11100
11111
656 1 my $message = "the transfer ended after $received of $declared bytes";
657 1 ▶ $message .= ': ' . (ref $cause ? $cause->message : $cause) if defined $cause;
Branch: 2 branches
BranchTrueFalseTotal
if defined $cause 1 0 1
ref $cause ? : 1 0 1
658 1 return VPNDetection::Error->new(kind => 'network', message => $message);
659 }
660  
661 # request_timeout bounds the WHOLE response and Mojo::UserAgent has no
662 # per-transaction form of it, yet a call may bring its own bound and a transfer
663 # must have none (0): 30 seconds is right for a lookup and wrong for a gigabyte.
664 # So it is set only across the hand-over, where start_p arms the timer
665 # synchronously - and set EXPLICITLY each time, because Mojo runs a loop tick
666 # inside start_p when the loop is idle, and a retry started in that tick would
667 # otherwise inherit this call's bound.
668 #
669 # A rejection is a plain string when the request never got far enough to have a
670 # status, which is exactly the transport failure the retry rule treats as worth
671 # another attempt.
672 1010 ▶sub _start_p {
Subroutine _start_p: called CC=3 SCAR=11.0
673 1010 my ($self, $tx, $timeout) = @_;
674 1010 my $ua = $self->{ua};
675 1010 my $bound = $ua->request_timeout;
676 1010 ▶ $ua->request_timeout(defined $timeout ? 0 + $timeout : $self->{timeout});
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 70 940 1010
677 1010 my $promise = eval { $ua->start_p($tx) };
678 1010 my $failed = $@;
679 1010 $ua->request_timeout($bound);
680 1010 ▶ die VPNDetection::Error->wrap($failed) unless $promise;
Branch: 1 branch
BranchTrueFalseTotal
unless $promise 0 1010 1010
681 47 ▶ return $promise->catch(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
682 47 die VPNDetection::Error->new(kind => 'network', message => "$_[0]");
683 1010 });
684 }
685  
686 790 ▶sub _headers {
Subroutine _headers: called CC=2 SCAR=6.9
687 790 my ($self) = @_;
688 790 my %headers = (Accept => 'application/json');
689 # One scheme, though the API offers three. Sending the same key as a bearer
690 # token, a header and a query parameter at once is three times the exposure
691 # for one credential, and query strings end up in logs. A blank key is none
692 # at all (see new).
693 790 ▶ $headers{Authorization} = "Bearer $self->{api_key}" if defined $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"api_key"} 618 172 790
694 790 return \%headers;
695 }
696  
697 913 ▶sub _url {
Subroutine _url: called CC=2 SCAR=6.9
698 913 my ($self, $path, %query) = @_;
699 913 my $url = Mojo::URL->new($self->{base_url} . $path);
700 913 ▶ $url->query(%query) if %query;
Branch: 1 branch
BranchTrueFalseTotal
if %query 66 847 913
701 913 return $url;
702 }
703  
704 868 ▶sub _wait {
Subroutine _wait: called CC=2 SCAR=6.9
705 868 my ($self, $promise) = @_;
706 868 my ($value, $error, $failed);
707 868 ▶ $promise->then(sub { $value = shift }, sub { ($error, $failed) = (shift, 1) })->wait;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
708 868 ▶ die $error if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 596 272 868
709 272 return $value;
710 }
711  
712 # Checked BEFORE the promise is built, not after: Mojo::UserAgent starts a
713 # transaction as soon as it is created, so croaking later would still have spent
714 # a request from the caller's allowance.
715 1002 ▶sub _assert_blocking_ok {
Subroutine _assert_blocking_ok: called CC=2 SCAR=6.9
716 1002 my ($self, $method) = @_;
717 1002 ▶ return unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 1001 1 1002
718 1 Carp::croak(
719 "VPNDetection::$method cannot block inside a running Mojo::IOLoop; "
720 . "call ${method}_p instead, which returns a Mojo::Promise"
721 );
722 }
723  
724 1078 ▶sub _check_options {
Subroutine _check_options: called CC=9 SCAR=22.0
725 1078 my ($self, $method, $options, @allowed) = @_;
726 1078 my %allowed = map { $_ => 1 } @allowed;
727 1078 my @unknown = sort grep { !$allowed{$_} } keys %$options;
728 1078 ▶ Carp::croak("VPNDetection::$method: unknown option(s): @unknown") if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 6 1072 1078
729 # A negative or non-numeric bound would fire at once and fail the very call
730 # it was meant to protect.
731 1072 my $timeout = $options->{timeout};
732 1072 ▶ Carp::croak("VPNDetection::$method: timeout must be a number of seconds, 0 or more")
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0 5 1067 1072
Condition100%6/6
defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0
!ll&&!rl&&r
1013545
&Scalar::Util::looks_like_number($timeout) && $timeout >= 0
!ll&&!rl&&r
2354
MC/DC100%3/3
defined $timeout and not &Scalar::Util::looks_like_number($timeout) && $timeout >= 0
defined $timeout &Scalar::Util::looks_like_number($timeout) $timeout >= 0
Truth table100%4/4
A and not(B && C)
A= defined $timeoutB= &Scalar::Util::looks_like_number($timeout)C= $timeout >= 0
ABCresult
0XX0
1110
10X1
1101
733 if defined $timeout && !(Scalar::Util::looks_like_number($timeout) && $timeout >= 0);
734 Carp::croak("VPNDetection::$method: retries must be a whole number, 0 or more")
735 1067 ▶ if defined $options->{retries} && !_is_count($options->{retries}, 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"retries"} and not _is_count($options->{"retries"}, 0) 98 969 1067
Condition100%3/3
defined $options->{"retries"} and not _is_count($options->{"retries"}, 0)
!ll&&!rl&&r
967298
MC/DC100%2/2
defined $options->{"retries"} and not _is_count($options->{"retries"}, 0)
defined $options->{"retries"} not _is_count($options->{"retries"}, 0)
Truth table100%3/3
A and not(B)
A= defined $options->{"retries"}B= not _is_count($options->{"retries"}, 0)
ABresult
0X0
100
111
736 Carp::croak("VPNDetection::$method: concurrency must be a whole number, 1 or more")
737 969 ▶ if defined $options->{concurrency} && !_is_count($options->{concurrency}, 1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1) 16 953 969
Condition100%3/3
defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1)
!ll&&!rl&&r
950316
MC/DC100%2/2
defined $options->{"concurrency"} and not _is_count($options->{"concurrency"}, 1)
defined $options->{"concurrency"} not _is_count($options->{"concurrency"}, 1)
Truth table100%3/3
A and not(B)
A= defined $options->{"concurrency"}B= not _is_count($options->{"concurrency"}, 1)
ABresult
0X0
100
111
738 }
739  
740 # A whole number from $least, or Inf for no limit. NaN compares false with
741 # anything, so a NaN count retried a failing call for good and a NaN concurrency
742 # put no chunk in flight, spinning the loop forever; a word reads as 0, and 1.5
743 # as two retries.
744 1706 ▶sub _is_count {
Subroutine _is_count: called CC=4 SCAR=13.9
745 1706 my ($n, $least) = @_;
746 1706 ▶ return !ref $n && Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n;
Condition100%9/9
!ref($n) && &Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n
!ll&&!rl&&r
112181576
not ref $n and &Scalar::Util::looks_like_number($n) and $n >= $least
!ll&&!rl&&r
54581594
not ref $n and &Scalar::Util::looks_like_number($n)
!ll&&!rl&&r
18361652
MC/DC100%4/4
!ref($n) && &Scalar::Util::looks_like_number($n) && $n >= $least && $n == int $n
not ref $n &Scalar::Util::looks_like_number($n) $n >= $least $n == int $n
Truth table100%5/5
not(A) and B and C && D
A= not ref $nB= &Scalar::Util::looks_like_number($n)C= $n >= $leastD= $n == int $n
ABCDresult
0XXX0
10XX0
110X0
11100
11111
747 }
748  
749 775 ▶sub _base_url {
Subroutine _base_url: called CC=3 SCAR=11.0
750 775 my ($url) = @_;
751 775 ▶ $url = DEFAULT_BASE_URL unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 4 771 775
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
40771
MC/DC50%1/2
defined $url and length $url
defined $url length $url
Truth table66%2/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
752 775 $url =~ s{/+\z}{};
753 775 return $url;
754 }
755  
756 7 1;
757  
758 __END__
↑ Summary VPNDetection/Oauth.pm »