41 lines
943 B
Perl
41 lines
943 B
Perl
package My::parser::geoip;
|
|
use strict;
|
|
use warnings;
|
|
use IO::Socket::INET;
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $self = {};
|
|
bless ($self,$class);
|
|
$self->{'config'} = shift;
|
|
my $config = $self->{'config'};
|
|
$config->{'logger'}->log('Loaded My::parser::geoip');
|
|
return $self;
|
|
}
|
|
|
|
sub parse {
|
|
my $self = shift;
|
|
my $host = shift;
|
|
return unless($host =~ m/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/);
|
|
$| = 1;
|
|
my $socket = new IO::Socket::INET (
|
|
PeerHost => '10.0.0.100',
|
|
PeerPort => '7777',
|
|
Proto => 'tcp'
|
|
);
|
|
$self->{'config'}->{'logger'}->log("Failed to look up GeoIP for $host, could not connect to GeoIP resolver") unless $socket;
|
|
return { asn => 0, iso => '' } unless $socket;
|
|
my $size = $socket->send($host."\n");
|
|
my $response;
|
|
$socket->recv($response, 1024);
|
|
shutdown($socket,1);
|
|
$socket->close;
|
|
my ($asn,$iso) = split (",", $response);
|
|
my $r = {};
|
|
$r->{'asn'} = $asn;
|
|
$r->{'iso'} = $iso;
|
|
return $r;
|
|
}
|
|
|
|
1;
|