VPNDetection/Result.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 98.6% 74 / 75 CC 10 SCAR 14.5
CC
10
Cov
100%
from_wire
8.0
has
3.0
BEGIN
1.0
CRAP 4.3
« VPNDetection/Bogon.pm ↑ Summary VPNDetection/Cache.pm »
1 package VPNDetection::Result;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 our $VERSION = '3.20.0';
9  
10 12 our @FLAGS = qw(
11 is_vpn is_hosting is_relay is_tor is_cdn is_bot is_resproxy is_dcproxy is_mobproxy
12 is_pubproxy is_anycast is_carrier is_satellite is_ssh is_smtp is_web is_dns_resolver
13 is_bittorrent
14 );
15 # Each flag's detail: an object, or for `web` a list of the ports that answered.
16 # `is_anycast` has none.
17 12 our @DETAILS = qw(
18 vpn hosting relay tor cdn bot resproxy dcproxy mobproxy pubproxy carrier satellite ssh
19 smtp web dns_resolver bittorrent
20 );
21 # Where the address is and the AS announcing it, on every answer, with or without
22 # a key, each with every member present bar the ones a plan leaves out: an empty
23 # string, or AS 0, where nothing is known. The reverse DNS hostname is on every
24 # answer too, and company and abuse on a paid plan's.
25 12 our @OBJECTS = qw(location asn company hostname abuse);
26 # The members of each object. Those after flag_unicode in location, and an asn's
27 # type, are absent from an answer whose plan leaves them out.
28 12 our @LOCATION = qw(
29 continent continent_name country country_name timezone currency_code currency_name
30 currency_symbol flag_emoji flag_unicode region region_code city postal_code latitude
31 longitude accuracy_radius geoname_id dma_code last_changed
32 );
33 12 our @ASN = qw(asn name domain type prefix country rpki_status);
34 12 our @COMPANY = qw(name domain type country);
35 12 our @ABUSE = qw(email name phone address country network validated unvalidated_since);
36 # On answers to a key on the free plan only, and so never on a bogon's: false
37 # while its organization's trial of every field lasts, true once the answer has
38 # Unlimited's. cap_reached is the
39 # same on a paid plan's answers: false while the answer has every field, true
40 # once its organization's overage cap is reached.
41 12 our @MARKERS = qw(trial_expired cap_reached);
42 # What the user agent sent with the lookup names, and whether the address bears
43 # it out: on an answer with every field to a lookup that sent one, and on no
44 # other, so never on a bogon's.
45 12 our @VERDICTS = qw(user_agent);
46  
47 12 my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS;
48  
49 # The one place a wire body becomes a result. Every field is copied on key
50 # PRESENCE and never on truthiness: a plan that includes `is_hosting` and
51 # answers false must keep it, and a plan that does not include it must not gain
52 # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a
53 # missing key are all false.
54 28722 ▶sub from_wire {
Subroutine from_wire: called CC=8 SCAR=20.8
Pod undocumented
55 28722 my ($class, $body) = @_;
56 28722 ▶ my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$body->{'is_bogon'} ? : 1 28721 28722
57 # A served null stays undef, as a detail's does: read as 0 it would say the
58 # address is no hosting server, or that the trial lasts, when nothing was said.
59 28722 for my $flag (@FLAGS, @MARKERS) {
60 574440 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 545469 28971 574440
61 28971 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 28969 2 28971
$body->{$flag} ? : 184 28785 28969
62 }
63 28722 for my $detail (@DETAILS, @OBJECTS, @VERDICTS) {
64 660606 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 633 659973 660606
65 }
66 28722 return $class->_new(\%answer, $body);
67 }
68  
69 # Whether your plan carries this field at all, which is a different question
70 # from what the field says. `has` is the only honest answer to "is this address
71 # hosting?" when the field may not have been served: absent is not false.
72 390 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
73 390 my ($self, $field) = @_;
74 390 ▶ Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 1 389 390
75 389 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 232 157 389
76 }
77  
78 # The fields this answer actually carried, in wire order. Useful for seeing what
79 # a key is entitled to without reading the plan table.
80 2 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
81 2 my ($self) = @_;
82 2 return grep { exists $self->{$_} } @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS;
83 }
84  
85 # The response exactly as it came off the wire, with its original names and its
86 # JSON booleans intact. Empty for a bogon, which was never served.
87 3 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
88 3 return $_[0]->{_raw};
89 }
90  
91 28730 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
92 28730 my ($class, $answer, $raw) = @_;
93 28730 $answer->{_raw} = $raw;
94 28730 return bless $answer, $class;
95 }
96  
97 # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent
98 # "Can't locate object method" rather than a silent undef.
99 12 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS) {
100 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 5244 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
102 }
103  
104 12 1;
105  
106 __END__
« VPNDetection/Bogon.pm ↑ Summary VPNDetection/Cache.pm »