Metabrik-Repository

 view release on metacpan or  search on metacpan

lib/Metabrik/Lookup/Iplocation.pm  view on Meta::CPAN

#
# $Id$
#
# lookup::iplocation Brik
#
package Metabrik::Lookup::Iplocation;
use strict;
use warnings;

use base qw(Metabrik);

sub brik_properties {
   return {
      revision => '$Revision$',
      tags => [ qw(unstable location ipv4 ipv6 ip geo geolocation) ],
      author => 'GomoR <GomoR[at]metabrik.org>',
      license => 'http://opensource.org/licenses/BSD-3-Clause',
      attributes => {
         datadir => [ qw(datadir) ],
         _na => [ qw(INTERNAL) ],
      },
      commands => {
         update => [ ],
         from_ip => [ qw(ip_address) ],
         from_ipv4 => [ qw(ipv4_address) ],
         from_ipv6 => [ qw(ipv6_address) ],
         subnet4 => [ qw(ipv4_address) ],
         organization_name => [ qw(ip_address) ],
         range_from_ipv4 => [ qw(ipv4_address) ],
         networks_from_ipv4 => [ qw(ipv4_address) ],
      },
      require_modules => {
         'Geo::IP' => [ ],
         'Metabrik::Client::Www' => [ ],
         'Metabrik::File::Compress' => [ ],
         'Metabrik::Network::Address' => [ ],
      },
      need_packages => {
         ubuntu => [ qw(libgeoip-dev) ],
         debian => [ qw(libgeoip-dev) ],
         kali => [ qw(libgeoip-dev) ],
         freebsd => [ qw(net/GeoIP) ],
      },
   };
}

sub brik_init {
   my $self = shift;

   my $init = $self->SUPER::brik_init or return;

   my $na = Metabrik::Network::Address->new_from_brik_init($self) or return;
   $self->_na($na);

   return 1;
}

sub update {
   my $self = shift;

   my $datadir = $self->datadir;

   my $dl_path = 'http://geolite.maxmind.com/download/geoip/database/';

   my %mirror = (
      'GeoIP.dat.gz' => 'GeoLiteCountry/GeoIP.dat.gz',
      'GeoIPv6.dat.gz' => 'GeoIPv6.dat.gz',
      'GeoLiteCity.dat.gz' => 'GeoLiteCity.dat.gz',
      'GeoLiteCityv6.dat.gz' => 'GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz',
      'GeoIPASNum.dat.gz' => 'asnum/GeoIPASNum.dat.gz',
      'GeoIPASNumv6.dat.gz' => 'asnum/GeoIPASNumv6.dat.gz',
   );

   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
   $cw->user_agent("Metabrik-MaxMind-geolite-mirror/1.01");
   $cw->datadir($datadir);

   my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
   $fc->datadir($datadir);

   my @updated = ();
   for my $f (keys %mirror) {
      my $files = $cw->mirror($dl_path.$mirror{$f}, $f) or next;
      for my $file (@$files) {
         (my $outfile = $file) =~ s/\.gz$//;
         $self->log->verbose("update: uncompressing to [$outfile]");
         $fc->uncompress($file, $outfile) or next;
         push @updated, $outfile;
      }
   }

   return \@updated;
}

sub from_ipv4 {
   my $self = shift;
   my ($ipv4) = @_;

   $self->brik_help_run_undef_arg('from_ipv4', $ipv4) or return;

   my $na = $self->_na;

   my $gi = Geo::IP->open($self->datadir.'/GeoLiteCity.dat', Geo::IP::GEOIP_STANDARD())
      or return $self->log->error("from_ipv4: unable to open GeoLiteCity.dat");

   my $gi_asn = Geo::IP->open($self->datadir.'/GeoIPASNum.dat', Geo::IP::GEOIP_STANDARD())
      or return $self->log->error("from_ipv4: unable to open GeoIPASNum.dat");

   my $record;
   eval {
      $record = $gi->record_by_addr($ipv4);
   };
   if ($@ || ! defined($record)) {
      chomp($@);
      return $self->log->error("from_ipv4: unable to find info for IPv4 [$ipv4]");
   }

   my $h = {};



( run in 0.597 second using v1.01-cache-2.11-cpan-39bf76dae61 )