| 1 | package Geo::IPinfoPlus; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Cache::LRU; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use HTTP::Headers; | ||
| 9 | use JSON; | ||
| 10 | use Geo::DetailsPlus; | ||
| 11 | use Net::CIDR; | ||
| 12 | use Net::CIDR::Set; | ||
| 13 | |||
| 14 | our $VERSION = '3.4.0'; | ||
| 15 | use constant DEFAULT_CACHE_MAX_SIZE => 4096; | ||
| 16 | use constant DEFAULT_CACHE_TTL => 86_400; | ||
| 17 | use constant DEFAULT_TIMEOUT => 2; | ||
| 18 | use constant HTTP_TOO_MANY_REQUEST => 429; | ||
| 19 | |||
| 20 | my $base_url = 'https://api.ipinfo.io/lookup/'; | ||
| 21 | |||
| 22 | my @ip4_bogon_networks = ( | ||
| 23 | '0.0.0.0/8', '10.0.0.0/8', '100.64.0.0/10', '127.0.0.0/8', | ||
| 24 | '169.254.0.0/16', '172.16.0.0/12', '192.0.0.0/24', '192.0.2.0/24', | ||
| 25 | '192.168.0.0/16', '198.18.0.0/15', '198.51.100.0/24', | ||
| 26 | '203.0.113.0/24', '224.0.0.0/4', '240.0.0.0/4', '255.255.255.255/32', | ||
| 27 | ); | ||
| 28 | |||
| 29 | my @ip6_bogon_networks = ( | ||
| 30 | '::/128', '::1/128', '::ffff:0:0/96', '::/96', | ||
| 31 | '100::/64', '2001:10::/28', '2001:db8::/32', 'fc00::/7', | ||
| 32 | 'fe80::/10', 'fec0::/10', 'ff00::/8', '2002::/24', | ||
| 33 | '2002:a00::/24', '2002:7f00::/24', '2002:a9fe::/32', '2002:ac10::/28', | ||
| 34 | '2002:c000::/40', '2002:c000:200::/40', '2002:c0a8::/32', | ||
| 35 | '2002:c612::/31', '2002:c633:6400::/40', '2002:cb00:7100::/40', | ||
| 36 | '2002:e000::/20', '2002:f000::/20', '2002:ffff:ffff::/48', | ||
| 37 | '2001::/40', '2001:0:a00::/40', '2001:0:7f00::/40', | ||
| 38 | '2001:0:a9fe::/48', '2001:0:ac10::/44', '2001:0:c000::/56', | ||
| 39 | '2001:0:c000:200::/56', '2001:0:c0a8::/48', '2001:0:c612::/47', | ||
| 40 | '2001:0:c633:6400::/56', '2001:0:cb00:7100::/56', '2001:0:e000::/36', | ||
| 41 | '2001:0:f000::/36', '2001:0:ffff:ffff::/64', | ||
| 42 | ); | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ( $class, %options ) = @_; | ||
| 46 | |||
| 47 | my $token = defined $options{token} ? $options{token} : ''; | ||
| 48 | |||
| 49 | my $cache_maxsize = | ||
| 50 | defined $options{cache_maxsize} | ||
| 51 | ? $options{cache_maxsize} | ||
| 52 | : DEFAULT_CACHE_MAX_SIZE; | ||
| 53 | |||
| 54 | my $cache_ttl = | ||
| 55 | defined $options{cache_ttl} | ||
| 56 | ? $options{cache_ttl} | ||
| 57 | : DEFAULT_CACHE_TTL; | ||
| 58 | |||
| 59 | my $timeout = | ||
| 60 | defined $options{timeout} | ||
| 61 | ? $options{timeout} | ||
| 62 | : DEFAULT_TIMEOUT; | ||
| 63 | |||
| 64 | my $header = HTTP::Headers->new(); | ||
| 65 | $header->header( 'User-Agent' => 'IPinfoClient/Perl/3.4.0' ); | ||
| 66 | $header->header( 'Accept' => 'application/json' ); | ||
| 67 | $header->header( 'Content-Type' => 'application/json' ); | ||
| 68 | |||
| 69 | if ($token) { | ||
| 70 | $header->header( 'Authorization' => 'Bearer ' . $token ); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $ua = LWP::UserAgent->new( | ||
| 74 | timeout => $timeout, | ||
| 75 | show_progress => 0, | ||
| 76 | ); | ||
| 77 | |||
| 78 | $ua->default_headers($header); | ||
| 79 | |||
| 80 | my $cache; | ||
| 81 | if ( defined $options{cache} ) { | ||
| 82 | $cache = $options{cache}; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $cache = _build_cache( __PACKAGE__, cache_maxsize => $cache_maxsize, cache_ttl => $cache_ttl ); | ||
| 86 | } | ||
| 87 | |||
| 88 | my $self = { | ||
| 89 | token => $token, | ||
| 90 | base_url => $base_url, | ||
| 91 | ua => $ua, | ||
| 92 | cache => $cache, | ||
| 93 | cache_ttl => $cache_ttl, | ||
| 94 | message => '', | ||
| 95 | }; | ||
| 96 | |||
| 97 | return bless $self, $class; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub info { | ||
| 101 | my ( $self, $ip ) = @_; | ||
| 102 | |||
| 103 | return $self->_get_info( $ip, ); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub _get_info { | ||
| 108 | my ( $self, $ip ) = @_; | ||
| 109 | |||
| 110 | $ip = defined $ip ? $ip : ''; | ||
| 111 | |||
| 112 | if ( $ip ne '' ) { | ||
| 113 | my $validated_ip = Net::CIDR::cidrvalidate($ip); | ||
| 114 | if ( !defined $validated_ip ) { | ||
| 115 | $self->{message} = 'Invalid IP address'; | ||
| 116 | return undef; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | my ( $info, $message ) = $self->_lookup_info( $ip ); | ||
| 121 | $self->{message} = $message; | ||
| 122 | return $info if eval { $info->isa('Geo::DetailsPlus') }; | ||
| 123 | |||
| 124 | return defined $info ? Geo::DetailsPlus->new( $info ) : undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _lookup_info { | ||
| 128 | my ( $self, $ip ) = @_; | ||
| 129 | |||
| 130 | # checking bogon IP and returning response locally. | ||
| 131 | if ( $ip ne '' ) { | ||
| 132 | if ( _is_bogon($ip) ) { | ||
| 133 | my $details = {}; | ||
| 134 | $details->{ip} = $ip; | ||
| 135 | $details->{bogon} = "True"; | ||
| 136 | return ( $details, '' ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | my ( $info, $message ); | ||
| 141 | my $cache_key = 'plus_' . $ip; | ||
| 142 | |||
| 143 | if ( !defined $self->{cache} ) { | ||
| 144 | ( $info, $message ) = $self->_lookup_info_from_source($ip); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | ( $info, $message ) = $self->_lookup_info_from_cache( $ip, $cache_key ); | ||
| 148 | |||
| 149 | if ( !defined $info ) { | ||
| 150 | ( $info, $message ) = $self->_lookup_info_from_source($ip); | ||
| 151 | |||
| 152 | if ( defined $info && ref $info eq 'HASH' && !exists $info->{bogon} ) { | ||
| 153 | $self->{cache}->set( $cache_key => $info, $self->{cache_ttl} ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | return ( $info, $message ); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _lookup_info_from_cache { | ||
| 162 | my ( $self, $ip, $cache_key ) = @_; | ||
| 163 | |||
| 164 | my $info = $self->{cache}->get($cache_key); | ||
| 165 | |||
| 166 | if ( !defined $info ) { | ||
| 167 | return ( undef, '' ); | ||
| 168 | } | ||
| 169 | |||
| 170 | return ( $info, '' ); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _lookup_info_from_source { | ||
| 174 | my ( $self, $ip ) = @_; | ||
| 175 | |||
| 176 | my $url = ''; | ||
| 177 | if ( $ip ) { | ||
| 178 | $url = $self->{base_url} . $ip; | ||
| 179 | } else { | ||
| 180 | $url = $self->{base_url} . "me"; | ||
| 181 | } | ||
| 182 | |||
| 183 | my $response = $self->{ua}->get($url); | ||
| 184 | |||
| 185 | if ( $response->is_success ) { | ||
| 186 | |||
| 187 | my $content_type = $response->header('Content-Type') || ''; | ||
| 188 | my $info; | ||
| 189 | |||
| 190 | if ( $content_type =~ m{application/json}i ) { | ||
| 191 | eval { $info = from_json( $response->decoded_content ); }; | ||
| 192 | if ($@) { | ||
| 193 | return ( undef, 'Error parsing JSON response.' ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | $info = $response->decoded_content; | ||
| 198 | chomp($info); | ||
| 199 | } | ||
| 200 | |||
| 201 | return ( $info, '' ); | ||
| 202 | } | ||
| 203 | if ( $response->code == HTTP_TOO_MANY_REQUEST ) { | ||
| 204 | return ( undef, 'Your monthly request quota has been exceeded.' ); | ||
| 205 | } | ||
| 206 | |||
| 207 | return ( undef, $response->status_line ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _build_cache { | ||
| 211 | my ( $pkg, %options ) = @_; | ||
| 212 | |||
| 213 | my $cache_maxsize = defined $options{cache_maxsize} ? $options{cache_maxsize} : DEFAULT_CACHE_MAX_SIZE; | ||
| 214 | my $cache_ttl = defined $options{cache_ttl} ? $options{cache_ttl} : DEFAULT_CACHE_TTL; | ||
| 215 | |||
| 216 | my $cache = Cache::LRU->new( size => $cache_maxsize ); | ||
| 217 | |||
| 218 | return $cache; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub _is_bogon { | ||
| 222 | my $ip = shift; | ||
| 223 | |||
| 224 | my $ip_is_bogon = 0; | ||
| 225 | |||
| 226 | if ( $ip =~ /:/ ) { # IPv6 address | ||
| 227 | my $ip6_bogon_cidr_set = Net::CIDR::Set->new(); | ||
| 228 | $ip6_bogon_cidr_set->add($_) foreach (@ip6_bogon_networks); | ||
| 229 | $ip_is_bogon = $ip6_bogon_cidr_set->contains($ip); | ||
| 230 | } | ||
| 231 | else { # IPv4 address | ||
| 232 | my $ip4_bogon_cidr_set = Net::CIDR::Set->new(); | ||
| 233 | $ip4_bogon_cidr_set->add($_) foreach (@ip4_bogon_networks); | ||
| 234 | $ip_is_bogon = $ip4_bogon_cidr_set->contains($ip); | ||
| 235 | } | ||
| 236 | |||
| 237 | return $ip_is_bogon; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | __END__ |