From 8a64d10f24b2161592814e9f608a1ea674784312 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Thu, 5 Dec 2019 15:53:42 +0100 Subject: [PATCH] geoip-functions.pl: Use libloc instead of maxmind for address lookups. Signed-off-by: Stefan Schantl --- config/cfgroot/geoip-functions.pl | 59 ++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index 4674c9bc8e..c8b3c5e704 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -23,20 +23,15 @@ 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); -- 2.39.5