From patchwork Fri Nov 10 09:32:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 1526 Return-Path: Received: from mail01.ipfire.org (unknown [172.28.1.200]) by web02.ipfire.org (Postfix) with ESMTP id 7341360DB8 for ; Thu, 9 Nov 2017 23:32:19 +0100 (CET) Received: from mail01.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id A381C37DC; Thu, 9 Nov 2017 23:32:18 +0100 (CET) Received: from ipfire.tremer.co.uk (unknown [88.98.205.56]) (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 E988B36A8; Thu, 9 Nov 2017 23:32:16 +0100 (CET) From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 1/2] geoip-functions.pl: Fix typos and formatting Date: Thu, 9 Nov 2017 22:32:03 +0000 Message-Id: <20171109223204.14213-1-michael.tremer@ipfire.org> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20171108225248.204ac963.peter.mueller@link38.eu> References: <20171108225248.204ac963.peter.mueller@link38.eu> X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Signed-off-by: Michael Tremer Signed-off-by: Michael Tremer Reviewed-by: Peter Müller --- config/cfgroot/geoip-functions.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index fc2dfdd34..623169eaf 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -63,10 +63,10 @@ sub get_flag_icon($) { # the icon for "unknown". my $ccode = "unknown"; - # Redoing all the stuff from abouve for the "unknown" icon. - my $file = join('.', $ccode,$ext); - my $flag_icon = join('/', $flagdir,$file); - my $absolute_path = join('', $webroot,$flag_icon); + # Redoing all the stuff from above for the "unknown" icon. + my $file = join('.', $ccode, $ext); + my $flag_icon = join('/', $flagdir, $file); + my $absolute_path = join('', $webroot, $flag_icon); # Check if the icon is present. if (-e "$absolute_path") { From patchwork Fri Nov 10 09:32:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 1527 Return-Path: Received: from mail01.ipfire.org (unknown [172.28.1.200]) by web02.ipfire.org (Postfix) with ESMTP id A07AC60DB8 for ; Thu, 9 Nov 2017 23:32:23 +0100 (CET) Received: from mail01.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 0D6FD3804; Thu, 9 Nov 2017 23:32:22 +0100 (CET) Received: from ipfire.tremer.co.uk (unknown [88.98.205.56]) (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 2C04D36BB; Thu, 9 Nov 2017 23:32:17 +0100 (CET) From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 2/2] GeoIP: Add lookup function for convenience Date: Thu, 9 Nov 2017 22:32:04 +0000 Message-Id: <20171109223204.14213-2-michael.tremer@ipfire.org> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20171109223204.14213-1-michael.tremer@ipfire.org> References: <20171108225248.204ac963.peter.mueller@link38.eu> <20171109223204.14213-1-michael.tremer@ipfire.org> X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Instead of opening the database again for each lookup, we will read it into memory on first use and every lookup after that will be coming from cache. Signed-off-by: Michael Tremer Signed-off-by: Michael Tremer Reviewed-by: Peter Müller --- config/cfgroot/geoip-functions.pl | 15 +++++++++++++++ html/cgi-bin/country.cgi | 10 +++++----- html/cgi-bin/logs.cgi/firewalllog.dat | 9 +++------ html/cgi-bin/logs.cgi/firewalllogcountry.dat | 4 +--- html/cgi-bin/logs.cgi/firewalllogip.dat | 7 ++----- html/cgi-bin/logs.cgi/showrequestfromcountry.dat | 5 ++--- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/config/cfgroot/geoip-functions.pl b/config/cfgroot/geoip-functions.pl index 623169eaf..be50d5e14 100644 --- a/config/cfgroot/geoip-functions.pl +++ b/config/cfgroot/geoip-functions.pl @@ -23,8 +23,23 @@ package GeoIP; +use Geo::IP::PurePerl; use Locale::Codes::Country; +my $database; + +sub lookup($) { + my $address = shift; + + # Load the database into memory if not already done + if (!$database) { + $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE); + } + + # Return the name of the country + return $database->country_code_by_name($address); +} + # Function to get the flag icon for a specified country code. sub get_flag_icon($) { my ($input) = @_; diff --git a/html/cgi-bin/country.cgi b/html/cgi-bin/country.cgi index f2ae81300..8df2427a9 100644 --- a/html/cgi-bin/country.cgi +++ b/html/cgi-bin/country.cgi @@ -60,7 +60,7 @@ foreach my $country (@countries) { $lines++; # Convert country code into upper case. - my $country_uc = uc($country); + $country = uc($country); # Get flag icon for of the country. my $flag_icon = &GeoIP::get_flag_icon($country); @@ -69,8 +69,8 @@ foreach my $country (@countries) { my $name = &GeoIP::get_full_country_name($country); if ($lines % 2) { - print "$country_uc"; - print "$country_uc"; + print "$country"; + print "$country"; print "$name\n"; } else { $lines2++; @@ -80,8 +80,8 @@ foreach my $country (@countries) { $col="style='background-color:${Header::table1colour};'"; } print ""; - print "$country_uc"; - print "$country_uc"; + print "$country"; + print "$country"; print "$name"; print " "; diff --git a/html/cgi-bin/logs.cgi/firewalllog.dat b/html/cgi-bin/logs.cgi/firewalllog.dat index 5c9722b85..e67a40a9f 100644 --- a/html/cgi-bin/logs.cgi/firewalllog.dat +++ b/html/cgi-bin/logs.cgi/firewalllog.dat @@ -13,7 +13,6 @@ # use strict; -use Geo::IP::PurePerl; use Getopt::Std; # enable only the following on debugging purpose @@ -352,9 +351,7 @@ foreach $_ (@log) $srcport=$1 if $packet =~ /SPT=(\d+)/; $dstport=$1 if $packet =~ /DPT=(\d+)/; - my $gi = Geo::IP::PurePerl->new(); - my $ccode = $gi->country_code_by_name($srcaddr); - my $fcode = lc($ccode); + my $ccode = &GeoIP::lookup($srcaddr); my $servi = uc(getservbyport($srcport, lc($proto))); if ($servi ne '' && $srcport < 1024) { @@ -386,10 +383,10 @@ foreach $_ (@log) END ; # Get flag icon for of the country. - my $flag_icon = &GeoIP::get_flag_icon($fcode); + my $flag_icon = &GeoIP::get_flag_icon($ccode); if ( $flag_icon) { - print "$ccode"; + print "$ccode"; } else { print ""; } diff --git a/html/cgi-bin/logs.cgi/firewalllogcountry.dat b/html/cgi-bin/logs.cgi/firewalllogcountry.dat index f2b6048f7..949f2599d 100644 --- a/html/cgi-bin/logs.cgi/firewalllogcountry.dat +++ b/html/cgi-bin/logs.cgi/firewalllogcountry.dat @@ -11,7 +11,6 @@ # and Michael Tremer (www.ipfire.org) use strict; -use Geo::IP::PurePerl; use Getopt::Std; # enable only the following on debugging purpose @@ -287,7 +286,6 @@ print "

$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines

"; my $red_interface = &General::get_red_interface(); my $linesjc = 0; my %tabjc; -my $gi = Geo::IP::PurePerl->new(); if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; }; $lines = 0; @@ -310,7 +308,7 @@ foreach $_ (@log) # Traffic from red if($srcaddr ne '') { # srcaddr is set - my $ccode = $gi->country_code_by_name($srcaddr); + my $ccode = &GeoIP::lookup($srcaddr); if ($ccode eq '') { $ccode = 'unknown'; } diff --git a/html/cgi-bin/logs.cgi/firewalllogip.dat b/html/cgi-bin/logs.cgi/firewalllogip.dat index 9e366745d..c73d24fd6 100644 --- a/html/cgi-bin/logs.cgi/firewalllogip.dat +++ b/html/cgi-bin/logs.cgi/firewalllogip.dat @@ -11,7 +11,6 @@ # and Michael Tremer (www.ipfire.org) use strict; -use Geo::IP::PurePerl; use Getopt::Std; # enable only the following on debugging purpose @@ -436,9 +435,7 @@ for($s=0;$s<$lines;$s++) $col="bgcolor='$color{\"color$colorIndex\"}'"; print ""; - my $gi = Geo::IP::PurePerl->new(); - my $ccode = $gi->country_code_by_name($key[$s]); - my $fcode = lc($ccode); + my $ccode = &GeoIP::lookup($key[$s]); $color++; print "
"; @@ -448,7 +445,7 @@ for($s=0;$s<$lines;$s++) my $flag_icon = &GeoIP::get_flag_icon($ccode); if ( $flag_icon ) { - print "$ccode"; + print "$ccode"; } else { print ""; } diff --git a/html/cgi-bin/logs.cgi/showrequestfromcountry.dat b/html/cgi-bin/logs.cgi/showrequestfromcountry.dat index b6383ed59..605873ac0 100644 --- a/html/cgi-bin/logs.cgi/showrequestfromcountry.dat +++ b/html/cgi-bin/logs.cgi/showrequestfromcountry.dat @@ -13,9 +13,9 @@ #use CGI::Carp 'fatalsToBrowser'; #use strict; -use Geo::IP::PurePerl; require '/var/ipfire/general-functions.pl'; +require "${General::swroot}/geoip-functions.pl"; require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; @@ -152,7 +152,6 @@ if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) { my $lines = 0; my @log=(); my $country = $cgiparams{country}; -my $gi = Geo::IP::PurePerl->new(); if (!$skip) { @@ -179,7 +178,7 @@ if (!$skip) } elsif($srcaddr ne '') { # or srcaddr matches country code - my $ccode = $gi->country_code_by_name($srcaddr); + my $ccode = &GeoIP::lookup($srcaddr); if($ccode eq uc($country)){ $log[$lines] = $_; $lines++;