line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geo::IPinfo; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
52507
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
5
|
1
|
|
|
1
|
|
17
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
543
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
39721
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
522
|
use JSON; |
|
1
|
|
|
|
|
7886
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.0'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my %valid_fields = ( |
13
|
|
|
|
|
|
|
ip => 1, |
14
|
|
|
|
|
|
|
hostname => 1, |
15
|
|
|
|
|
|
|
city => 1, |
16
|
|
|
|
|
|
|
region => 1, |
17
|
|
|
|
|
|
|
country => 1, |
18
|
|
|
|
|
|
|
loc => 1, |
19
|
|
|
|
|
|
|
org => 1, |
20
|
|
|
|
|
|
|
postal => 1, |
21
|
|
|
|
|
|
|
phone => 1 |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
my $base_url = 'https://ipinfo.io/'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new |
28
|
|
|
|
|
|
|
{ |
29
|
0
|
|
|
0
|
1
|
|
my ($pkg, $token) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $self = {}; |
32
|
0
|
0
|
|
|
|
|
$self->{token} = defined $token ? "?token=$token" : ""; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$self->{base_url} = $base_url; |
35
|
0
|
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new; |
36
|
0
|
|
|
|
|
|
$self->{ua}->agent("curl/Geo::IP $VERSION"); |
37
|
0
|
|
|
|
|
|
$self->{message} = ""; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
bless($self, $pkg); |
40
|
0
|
|
|
|
|
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub info |
46
|
|
|
|
|
|
|
{ |
47
|
0
|
|
|
0
|
1
|
|
my ($self, $ip) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
return $self->_getinfo($ip, ""); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub geo |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
1
|
|
my ($self, $ip) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return $self->_getinfo($ip, "geo"); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub field |
64
|
|
|
|
|
|
|
{ |
65
|
0
|
|
|
0
|
1
|
|
my ($self, $ip, $field) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
if (not defined $ip) |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
|
$self->{message} = "IP is undefined"; |
70
|
0
|
|
|
|
|
|
return undef; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
if (not defined $field) |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
|
|
|
$self->{message} = "field() requires 2 arguments"; |
76
|
0
|
|
|
|
|
|
return undef; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (not defined $valid_fields{$field}) |
80
|
|
|
|
|
|
|
{ |
81
|
0
|
|
|
|
|
|
$self->{message} = "Invalid field: $field"; |
82
|
0
|
|
|
|
|
|
return undef; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $url = $self->{base_url} . $ip . "/" . $field . $self->{token}; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $res = $self->{ua}->get($url); |
88
|
0
|
0
|
|
|
|
|
if ($res->is_success) |
89
|
|
|
|
|
|
|
{ |
90
|
0
|
|
|
|
|
|
$self->{message} = ""; |
91
|
0
|
|
|
|
|
|
my $value = $res->decoded_content; |
92
|
0
|
|
|
|
|
|
chomp $value; |
93
|
0
|
|
|
|
|
|
return $value; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else |
96
|
|
|
|
|
|
|
{ |
97
|
0
|
|
|
|
|
|
$self->{message} = $res->status_line; |
98
|
0
|
|
|
|
|
|
return undef; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub error_msg |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return $self->{message}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
112
|
|
|
|
|
|
|
#-- private method(s) below , don't call them directly ------------------------- |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _getinfo |
115
|
|
|
|
|
|
|
{ |
116
|
0
|
|
|
0
|
|
|
my ($self, $ip, $type) = @_; |
117
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
if (not defined $ip) |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
|
|
|
|
|
$self->{message} = "IP is undefined"; |
121
|
0
|
|
|
|
|
|
return undef; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my $url = $self->{base_url} . $ip . "/" . $type . $self->{token}; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $res = $self->{ua}->get($url); |
127
|
0
|
0
|
|
|
|
|
if ($res->is_success) |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
|
|
|
$self->{message} = ""; |
130
|
0
|
|
|
|
|
|
return from_json($res->decoded_content); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
else |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
|
|
|
|
|
$self->{message} = $res->status_line; |
135
|
0
|
|
|
|
|
|
return undef; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
__END__ |