Merge pull request #146 from msimerson/geoip

Geoip: include a script to update GeoIP DBs
This commit is contained in:
Jared Johnson 2014-11-11 15:15:09 -06:00
commit 4feccf8f95
5 changed files with 60 additions and 3 deletions

View File

@ -1,5 +1,6 @@
.gitignore
.travis.yml
bin/geolite-mirror-simple.pl
bin/install_deps.pl
Changes
config.sample/badhelo
@ -161,6 +162,7 @@ t/config/flat_auth_pw
t/config/invalid_resolvable_fromhost
t/config/norelayclients
t/config/plugins
t/config/postgrey_whitelist_clients
t/config/public_suffix_list
t/config/rcpthosts
t/config/relayclients

View File

@ -32,7 +32,8 @@ WriteMakefile(
# 'Mail::SpamAssassin' => 0,
# 'GeoIP2' => 2,
# 'Geo::IP' => 1,
# 'Math::Complex' => 0, # geodesic distance in geoip
# 'Math::Complex' => 0, # geodesic distance in Geo::IP
# 'PerlIO::gzip' => 0, # gunzip GeoIP databases
# 'Mail::SPF' => 0,
},
ABSTRACT => 'Flexible smtpd daemon written in Perl',

View File

@ -0,0 +1,49 @@
#!/usr/bin/perl
use strict;
our $VERSION = '0.02';
use LWP::Simple qw/ mirror RC_NOT_MODIFIED RC_OK $ua /;
use File::Copy qw/ mv /;
use File::Spec;
use PerlIO::gzip;
# --- maxmind.com - please send comments to support@maxmind.com
#
# mirror various maxmind databases from geolite.maxmind.com.
# The script download only changed files, unzip the files and
# move it into the desired directory.
#
# Here is a sample cron entry that check daily for new files.
# 34 15 * * * /usr/local/bin/geolite-mirror-simple.pl
# adjust the path to your needs. Make sure the directories exists
-d ( my $download_dir = '/usr/local/share/GeoIP/download' ) or die $!;
-d ( my $dest_dir = '/usr/local/share/GeoIP' ) or die $!;
# --- remove lines you do not need
# geoip customers should rename or remove GeoIP.dat.gz and GeoIPCity.dat.gz
# This example overwrite your GeoIPCity.dat database!
my %mirror = ( # local-filename geolite-name
'GeoIP.dat.gz' => 'GeoLiteCountry/GeoIP.dat.gz',
'GeoIPCity.dat.gz' => 'GeoLiteCity.dat.gz',
'GeoIPv6.dat.gz' => 'GeoIPv6.dat.gz',
'GeoIPASNum.dat.gz' => 'asnum/GeoIPASNum.dat.gz'
);
$ua->agent("MaxMind-geolite-mirror-simple/$VERSION");
my $dl_path = 'http://geolite.maxmind.com/download/geoip/database/';
chdir $download_dir or die $!;
for my $f ( keys %mirror ) {
my $rc = mirror( $dl_path . $mirror{$f}, $f );
next if $rc == RC_NOT_MODIFIED;
if ( $rc == RC_OK ) {
( my $outfile = $f ) =~ s/\.gz$//;
open my $in, '<:gzip', $f or die $!;
open my $out, '>', $outfile or die $!;
print $out $_ or die $! while <$in>;
mv( $outfile, File::Spec->catfile( $dest_dir, $outfile ) ) or die $!;
}
}
exit 0;

View File

@ -11,13 +11,14 @@ geographic information about incoming connections.
=head1 DESCRIPTION
This plugin saves geographic information in the following connection notes:
Save geographic information about the sender in the following connection notes:
geoip_country - 2 char country code
geoip_country_name - english name of country
geoip_continent - 2 char continent code
geoip_city - english name of city
geoip_distance - distance in kilometers
geoip_asn - network number
And adds entries like this to your logs:
@ -194,6 +195,7 @@ sub geoip2_lookup {
my $self = shift;
my $ip = $self->qp->connection->remote_ip;
return DECLINED if $self->is_localhost($ip);
if ($self->{_geoip2_city}) {
my $city_rec = $self->{_geoip2_city}->city(ip => $ip);
@ -222,6 +224,8 @@ sub geoip2_lookup {
sub geoip_lookup {
my $self = shift;
return DECLINED if $self->is_localhost($self->qp->connection->remote_ip);
# reopen the DB if Geo::IP failed due to DB update
$self->open_geoip_db();
@ -464,4 +468,3 @@ sub get_sender_lat_lon {
}
return $lat, $lon;
}

View File

@ -169,6 +169,8 @@ sub test_set_distance {
sub test_set_asn {
my $self = shift;
return if !$self->{GeoIPASNum};
$self->qp->connection->remote_ip('');
$self->set_asn();
my $asn = $self->set_asn();