| 1 | package VPNDetection::Result; | ||||||||||||||
| 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 | |||||||||||||||
| 8 | 7 | our $VERSION = '3.14.0'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 7 | our @FLAGS = qw( | |||||||||||||
| 11 | is_vpn is_hosting is_relay is_tor is_cdn is_resproxy is_dcproxy is_mobproxy | ||||||||||||||
| 12 | is_pubproxy | ||||||||||||||
| 13 | ); | ||||||||||||||
| 14 | 7 | our @DETAILS = qw(vpn hosting relay tor cdn resproxy dcproxy mobproxy pubproxy); | |||||||||||||
| 15 | # Where the address is and the AS announcing it, on every answer, with or without | ||||||||||||||
| 16 | # a key, each with every member present: an empty string, or AS 0, where nothing | ||||||||||||||
| 17 | # is known. | ||||||||||||||
| 18 | 7 | our @OBJECTS = qw(location asn); | |||||||||||||
| 19 | 7 | our @LOCATION = qw( | |||||||||||||
| 20 | continent continent_name country country_name timezone currency_code currency_name | ||||||||||||||
| 21 | currency_symbol flag_emoji flag_unicode | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | 7 | our @ASN = qw(asn name domain type prefix); | |||||||||||||
| 24 | # On answers to a key on the free plan only, and so never on a bogon's: false | ||||||||||||||
| 25 | # while its organization's daily trial lasts (every field for the first 100 | ||||||||||||||
| 26 | # lookups each UTC day), true once the answer has Unlimited's. | ||||||||||||||
| 27 | 7 | our @MARKERS = qw(trial_expired); | |||||||||||||
| 28 | |||||||||||||||
| 29 | 7 | my %KNOWN = map { $_ => 1 } 'ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS; | |||||||||||||
| 30 | |||||||||||||||
| 31 | # The one place a wire body becomes a result. Every field is copied on key | ||||||||||||||
| 32 | # PRESENCE and never on truthiness: a plan that includes `is_hosting` and | ||||||||||||||
| 33 | # answers false must keep it, and a plan that does not include it must not gain | ||||||||||||||
| 34 | # it. In Perl those two are one careless `if` apart, since undef, 0, '' and a | ||||||||||||||
| 35 | # missing key are all false. | ||||||||||||||
| 36 | 27628 | ▶ | sub from_wire { | ||||||||||||
|
Subroutine
from_wire: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||
| 37 | 27628 | my ($class, $body) = @_; | |||||||||||||
| 38 | 27628 | ▶ | my %answer = (ip => $body->{ip}, is_bogon => $body->{is_bogon} ? 1 : 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | # A served null stays undef, as a detail's does: read as 0 it would say the | ||||||||||||||
| 40 | # address is no hosting server, or that the trial lasts, when nothing was said. | ||||||||||||||
| 41 | 27628 | for my $flag (@FLAGS, @MARKERS) { | |||||||||||||
| 42 | 276280 | ▶ | next unless exists $body->{$flag}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 27677 | ▶ | $answer{$flag} = defined $body->{$flag} ? ($body->{$flag} ? 1 : 0) : undef; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 44 | } | ||||||||||||||
| 45 | 27628 | for my $detail (@DETAILS, @OBJECTS) { | |||||||||||||
| 46 | 303908 | ▶ | $answer{$detail} = $body->{$detail} if exists $body->{$detail}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | } | ||||||||||||||
| 48 | 27628 | return $class->_new(\%answer, $body); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | # Whether your plan carries this field at all, which is a different question | ||||||||||||||
| 52 | # from what the field says. `has` is the only honest answer to "is this address | ||||||||||||||
| 53 | # hosting?" when the field may not have been served: absent is not false. | ||||||||||||||
| 54 | 173 | ▶ | sub has { | ||||||||||||
|
Subroutine
has: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 55 | 173 | my ($self, $field) = @_; | |||||||||||||
| 56 | 173 | ▶ | Carp::croak("VPNDetection::Result::has: unknown field '$field'") unless $KNOWN{$field}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 172 | ▶ | return exists $self->{$field} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | # The fields this answer actually carried, in wire order. Useful for seeing what | ||||||||||||||
| 61 | # a key is entitled to without reading the plan table. | ||||||||||||||
| 62 | 1 | ▶ | sub fields { | ||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 63 | 1 | my ($self) = @_; | |||||||||||||
| 64 | 1 | return grep { exists $self->{$_} } @FLAGS, @DETAILS, @OBJECTS, @MARKERS; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | # The response exactly as it came off the wire, with its original names and its | ||||||||||||||
| 68 | # JSON booleans intact. Empty for a bogon, which was never served. | ||||||||||||||
| 69 | 2 | ▶ | sub raw { | ||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 70 | 2 | return $_[0]->{_raw}; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 27634 | ▶ | sub _new { | ||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | 27634 | my ($class, $answer, $raw) = @_; | |||||||||||||
| 75 | 27634 | $answer->{_raw} = $raw; | |||||||||||||
| 76 | 27634 | return bless $answer, $class; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | # One reader per field rather than AUTOLOAD, so a typo is a compile-adjacent | ||||||||||||||
| 80 | # "Can't locate object method" rather than a silent undef. | ||||||||||||||
| 81 | 7 | for my $field ('ip', 'is_bogon', @FLAGS, @DETAILS, @OBJECTS, @MARKERS) { | |||||||||||||
| 82 | 7 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 83 | 2774 | ▶ | *{__PACKAGE__ . "::$field"} = sub { $_[0]->{$field} }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 7 | 1; | |||||||||||||
| 87 | |||||||||||||||
| 88 | __END__ | ||||||||||||||