From e34dbea7474bdd4b34c4551d3704c8b9d6402622 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Thu, 5 Dec 2019 15:50:56 +0100 Subject: [PATCH] geoip-locations.pl: Rework method to grab and handling GeoIP locations. Now directly get the locations which are part of ISO 3166 from the perl Locale::Country module. In case it is not listed there grab the country code and location name from a hash. Signed-off-by: Stefan Schantl --- config/cfgroot/geoip-functions.pl | 48 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index b2319daaaf..4674c9bc8e 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -31,6 +31,20 @@ 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", + "an" => "Netherlands Antilles", + "ap" => "Asia/Pacific Region", + "eu" => "Europe", + "fx" => "France, Metropolitan", + "o1" => "Other Country", + "yu" => "Yugoslavia" +); + sub lookup($) { my $address = shift; @@ -106,13 +120,10 @@ sub get_full_country_name($) { my $code = lc($input); # Handle country codes which are not in the list. - if ($code eq "a1") { $name = "Anonymous Proxy" } - elsif ($code eq "a2") { $name = "Satellite Provider" } - elsif ($code eq "o1") { $name = "Other Country" } - elsif ($code eq "ap") { $name = "Asia/Pacific Region" } - elsif ($code eq "eu") { $name = "Europe" } - elsif ($code eq "yu") { $name = "Yugoslavia" } - else { + if ($not_iso_3166_location{$code}) { + # Grab location name from hash. + $name = $not_iso_3166_location{$code}; + } else { # Use perl built-in module to get the country code. $name = &Locale::Codes::Country::code2country($code); } @@ -124,27 +135,14 @@ sub get_full_country_name($) { sub get_geoip_locations() { my @locations = (); - # Open the location database. - open(LOCATION, "$geoip_database_dir/$location_database") or return @locations; - - # Loop through the file. - while(my $line = ) { - # Remove newlines. - chomp($line); + # Get listed country codes from ISO 3166-1. + @locations = &Locale::Codes::Country::all_country_codes(); - # Split the line content. - my ($geoname_id, $locale_code, $continent_code, $continent_name, $country_iso_code, $country_name, $is_in_european_union) = split(/\,/, $line); - - # Check if the country_iso_code is upper case. - if($country_iso_code =~ /[A-Z]/) { - # Add the current ISO code. - push(@locations, $country_iso_code); - } + # Add locations from not_iso_3166_locations. + foreach my $location (keys %not_iso_3166_location) { + push(@locations, $location); } - # Close filehandle. - close(LOCATION); - # Sort locations array in alphabetical order. my @sorted_locations = sort(@locations); -- 2.39.5