Skip to content

Commit 28e3291

Browse files
committed
added ipv6 support
1 parent 735e47c commit 28e3291

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
./t
2+
.vscode

Geo-IPinfo/lib/Geo/IPinfo.pm

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ my %valid_fields = (
3535
domains => 1,
3636
);
3737
my $base_url = 'https://ipinfo.io/';
38+
my $base_url_ipv6 = 'https://v6.ipinfo.io/';
3839
my $country_flag_url = 'https://cdn.ipinfo.io/static/images/countries-flags/';
3940
my $cache_ttl = 0;
4041
my $custom_cache = 0;
@@ -1060,8 +1061,9 @@ sub new {
10601061
my $self = {};
10611062
$token = defined $token ? $token : '';
10621063

1063-
$self->{base_url} = $base_url;
1064-
$self->{ua} = LWP::UserAgent->new;
1064+
$self->{base_url} = $base_url;
1065+
$self->{base_url_ipv6} = $base_url_ipv6;
1066+
$self->{ua} = LWP::UserAgent->new;
10651067
$self->{ua}->ssl_opts( 'verify_hostname' => 0 );
10661068
$self->{ua}->default_headers(
10671069
HTTP::Headers->new(
@@ -1198,7 +1200,9 @@ sub _lookup_info {
11981200
return ( $cached_info, '' );
11991201
}
12001202

1201-
my ( $source_info, $message ) = $self->_lookup_info_from_source($key);
1203+
my $is_ipv6 = 0;
1204+
$is_ipv6 = 1 if ( $ip =~ /:/ );
1205+
my ( $source_info, $message ) = $self->_lookup_info_from_source($is_ipv6, $key);
12021206
if ( not defined $source_info ) {
12031207
return ( $source_info, $message );
12041208
}
@@ -1253,9 +1257,15 @@ sub _lookup_info_from_cache {
12531257
}
12541258

12551259
sub _lookup_info_from_source {
1256-
my ( $self, $key ) = @_;
1257-
1258-
my $url = $self->{base_url} . $key;
1260+
my ( $self, $is_ipv6, $key ) = @_;
1261+
1262+
my $url = '';
1263+
if ( $is_ipv6 ) {
1264+
$url = $self->{base_url_ipv6} . $key;
1265+
} else {
1266+
$url = $self->{base_url} . $key;
1267+
}
1268+
12591269
my $response = $self->{ua}->get($url);
12601270

12611271
if ( $response->is_success ) {

example.pl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@
5151
print $ipinfo->error_msg . "\n";
5252
}
5353

54+
# you can also retrieve information for IPv6 addresses in a similar fasion
55+
my $ipv6_data = $ipinfo->info('2001:4860:4860::8888');
56+
if ( defined $ipv6_data ) # valid data returned
57+
{
58+
# print JSON string
59+
my $json = JSON->new->allow_blessed->convert_blessed;
60+
my $json_string = $json->utf8->pretty->encode($ipv6_data);
61+
print $json_string . "\n";
62+
}
63+
else # invalid data obtained, show error message
64+
{
65+
print $ipinfo->error_msg . "\n";
66+
}
67+
5468
# retrieve only city information of the IP address
5569
my $details = $ipinfo->field( '8.8.8.8', 'city' );
56-
5770
print "The city of 8.8.8.8 is " . $details->city . "\n";
58-

0 commit comments

Comments
 (0)