]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
GeoIP: Add lookup function for convenience
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 9 Nov 2017 22:32:04 +0000 (22:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 13 Nov 2017 22:37:19 +0000 (22:37 +0000)
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.

Reviewed-by: Peter Müller <peter.mueller@link38.eu>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
config/cfgroot/geoip-functions.pl
html/cgi-bin/country.cgi
html/cgi-bin/logs.cgi/firewalllog.dat
html/cgi-bin/logs.cgi/firewalllogcountry.dat
html/cgi-bin/logs.cgi/firewalllogip.dat
html/cgi-bin/logs.cgi/showrequestfromcountry.dat

index 623169eaf8d5f473b0831a33ed639fe64a2ea082..be50d5e142f448c4b59c9fb0df47eac5e04a2ccf 100644 (file)
 
 package GeoIP;
 
 
 package GeoIP;
 
+use Geo::IP::PurePerl;
 use Locale::Codes::Country;
 
 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) = @_;
 # Function to get the flag icon for a specified country code.
 sub get_flag_icon($) {
        my ($input) = @_;
index f2ae81300905ee5fafb995224471cdbe5a09aa54..8df2427a9eb10d24fd43bd8c5ce1b36cceecb416 100644 (file)
@@ -60,7 +60,7 @@ foreach my $country (@countries) {
        $lines++;
 
        # Convert country code into upper case.
        $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);
 
        # 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) {
        my $name = &GeoIP::get_full_country_name($country);
 
        if ($lines % 2) {
-               print "<td $col><a id='$country'><img src='$flag_icon' alt='$country_uc' title='$country_uc'/></a></td>";
-               print "<td $col>$country_uc</td>";
+               print "<td $col><a id='$country'><img src='$flag_icon' alt='$country' title='$country'/></a></td>";
+               print "<td $col>$country</td>";
                print "<td $col>$name</td></tr>\n";
        } else {
                $lines2++;
                print "<td $col>$name</td></tr>\n";
        } else {
                $lines2++;
@@ -80,8 +80,8 @@ foreach my $country (@countries) {
                        $col="style='background-color:${Header::table1colour};'";
                }
                print "<tr>";
                        $col="style='background-color:${Header::table1colour};'";
                }
                print "<tr>";
-               print "<td $col><a id='$country'><img src='$flag_icon' alt='$country_uc' title='$country_uc'/></a></td>";
-               print "<td $col>$country_uc</td>";
+               print "<td $col><a id='$country'><img src='$flag_icon' alt='$country' title='$country'/></a></td>";
+               print "<td $col>$country</td>";
                print "<td $col>$name</td>";
                print "<td $col>&nbsp;</td>";
 
                print "<td $col>$name</td>";
                print "<td $col>&nbsp;</td>";
 
index 5c9722b8521f4df04e0c317968df849811492fe7..e67a40a9f506d4712c3ac1255c38b4e54f5938aa 100644 (file)
@@ -13,7 +13,6 @@
 #
 use strict;
 
 #
 use strict;
 
-use Geo::IP::PurePerl;
 use Getopt::Std;
 
 # enable only the following on debugging purpose
 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+)/;
 
        $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) {
 
        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.
 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) {
 
        if ( $flag_icon) {
-               print "<td align='center' $col><a href='../country.cgi#$fcode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
+               print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
        } else {
                print "<td align='center' $col></td>";
        }
        } else {
                print "<td align='center' $col></td>";
        }
index f2b6048f7930ff7d7aca7262ed270e300be83d70..949f2599daade08aa40d9285a3c754bccf419c95 100644 (file)
@@ -11,7 +11,6 @@
 #                          and Michael Tremer (www.ipfire.org)
 
 use strict;
 #                          and Michael Tremer (www.ipfire.org)
 
 use strict;
-use Geo::IP::PurePerl;
 use Getopt::Std;
 
 # enable only the following on debugging purpose
 use Getopt::Std;
 
 # enable only the following on debugging purpose
@@ -287,7 +286,6 @@ print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
 my $red_interface = &General::get_red_interface();
 my $linesjc = 0;
 my %tabjc;
 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;
 
 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
                # 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';
                        }
                        if ($ccode eq '') {
                                $ccode = 'unknown';
                        }
index 9e366745de1a2e366fdcb7000224b4d3fe8bfe19..c73d24fd663fee6af86ff0cc31e669f7acf217b0 100644 (file)
@@ -11,7 +11,6 @@
 #                          and Michael Tremer (www.ipfire.org)
 
 use strict;
 #                          and Michael Tremer (www.ipfire.org)
 
 use strict;
-use Geo::IP::PurePerl;
 use Getopt::Std;
 
 # enable only the following on debugging purpose
 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 "<tr>";
 
        $col="bgcolor='$color{\"color$colorIndex\"}'";
        print "<tr>";
 
-       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 "<td align='center' $col><form method='post' action='showrequestfromip.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='ip' value='$key[$s]'> <input type='submit' value='$Lang::tr{'details'}'></form></td>";
   
        $color++;
        print "<td align='center' $col><form method='post' action='showrequestfromip.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='ip' value='$key[$s]'> <input type='submit' value='$Lang::tr{'details'}'></form></td>";
@@ -448,7 +445,7 @@ for($s=0;$s<$lines;$s++)
        my $flag_icon = &GeoIP::get_flag_icon($ccode);
 
        if ( $flag_icon ) {
        my $flag_icon = &GeoIP::get_flag_icon($ccode);
 
        if ( $flag_icon ) {
-               print "<td align='center' $col><a href='/cgi-bin/country.cgi#$fcode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode'></a></td>";
+               print "<td align='center' $col><a href='/cgi-bin/country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode'></a></td>";
        } else {
                print "<td align='center' $col></td>";
        }
        } else {
                print "<td align='center' $col></td>";
        }
index b6383ed59bf6806bbff57f2bc8e8cd247505e918..605873ac00ce7ca965750582b0264d118aa3cde9 100644 (file)
@@ -13,9 +13,9 @@
 #use CGI::Carp 'fatalsToBrowser';
 
 #use strict;
 #use CGI::Carp 'fatalsToBrowser';
 
 #use strict;
-use Geo::IP::PurePerl;
 
 require '/var/ipfire/general-functions.pl';
 
 require '/var/ipfire/general-functions.pl';
+require "${General::swroot}/geoip-functions.pl";
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.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 $lines = 0;
 my @log=();
 my $country = $cgiparams{country};
-my $gi = Geo::IP::PurePerl->new();
 
 if (!$skip)
 {
 
 if (!$skip)
 {
@@ -179,7 +178,7 @@ if (!$skip)
                        }
                        elsif($srcaddr ne '') {
                                # or srcaddr matches country code
                        }
                        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++;
                                if($ccode eq uc($country)){
                                        $log[$lines] = $_;
                                        $lines++;