]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/commitdiff
geoip-locations.pl: Rework method to grab and handling GeoIP locations.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 5 Dec 2019 14:50:56 +0000 (15:50 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Fri, 6 Dec 2019 12:58:20 +0000 (13:58 +0100)
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 <stefan.schantl@ipfire.org>
config/cfgroot/geoip-functions.pl

index b2319daaaffe7168d04cfafeabb2113af92cd08a..4674c9bc8ec189868aefee114bff78fbe5f51398 100644 (file)
@@ -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 = <LOCATION>) {
-               # 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);