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 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our $VERSION = '3.19.0';
9  
10 10 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 10 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: an empty string, or AS 0, where nothing
23 # is known. The reverse DNS hostname is on every answer too, and company and abuse
24 # on a paid plan's.
25 10 our @OBJECTS = qw(location asn company hostname abuse);
26 # The members of each object. Those after flag_unicode in location, and after
27 # prefix in asn, are absent from an answer whose plan leaves them out.
28 10 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 10 our @ASN = qw(asn name domain type prefix country rpki_status);
34 10 our @COMPANY = qw(name domain type country);
35 10 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 10 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 10 our @VERDICTS = qw(user_agent);
46  
47 10 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 28690 ▶sub from_wire {
Subroutine from_wire: called CC=8 SCAR=20.8
Pod undocumented
55 28690 my ($class, $body) = @_;
56 28690 ▶ my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$body->{'is_bogon'} ? : 1 28689 28690
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 28690 for my $flag (@FLAGS, @MARKERS) {
60 573800 ▶ next unless exists $body->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $body->{$flag} 544880 28920 573800
61 28920 ▶ $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $body->{$flag} ? : 28918 2 28920
$body->{$flag} ? : 185 28733 28918
62 }
63 28690 for my $detail (@DETAILS, @OBJECTS, @VERDICTS) {
64 659870 ▶ $answer{$detail} = $body->{$detail} if exists $body->{$detail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $body->{$detail} 626 659244 659870
65 }
66 28690 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 389 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod documented
73 389 my ($self, $field) = @_;
74 389 ▶ Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN{$field} 1 388 389
75 388 ▶ return exists $self->{$field} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 234 154 388
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 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
88 2 return $_[0]->{_raw};
89 }
90  
91 28698 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
92 28698 my ($class, $answer, $raw) = @_;
93 28698 $answer->{_raw} = $raw;
94 28698 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 10 for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS) {
100 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 5190 ▶ *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
102 }
103  
104 10 1;
105  
106 __END__
« VPNDetection/Bogon.pm ↑ Summary VPNDetection/Cache.pm »