From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 2/2] Revert "geoip-functions.pl: Re-write code to lookup the iso country code of a given IP-address." Date: Fri, 11 Jan 2019 10:05:24 +0100 Message-ID: <20190111090524.12871-2-stefan.schantl@ipfire.org> In-Reply-To: <20190111090524.12871-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============6766963553731593206==" List-Id: --===============6766963553731593206== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Enhanching the code to fix the lookup will rapidely slow down the lookup spee= d. Because using the GeoIP2 module is no option ( the reasons have been described in the commi= t message which will now reverted), we have decided to temporary switch back to the old module unt= il a nice solution has been found. This reverts commit ff21ff90d24de0f648d24bb906c45738b81ce67a. Signed-off-by: Stefan Schantl --- config/cfgroot/geoip-functions.pl | 75 +++---------------------------- 1 file changed, 7 insertions(+), 68 deletions(-) diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functio= ns.pl index 9f86a09ba..c8ff47d2e 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -23,82 +23,21 @@ =20 package GeoIP; =20 -require '/var/ipfire/network-functions.pl'; - +use Geo::IP::PurePerl; use Locale::Codes::Country; =20 -# Path where all the GeoIP related databases are stored. -my $geoip_database_dir =3D "/var/lib/GeoIP"; - -# Database which contains all IPv4 networks. -my $address_ipv4_database =3D "GeoLite2-Country-Blocks-IPv4.csv"; - -# Database wich contains the locations data. -my $location_database =3D "GeoLite2-Country-Locations-en.csv"; +my $database; =20 sub lookup($) { my $address =3D shift; - my $location_id; - my $country_code; - - # Check if the given address is valid. - unless(&Network::check_ip_address($address)) { - return; - } - - # Open the address database. - open(ADDRESS, "$geoip_database_dir/$address_ipv4_database") or die "Could n= ot open $geoip_database_dir/$address_ipv4_database. $!\n"; - - # Loop through the file. - while(my $line =3D
) { - # Remove newlines. - chomp($line); - - # Split the line content. - my ($network, $geoname_id, $registered_country_geoname_id, $represented_co= untry_geoname_id, $is_anonymous_proxy, $is_satellite_provider) =3D split(/\,/= , $line); =20 - # Check if the given address is part of the current processed network. - if (&Network::ip_address_in_network($address, $network)) { - # Store the geoname_id for this address. - $location_id =3D $geoname_id; - - # Break loop. - last; - } + # Load the database into memory if not already done + if (!$database) { + $database =3D Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE); } =20 - # Return nothing if no location_id could be found. - return unless($location_id); - - # Close filehandle. - close(ADDRESS); - - # Open the location database. - open(LOCATION, "$geoip_database_dir/$location_database") or die "Could not = open $geoip_database_dir/$location_database. $!\n"; - - # Loop through the file. - while(my $line =3D ) { - # Remove newlines. - chomp($line); - - # Split the line content. - my ($geoname_id, $locale_code, $continent_code, $continent_name, $country_= iso_code, $country_name, $is_in_european_union) =3D split(/\,/, $line); - - # Check if the correct location_id has been found. - if ($geoname_id eq $location_id) { - # Store the county code. - $country_code =3D $country_iso_code; - - # Break loop. - last; - } - } - - # Close filehandle. - close(LOCATION); - - # Return the obtained country code. - return $country_code; + # Return the name of the country + return $database->country_code_by_name($address); } =20 # Function to get the flag icon for a specified country code. --=20 2.19.1 --===============6766963553731593206==--