Lookup/Iplocation.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Lookup/Ip.pm ↑ Summary Lookup/Ipoption.pm »
1 #
2 # $Id$
3 #
4 # lookup::iplocation Brik
5 #
6 package Metabrik::Lookup::Iplocation;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable location ipv4 ipv6 ip geo geolocation) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 _na => [ qw(INTERNAL) ],
21 },
22 commands => {
23 update => [ ],
24 from_ip => [ qw(ip_address) ],
25 from_ipv4 => [ qw(ipv4_address) ],
26 from_ipv6 => [ qw(ipv6_address) ],
27 subnet4 => [ qw(ipv4_address) ],
28 organization_name => [ qw(ip_address) ],
29 range_from_ipv4 => [ qw(ipv4_address) ],
30 networks_from_ipv4 => [ qw(ipv4_address) ],
31 },
32 require_modules => {
33 'Geo::IP' => [ ],
34 'Metabrik::Client::Www' => [ ],
35 'Metabrik::File::Compress' => [ ],
36 'Metabrik::Network::Address' => [ ],
37 },
38 need_packages => {
39 ubuntu => [ qw(libgeoip-dev) ],
40 debian => [ qw(libgeoip-dev) ],
41 kali => [ qw(libgeoip-dev) ],
42 freebsd => [ qw(net/GeoIP) ],
43 },
44 };
45 }
46  
47 sub brik_init {
48 my $self = shift;
49  
50 my $init = $self->SUPER::brik_init or return;
51  
52 my $na = Metabrik::Network::Address->new_from_brik_init($self) or return;
53 $self->_na($na);
54  
55 return 1;
56 }
57  
58 sub update {
59 my $self = shift;
60  
61 my $datadir = $self->datadir;
62  
63 my $dl_path = 'http://geolite.maxmind.com/download/geoip/database/';
64  
65 my %mirror = (
66 'GeoIP.dat.gz' => 'GeoLiteCountry/GeoIP.dat.gz',
67 'GeoIPv6.dat.gz' => 'GeoIPv6.dat.gz',
68 'GeoLiteCity.dat.gz' => 'GeoLiteCity.dat.gz',
69 'GeoLiteCityv6.dat.gz' => 'GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz',
70 'GeoIPASNum.dat.gz' => 'asnum/GeoIPASNum.dat.gz',
71 'GeoIPASNumv6.dat.gz' => 'asnum/GeoIPASNumv6.dat.gz',
72 );
73  
74 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
75 $cw->user_agent("Metabrik-MaxMind-geolite-mirror/1.01");
76 $cw->datadir($datadir);
77  
78 my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
79 $fc->datadir($datadir);
80  
81 my @updated = ();
82 for my $f (keys %mirror) {
83 my $files = $cw->mirror($dl_path.$mirror{$f}, $f) or next;
84 for my $file (@$files) {
85 (my $outfile = $file) =~ s/\.gz$//;
86 $self->log->verbose("update: uncompressing to [$outfile]");
87 $fc->uncompress($file, $outfile) or next;
88 push @updated, $outfile;
89 }
90 }
91  
92 return \@updated;
93 }
94  
95 sub from_ipv4 {
96 my $self = shift;
97 my ($ipv4) = @_;
98  
99 $self->brik_help_run_undef_arg('from_ipv4', $ipv4) or return;
100  
101 my $na = $self->_na;
102  
103 my $gi = Geo::IP->open($self->datadir.'/GeoLiteCity.dat', Geo::IP::GEOIP_STANDARD())
104 or return $self->log->error("from_ipv4: unable to open GeoLiteCity.dat");
105  
106 my $gi_asn = Geo::IP->open($self->datadir.'/GeoIPASNum.dat', Geo::IP::GEOIP_STANDARD())
107 or return $self->log->error("from_ipv4: unable to open GeoIPASNum.dat");
108  
109 my $record;
110 eval {
111 $record = $gi->record_by_addr($ipv4);
112 };
113 if ($@ || ! defined($record)) {
114 chomp($@);
115 return $self->log->error("from_ipv4: unable to find info for IPv4 [$ipv4]");
116 }
117  
118 my $h = {};
119 $h->{country_code} = $record->country_code;
120 $h->{country_code3} = $record->country_code3;
121 $h->{country_name} = $record->country_name;
122 $h->{region} = $record->region;
123 $h->{region_name} = $record->region_name;
124 $h->{city} = $record->city;
125 $h->{postal_code} = $record->postal_code;
126 $h->{latitude} = $record->latitude;
127 $h->{longitude} = $record->longitude;
128 $h->{time_zone} = $record->time_zone;
129 $h->{area_code} = $record->area_code;
130 $h->{continent_code} = $record->continent_code;
131 $h->{metro_code} = $record->metro_code;
132  
133 my $asn = '';
134 my $organization = '';
135 my $asn_organization = $gi_asn->name_by_addr($ipv4);
136 if ($asn_organization) {
137 ($asn, $organization) = $asn_organization =~ m{^(\S+)(?:\s+(.*))?$};
138 $asn ||= $asn_organization; # Not able to parse, we put it raw.
139 }
140 $asn ||= 'undef';
141 $organization ||= 'undef';
142  
143 my ($from, $to) = $gi->range_by_ip($ipv4);
144 if (! defined($from) || ! defined($to)) {
145 return $self->log->error("from_ipv4: unable to find range for IPv4 [$ipv4]");
146 }
147  
148 my $network = $na->range_to_cidr($from, $to) or return;
149 my $network_list = join('|', @$network);
150  
151 # Add other info and return
152 $h->{asn} = $asn;
153 $h->{organization} = $organization;
154 $h->{first_ip} = $from;
155 $h->{last_ip} = $to;
156 $h->{networks} = $network_list;
157  
158 # If not defined, we set to 0, as this should be a number.
159 $h->{dma_code} ||= 0;
160 $h->{area_code} ||= 0;
161 $h->{metro_code} ||= 0;
162  
163 # Set as undef if nothing found
164 for my $k (keys %$h) {
165 if (! defined($h->{$k}) || ! length($h->{$k})) {
166 $h->{$k} = 'undef';
167 }
168 }
169  
170 return $h;
171 }
172  
173 sub from_ipv6 {
174 my $self = shift;
175 my ($ipv6) = @_;
176  
177 $self->brik_help_run_undef_arg('from_ipv6', $ipv6) or return;
178  
179 my $na = $self->_na;
180  
181 my $gi = Geo::IP->open($self->datadir.'/GeoLiteCityv6.dat', Geo::IP::GEOIP_STANDARD())
182 or return $self->log->error("from_ipv6: unable to open GeoLiteCityv6.dat");
183  
184 my $gi_asn = Geo::IP->open($self->datadir.'/GeoIPASNumv6.dat',
185 Geo::IP::GEOIP_STANDARD())
186 or return $self->log->error("from_ipv6: unable to open GeoIPASNumv6.dat");
187  
188 my $record;
189 eval {
190 $record = $gi->record_by_addr_v6($ipv6);
191 };
192 if ($@ || ! defined($record)) {
193 chomp($@);
194 return $self->log->error("from_ipv6: unable to find info for IPv6 [$ipv6]");
195 }
196  
197 my $h = {};
198 $h->{country_code} = $record->country_code;
199 $h->{country_code3} = $record->country_code3;
200 $h->{country_name} = $record->country_name;
201 $h->{region} = $record->region;
202 $h->{region_name} = $record->region_name;
203 $h->{city} = $record->city;
204 $h->{postal_code} = $record->postal_code;
205 $h->{latitude} = $record->latitude;
206 $h->{longitude} = $record->longitude;
207 $h->{time_zone} = $record->time_zone;
208 $h->{area_code} = $record->area_code;
209 $h->{continent_code} = $record->continent_code;
210 $h->{metro_code} = $record->metro_code;
211  
212 my $asn = '';
213 my $organization = '';
214 my $asn_organization = $gi_asn->name_by_addr_v6($ipv6);
215 if ($asn_organization) {
216 ($asn, $organization) = $asn_organization =~ m{^(\S+)(?:\s+(.*))?$};
217 $asn ||= $asn_organization; # Not able to parse, we put it raw.
218 }
219 $asn ||= 'undef';
220 $organization ||= 'undef';
221  
222 #my ($from, $to) = $gi->range_by_ip($ipv6);
223 #if (! defined($from) || ! defined($to)) {
224 #return $self->log->error("from_ipv6: unable to find range for IPv6 [$ipv6]");
225 #}
226  
227 #my $network = $na->range_to_cidr($from, $to) or return;
228 my $network = [ 'undef' ]; # Not avail in IPv6 for now.
229 my $network_list = join('|', @$network);
230 $h->{first_ip} = 'undef';
231 $h->{last_ip} = 'undef';
232  
233 # Add other info and return
234 $h->{asn} = $asn;
235 $h->{organization} = $organization;
236 $h->{networks} = $network_list;
237  
238 # If not defined, we set to 0, as this should be a number.
239 $h->{dma_code} ||= 0;
240 $h->{area_code} ||= 0;
241 $h->{metro_code} ||= 0;
242  
243 # Set as undef if nothing found
244 for my $k (keys %$h) {
245 if (! defined($h->{$k}) || ! length($h->{$k})) {
246 $h->{$k} = 'undef';
247 }
248 }
249  
250 return $h;
251 }
252  
253 sub from_ip {
254 my $self = shift;
255 my ($ip) = @_;
256  
257 $self->brik_help_run_undef_arg('from_ip', $ip) or return;
258  
259 my $na = $self->_na;
260 if ($na->is_ipv4($ip)) {
261 return $self->from_ipv4($ip);
262 }
263 elsif ($na->is_ipv6($ip)) {
264 return $self->from_ipv6($ip);
265 }
266  
267 $self->log->info("from_ip: IP [$ip] is not a valid IP address");
268  
269 return 0;
270 }
271  
272 sub subnet4 {
273 my $self = shift;
274 my ($ipv4_address) = @_;
275  
276 $self->brik_help_run_undef_arg('subnet4', $ipv4_address) or return;
277  
278 my $gi = Geo::IP->open($self->datadir.'/GeoLiteCity.dat', Geo::IP::GEOIP_STANDARD())
279 or return $self->log->error("subnet4: unable to open GeoLiteCity.dat");
280  
281 my ($from, $to) = $gi->range_by_ip($ipv4_address);
282  
283 return [ $from, $to ];
284 }
285  
286 sub organization_name {
287 my $self = shift;
288 my ($ip_address) = @_;
289  
290 $self->brik_help_run_undef_arg('organization_name', $ip_address) or return;
291
292 my $gi = Geo::IP->open($self->datadir.'/GeoLiteCity.dat', Geo::IP::GEOIP_STANDARD())
293 or return $self->log->error("organization_name: unable to open GeoLiteCity.dat");
294  
295 my $record = $gi->name_by_addr($ip_address);
296  
297 return $record;
298 }
299  
300 sub range_from_ipv4 {
301 my $self = shift;
302 my ($ipv4) = @_;
303  
304 $self->brik_help_run_undef_arg('range_from_ipv4', $ipv4) or return;
305  
306 my $gi = Geo::IP->open($self->datadir.'/GeoLiteCity.dat', Geo::IP::GEOIP_STANDARD())
307 or return $self->log->error("range_from_ipv4: unable to open GeoLiteCity.dat");
308  
309 my ($from, $to) = $gi->range_by_ip($ipv4);
310  
311 return [ $from, $to ];
312 }
313  
314 sub networks_from_ipv4 {
315 my $self = shift;
316 my ($ipv4) = @_;
317  
318 $self->brik_help_run_undef_arg('networks_from_ipv4', $ipv4) or return;
319  
320 my $range = $self->range_from_ipv4($ipv4) or return;
321  
322 my $na = $self->_na;
323  
324 return $na->range_to_cidr($range->[0], $range->[1]);
325 }
326  
327 1;
328  
329 __END__
« Lookup/Ip.pm ↑ Summary Lookup/Ipoption.pm »