| 1 | package VPNDetection::Result; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 13 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 13 | our $VERSION = '3.21.0'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 13 | 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 | 13 | 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 | 13 | 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 | 13 | 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 | 13 | our @ASN = qw(asn name domain type prefix country rpki_status); | |||||||||||||
| 34 | 13 | our @COMPANY = qw(name domain type country); | |||||||||||||
| 35 | 13 | 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 the answer has every field, true once your organization's full answers for | ||||||||||||||
| 38 | # the UTC day are used up and the answer has the basics, without is_vpn. | ||||||||||||||
| 39 | # cap_reached is the same on a paid plan's answers: false while the answer has | ||||||||||||||
| 40 | # every field, true once its organization's overage cap is reached and the answer | ||||||||||||||
| 41 | # has the basics and is_vpn. | ||||||||||||||
| 42 | 13 | our @MARKERS = qw(trial_expired cap_reached); | |||||||||||||
| 43 | # What the user agent sent with the lookup names, and whether the address bears | ||||||||||||||
| 44 | # it out: on an answer with every field to a lookup that sent one, and on no | ||||||||||||||
| 45 | # other, so never on a bogon's. | ||||||||||||||
| 46 | 13 | our @VERDICTS = qw(user_agent); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 13 | my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS; | |||||||||||||
| 49 | |||||||||||||||
| 50 | # The one place a wire body becomes a result. Every field is copied on key | ||||||||||||||
| 51 | # PRESENCE and never on truthiness: a plan that includes `is_hosting` and | ||||||||||||||
| 52 | # answers false must keep it, and a plan that does not include it must not gain | ||||||||||||||
| 53 | # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a | ||||||||||||||
| 54 | # missing key are all false. | ||||||||||||||
| 55 | 28729 | ▶ | sub from_wire { | ||||||||||||
|
Subroutine
from_wire: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||
| 56 | 28729 | my ($class, $body) = @_; | |||||||||||||
| 57 | 28729 | ▶ | my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | # A served null stays undef, as a detail's does: read as 0 it would say the | ||||||||||||||
| 59 | # address is no hosting server, or that the day's full answers last, when | ||||||||||||||
| 60 | # nothing was said. | ||||||||||||||
| 61 | 28729 | for my $flag (@FLAGS, @MARKERS) { | |||||||||||||
| 62 | 574580 | ▶ | next unless exists $body->{$flag}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 29001 | ▶ | $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 64 | } | ||||||||||||||
| 65 | 28729 | for my $detail (@DETAILS, @OBJECTS, @VERDICTS) { | |||||||||||||
| 66 | 660767 | ▶ | $answer{$detail} = $body->{$detail} if exists $body->{$detail}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | } | ||||||||||||||
| 68 | 28729 | return $class->_new(\%answer, $body); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | # Whether your plan carries this field at all, which is a different question | ||||||||||||||
| 72 | # from what the field says. `has` is the only honest answer to "is this address | ||||||||||||||
| 73 | # hosting?" when the field may not have been served: absent is not false. | ||||||||||||||
| 74 | 396 | ▶ | sub has { | ||||||||||||
|
Subroutine
has: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 75 | 396 | my ($self, $field) = @_; | |||||||||||||
| 76 | 396 | ▶ | Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 395 | ▶ | return exists $self->{$field} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | # The fields this answer actually carried, in wire order. Useful for seeing what | ||||||||||||||
| 81 | # a key is entitled to without reading the plan table. | ||||||||||||||
| 82 | 3 | ▶ | sub fields { | ||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 83 | 3 | my ($self) = @_; | |||||||||||||
| 84 | 3 | return grep { exists $self->{$_} } @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | # The response exactly as it came off the wire, with its original names and its | ||||||||||||||
| 88 | # JSON booleans intact. Empty for a bogon, which was never served. | ||||||||||||||
| 89 | 5 | ▶ | sub raw { | ||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 90 | 5 | return $_[0]->{_raw}; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 28737 | ▶ | sub _new { | ||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||
| 94 | 28737 | my ($class, $answer, $raw) = @_; | |||||||||||||
| 95 | 28737 | $answer->{_raw} = $raw; | |||||||||||||
| 96 | 28737 | return bless $answer, $class; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent | ||||||||||||||
| 100 | # "Can't locate object method" rather than a silent undef. | ||||||||||||||
| 101 | 13 | for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @VERDICTS, @MARKERS) { | |||||||||||||
| 102 | 13 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 103 | 5260 | ▶ | *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 13 | 1; | |||||||||||||
| 107 | |||||||||||||||
| 108 | __END__ | ||||||||||||||