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 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 our $VERSION = '4.0.0';
9  
10 15 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 15 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 15 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 15 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 15 our @ASN = qw(asn name domain type prefix country rpki_status);
34 15 our @COMPANY = qw(name domain type country);
35 15 our @ABUSE = qw(email name phone address country network validated unvalidated_since);
36 # On every answer to a key, and so never on a bogon's or a keyless one: false while
37 # the answer has every field, true once your organization's allowance is used (a
38 # free key's full answers for the UTC day, a paid key's quota or overage cap) and
39 # the answer has the basics, without is_vpn. A true one raises quota_exceeded, so
40 # it is read from the error's result.
41 15 our @MARKERS = qw(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 15 our @VERDICTS = qw(user_agent);
46  
47 15 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 28749 ▶sub from_wire {
Subroutine from_wire: called CC=8 SCAR=20.8
Pod undocumented
55 28749 my ($class, $body) = @_;
56 28749 ▶ my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$body->{'is_bogon'} ? : 1 28748 28749
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 day's full answers last, when
59 # nothing was said.
60 28749 for my $flag (@FLAGS, @MARKERS) {
61 546231 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 517152 29079 546231
62 29079 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 29076 3 29079
$body->{$flag} ? : 203 28873 29076
63 }
64 28749 for my $detail (@DETAILS, @OBJECTS, @VERDICTS) {
65 661227 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 683 660544 661227
66 }
67 28749 return $class->_new(\%answer, $body);
68 }
69  
70 # Whether your plan carries this field at all, which is a different question
71 # from what the field says. `has` is the only honest answer to "is this address
72 # hosting?" when the field may not have been served: absent is not false.
73 401 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
74 401 my ($self, $field) = @_;
75 401 ▶ Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 2 399 401
76 399 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 234 165 399
77 }
78  
79 # The fields this answer actually carried, in wire order. Useful for seeing what
80 # a key is entitled to without reading the plan table.
81 4 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
82 4 my ($self) = @_;
83 4 return grep { exists $self->{$_} } @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS;
84 }
85  
86 # The response exactly as it came off the wire, with its original names and its
87 # JSON booleans intact. Empty for a bogon, which was never served.
88 5 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
89 5 return $_[0]->{_raw};
90 }
91  
92 28760 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
93 28760 my ($class, $answer, $raw) = @_;
94 28760 $answer->{_raw} = $raw;
95 28760 return bless $answer, $class;
96 }
97  
98 # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent
99 # "Can't locate object method" rather than a silent undef.
100 15 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS) {
101 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 32968 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
103 }
104  
105 15 1;
106  
107 __END__
« VPNDetection/Bogon.pm ↑ Summary VPNDetection/Cache.pm »