| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::IPInfoDB; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
4
|
|
|
|
|
|
|
# Net::IPInfoDB - fetch ip info form ipinfodb.com |
|
5
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
920
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
48
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION $DEBUG @FIELDS); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
72
|
|
|
9
|
1
|
|
|
1
|
|
15
|
use vars qw($API_URI $API_VERSION); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use vars qw($WEB_URI); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
7278
|
use LWP::Simple qw(get); |
|
|
1
|
|
|
|
|
199299
|
|
|
|
1
|
|
|
|
|
18
|
|
|
13
|
1
|
|
|
1
|
|
250
|
use URI; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
690
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$VERSION = '3.0'; |
|
16
|
|
|
|
|
|
|
$DEBUG = 0 unless defined $DEBUG; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$API_URI = 'http://api.ipinfodb.com'; |
|
19
|
|
|
|
|
|
|
$API_VERSION = 'v3'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$WEB_URI = 'http://ipinfodb.com/ip_locator.php?ip='; |
|
22
|
|
|
|
|
|
|
@FIELDS = qw(status_code status_message ip_address country_code country_name |
|
23
|
|
|
|
|
|
|
region_name city_name zip_code latitude longitude timezone); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
|
26
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $self = bless { |
|
29
|
|
|
|
|
|
|
_KEY => '', |
|
30
|
|
|
|
|
|
|
_ERROR => '', |
|
31
|
|
|
|
|
|
|
} => $class; |
|
32
|
|
|
|
|
|
|
|
|
33
|
0
|
0
|
|
|
|
|
if (@_) { |
|
|
|
0
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$self->key($_[0]); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
elsif (my $k = $ENV{'IPINFODB_TOKEN'}) { |
|
37
|
0
|
|
|
|
|
|
$self->key($k); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
return $self; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub key { |
|
44
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
if (@_) { |
|
47
|
0
|
|
|
|
|
|
$self->{ _KEY } = $_[0]; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return $self->{ _KEY }; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub error { |
|
54
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
55
|
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
if (@_) { |
|
57
|
0
|
|
|
|
|
|
$self->{'_ERROR'} = "@_"; |
|
58
|
0
|
|
|
|
|
|
return; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
return $self->{'_ERROR'}; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get_city { |
|
65
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
66
|
0
|
|
|
|
|
|
my $host = shift; |
|
67
|
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return $self->_get('ip-city', $host); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub get_country { |
|
72
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
73
|
0
|
|
|
|
|
|
my $host = shift; |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return $self->_get('ip-country', $host); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _get { |
|
79
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
80
|
0
|
|
|
|
|
|
my $meth = shift; |
|
81
|
0
|
|
|
|
|
|
my $addr = shift; |
|
82
|
0
|
|
|
|
|
|
my $raw; |
|
83
|
0
|
|
|
|
|
|
my $res = Net::IPInfoDB::Result->new; |
|
84
|
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $uri = URI->new("$API_URI/$API_VERSION/$meth"); |
|
86
|
0
|
|
|
|
|
|
$uri->query_form( |
|
87
|
|
|
|
|
|
|
format => "raw", |
|
88
|
|
|
|
|
|
|
key => $self->key, |
|
89
|
|
|
|
|
|
|
ip => $addr, |
|
90
|
|
|
|
|
|
|
); |
|
91
|
|
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
if ($raw = get($uri)) { |
|
93
|
0
|
|
|
|
|
|
my @f = split /;/, $raw; |
|
94
|
0
|
|
|
|
|
|
for (my $i = 0; $i < @f; $i++) { |
|
95
|
0
|
|
|
|
|
|
my $m = $FIELDS[ $i ]; |
|
96
|
0
|
|
|
|
|
|
$res->$m($f[ $i ]); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
if ($res->status_code ne "OK") { |
|
102
|
0
|
|
|
|
|
|
return $self->error($res->status_message); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$res->web_uri("$WEB_URI$addr"); |
|
106
|
0
|
|
|
|
|
|
$res->_raw($raw); |
|
107
|
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return $res; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
package Net::IPInfoDB::Result; |
|
112
|
|
|
|
|
|
|
|
|
113
|
1
|
|
|
1
|
|
3231
|
use Class::Struct; |
|
|
1
|
|
|
|
|
25736
|
|
|
|
1
|
|
|
|
|
10
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
struct( |
|
116
|
|
|
|
|
|
|
(map { $_ => '$' } @Net::IPInfoDB::FIELDS), |
|
117
|
|
|
|
|
|
|
'web_uri' => '$', |
|
118
|
|
|
|
|
|
|
'_raw' => '$', |
|
119
|
|
|
|
|
|
|
); |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub fields { |
|
122
|
0
|
|
|
0
|
|
|
return @Net::IPInfoDB::FIELDS; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__END__ |