From patchwork Fri Jan 11 20:05:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 2037 Return-Path: Received: from mail01.ipfire.org (unknown [172.28.1.200]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mail01.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web07.i.ipfire.org (Postfix) with ESMTPS id B571D87AF91 for ; Fri, 11 Jan 2019 09:05:35 +0000 (GMT) Received: from mail01.i.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 53F8E20FA311; Fri, 11 Jan 2019 09:05:35 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=201801; t=1547197535; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id: list-unsubscribe:list-subscribe:list-post; bh=rm7SAWfJjPnHReeE2kzV7K3nnk/IKzyblS+8MgZYUt0=; b=E4S5Sdo79/02ttbhepN6XnpeCv668yx4vi/0OPqahdR4jpzKvUtmQDP1nWrbWNTOhoATL/ oaTjAZlzRGtJfUyoBSND/Nkm+yHrnEWZ2zJkNyF8HazLPHSGjW67jLwBKyj1/7OksqoMz9 isUQfUnsPGkS5D1nnUfEFQSTUbERGZ2gjrhdCCJhTeDGJ10+qF3o9+/AqOOIIjs202CWqw LEi70VL24nBADyfa1vxDmFvHs4SJzOpGMh3VE0sF1l56WVSdCbU6hLt3O9FLlHHuKL64u0 76HBOnJ0RyIT61xtntb7uXLTHFi9Ss/0nF12dEiDIn/NOoctBiaT9UxpujCW5g== Received: from tuxedo.stevee (212095005246.public.telering.at [212.95.5.246]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id D95BF20FA598; Fri, 11 Jan 2019 09:05:29 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=201801; t=1547197530; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rm7SAWfJjPnHReeE2kzV7K3nnk/IKzyblS+8MgZYUt0=; b=cEzpH0JCx4U+HgrCtDs+2izYO4SJjz898tijHToGDRk2tHrwWx212ZTvYcIpH3d+HwTpgA FvuIH/8q01KAjT1W66vj65+ECbga8Dno398ybcuXvmarCFxuu2IYI+dXV6PXxv1MM8JiWu 0cocsaRzGBdqm27jqFAZxuhO+ApSveN5seTT0q/yawPimpPA1WIYWkliwYs9mADJc+7wgJ jfHGBbTAMaVVUrAdkRQNRByVCNo7RUFwWUZzT79EvZzAjVXxdaAuI7niXMpGEMLaxSXsuy 2VsdhX+6r3dEhVF5OpJIrm94tNwsQeJwuehnnQfZL52ca6E3vSJy6/wVqgVIcg== 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> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190111090524.12871-1-stefan.schantl@ipfire.org> References: <20190111090524.12871-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.auth=stevee smtp.mailfrom=stefan.schantl@ipfire.org X-Spamd-Result: default: False [-4.54 / 11.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; REPLY(-4.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; DKIM_SIGNED(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM(-2.85)[-0.950,0]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:8412, ipnet:212.95.0.0/19, country:AT]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-2.60)[98.21%] X-Spam-Status: No, score=-4.54 X-Rspamd-Server: mail01.i.ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Enhanching the code to fix the lookup will rapidely slow down the lookup speed. Because using the GeoIP2 module is no option ( the reasons have been described in the commit message which will now reverted), we have decided to temporary switch back to the old module until 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-functions.pl index 9f86a09ba..c8ff47d2e 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -23,82 +23,21 @@ package GeoIP; -require '/var/ipfire/network-functions.pl'; - +use Geo::IP::PurePerl; use Locale::Codes::Country; -# Path where all the GeoIP related databases are stored. -my $geoip_database_dir = "/var/lib/GeoIP"; - -# Database which contains all IPv4 networks. -my $address_ipv4_database = "GeoLite2-Country-Blocks-IPv4.csv"; - -# Database wich contains the locations data. -my $location_database = "GeoLite2-Country-Locations-en.csv"; +my $database; sub lookup($) { my $address = 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 not open $geoip_database_dir/$address_ipv4_database. $!\n"; - - # Loop through the file. - while(my $line =
) { - # Remove newlines. - chomp($line); - - # Split the line content. - my ($network, $geoname_id, $registered_country_geoname_id, $represented_country_geoname_id, $is_anonymous_proxy, $is_satellite_provider) = split(/\,/, $line); - # 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 = $geoname_id; - - # Break loop. - last; - } + # Load the database into memory if not already done + if (!$database) { + $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE); } - # 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 = ) { - # 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) = split(/\,/, $line); - - # Check if the correct location_id has been found. - if ($geoname_id eq $location_id) { - # Store the county code. - $country_code = $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); } # Function to get the flag icon for a specified country code.