diff --git a/.gitignore b/.gitignore index cf91060..0ab7b09 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ ./t +.vscode diff --git a/Geo-IPinfo/lib/Geo/IPinfo.pm b/Geo-IPinfo/lib/Geo/IPinfo.pm index a90eac1..4ac5dc2 100644 --- a/Geo-IPinfo/lib/Geo/IPinfo.pm +++ b/Geo-IPinfo/lib/Geo/IPinfo.pm @@ -35,6 +35,7 @@ my %valid_fields = ( domains => 1, ); my $base_url = 'https://ipinfo.io/'; +my $base_url_ipv6 = 'https://v6.ipinfo.io/'; my $country_flag_url = 'https://cdn.ipinfo.io/static/images/countries-flags/'; my $cache_ttl = 0; my $custom_cache = 0; @@ -1060,8 +1061,9 @@ sub new { my $self = {}; $token = defined $token ? $token : ''; - $self->{base_url} = $base_url; - $self->{ua} = LWP::UserAgent->new; + $self->{base_url} = $base_url; + $self->{base_url_ipv6} = $base_url_ipv6; + $self->{ua} = LWP::UserAgent->new; $self->{ua}->ssl_opts( 'verify_hostname' => 0 ); $self->{ua}->default_headers( HTTP::Headers->new( @@ -1198,7 +1200,9 @@ sub _lookup_info { return ( $cached_info, '' ); } - my ( $source_info, $message ) = $self->_lookup_info_from_source($key); + my $is_ipv6 = 0; + $is_ipv6 = 1 if ( $ip =~ /:/ ); + my ( $source_info, $message ) = $self->_lookup_info_from_source($is_ipv6, $key); if ( not defined $source_info ) { return ( $source_info, $message ); } @@ -1253,9 +1257,15 @@ sub _lookup_info_from_cache { } sub _lookup_info_from_source { - my ( $self, $key ) = @_; - - my $url = $self->{base_url} . $key; + my ( $self, $is_ipv6, $key ) = @_; + + my $url = ''; + if ( $is_ipv6 ) { + $url = $self->{base_url_ipv6} . $key; + } else { + $url = $self->{base_url} . $key; + } + my $response = $self->{ua}->get($url); if ( $response->is_success ) { diff --git a/example.pl b/example.pl index f53e1ea..17edf7b 100755 --- a/example.pl +++ b/example.pl @@ -51,8 +51,20 @@ print $ipinfo->error_msg . "\n"; } +# you can also retrieve information for IPv6 addresses in a similar fasion +my $ipv6_data = $ipinfo->info('2001:4860:4860::8888'); +if ( defined $ipv6_data ) # valid data returned +{ + # print JSON string + my $json = JSON->new->allow_blessed->convert_blessed; + my $json_string = $json->utf8->pretty->encode($ipv6_data); + print $json_string . "\n"; +} +else # invalid data obtained, show error message +{ + print $ipinfo->error_msg . "\n"; +} + # retrieve only city information of the IP address my $details = $ipinfo->field( '8.8.8.8', 'city' ); - print "The city of 8.8.8.8 is " . $details->city . "\n"; -