maxmind / geoipupdate-legacy

GeoIP update client code
GNU General Public License v2.0
258 stars 63 forks source link

No .dat file generated (only .mmdb), but Perl Geo::IP module needing it? #83

Closed FGIKCM closed 6 years ago

FGIKCM commented 6 years ago

Hello,

the geoipupdate script only generate .mmdb files. But using the MaxMind perl module Geo::IP, this format does not seems to be recognized (the API looks for .dat file).

Am I missing something, or is it a bug?

When I perform a geoipupdate into an empty data directory, with a valid subscription: $ geoipupdate -v -f /etc/GeoIP.conf -d /usr/local/share/GeoIP/

geoipupdate 2.5.0
Opened License file /etc/GeoIP.conf
AccountID xxxxxx
LicenseKey xxxxxxxxxxxxxxxxxxxxxxxxx
Insert edition_id GeoIP2-Country
Read in license key /etc/GeoIP.conf
Number of edition IDs 1
url: https://updates.maxmind.com/app/update_getfilename?product_id=GeoIP2-Country
md5hex_digest: 00000000000000000000000000000000
url: https://updates.maxmind.com/app/update_getipaddr
Client IP address: xxx.xxx.xxx.xxx
md5hex_digest2 (challenge): xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
url: https://updates.maxmind.com/app/update_secure?db_md5=00000000000000000000000000000000&challenge_md5=xxxxxxxxxxxxxx&user_id=xxxxx&edition_id=GeoIP2-Country
Uncompress file /usr/local/share/GeoIP//GeoIP2-Country.mmdb.gz to /usr/local/share/GeoIP//GeoIP2-Country.mmdb.test
Rename /usr/local/share/GeoIP//GeoIP2-Country.mmdb.test to /usr/local/share/GeoIP//GeoIP2-Country.mmdb

I get: $ ls -Al /usr/local/share/GeoIP/

total 3432
-rw-r--r-- 1 root staff 3513166 nov.  10 13:15 GeoIP2-Country.mmdb
-rw------- 1 root staff       0 nov.  10 13:15 .geoipupdate.lock

If I want to force using the .mmdb file instead of .dat in Perl: $ cat test.pl

#!/usr/bin/perl -w
use strict;
use Geo::IP;
my $geoIP = Geo::IP->open("/usr/local/share/GeoIP/GeoIP2-Country.mmdb", GEOIP_STANDARD);
my $country = $geoIP->country_code_by_addr("8.8.8.8");

I get: $ perl test.pl

Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Use of uninitialized value $x0 in numeric ge (>=) at (eval 4) line 5176.
Use of uninitialized value $offset in multiplication (*) at (eval 4) line 5155.
Error Traversing Database for ipnum = 134744072 - Perhaps database is corrupt?
FGIKCM commented 6 years ago

Ah, as I understand, I must not use Geo::IP anymore, but GeoIP2::Database::Reader. This is not documented anywhere that the first library is deprecated...

For those having the same problem than me, here is the new method:

#!/usr/bin/perl -w
use strict;
use GeoIP2::Database::Reader;
use Socket;

my $reader = GeoIP2::Database::Reader->new(file => '/usr/local/share/GeoIP/GeoIP2-Country.mmdb');

# All Reader methods are for IPs. If you have domain name, you will have to get an IP:
my $domain = "xxx.com";
my $ip = inet_ntoa(inet_aton($domain)); # Get the first IP

# Then we extract the country
my $country = $reader->country(ip => $ip);
my $iso2 = $country->country()->iso_code();