]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/commitdiff
geoip-functions.pl: Use libloc instead of maxmind for address lookups.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 5 Dec 2019 14:53:42 +0000 (15:53 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Fri, 6 Dec 2019 13:20:31 +0000 (14:20 +0100)
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/geoip-functions.pl

index 4674c9bc8ec189868aefee114bff78fbe5f51398..c8b3c5e7046b5f1dcab48d27ffd5a92d6c1679d7 100644 (file)
 
 package GeoIP;
 
-use Geo::IP::PurePerl;
+use Location;
 use Locale::Codes::Country;
 
-my $geoip_database_dir = "/var/lib/GeoIP";
-my $location_database = "GeoLite2-Country-Locations-en.csv";
-
-my $database;
-
 # Hash which contains country codes and their names which are special or not
 # part of ISO 3166-1.
 my %not_iso_3166_location = (
        "a1" => "Anonymous Proxy",
        "a2" => "Satellite Provider",
-       "a3" => "Worldwide Anycast Anstance",
+       "a3" => "Worldwide Anycast Instance",
        "an" => "Netherlands Antilles",
        "ap" => "Asia/Pacific Region",
        "eu" => "Europe",
@@ -45,16 +40,53 @@ my %not_iso_3166_location = (
        "yu" => "Yugoslavia"
 );
 
-sub lookup($) {
-       my $address = shift;
+# Directory where the libloc database and keyfile lives.
+my $location_dir = "/usr/share/location/";
+
+# Libloc database file.
+my $database = "$location_dir/database.db";
+
+# Libloc keyfile to verify the database.
+my $keyfile = "$location_dir/signing-key.pem";
+
+#
+## Tiny function to init the location database.
+#
+sub init () {
+       # Init and open the database.
+       my $db = &Location::init($database);
+
+       # Return the database handle.
+       return $db;
+}
 
-       # Load the database into memory if not already done
-       if (!$database) {
-               $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE);
+#
+## Function to verify the integrity of the location database.
+#
+sub verify ($) {
+       my ($db_handle) = @_;
+
+       # Verify the integrity of the database.
+       if(&Location::verify($db_handle, $keyfile)) {
+               # Success, return "1".
+               return 1;
        }
 
+       # If we got here, return nothing.
+       return;
+}
+
+#
+## Function to the the country code of a given address.
+#
+sub lookup_country_code($$) {
+       my ($db_handle, $address) = @_;
+
+       # Lookup the given address.
+       my $country_code = &Location::lookup_country_code($db_handle, $address);
+
        # Return the name of the country
-       return $database->country_code_by_name($address);
+       return $country_code;
 }
 
 # Function to get the flag icon for a specified country code.
@@ -116,6 +148,7 @@ sub get_full_country_name($) {
        # Remove whitespaces.
        chomp($input);
 
+
        # Convert input into lower case format.
        my $code = lc($input);