show GeoIP information in WebUI connection list

Message ID 20170908211141.35e974b8.peter.mueller@link38.eu
State New
Headers show
Series
  • show GeoIP information in WebUI connection list
Related show

Commit Message

Peter Müller Sept. 8, 2017, 7:11 p.m.
Show country flag for source and destination IP addresses at the connection
list ("connections.cgi"). This might be useful for debugging geoip based
firewall rules and to identify possible harmful connections (i.e. to offshore
locations or other countries rarely used).

This is another "nice to have" patch (trying to gain experience by adding
some simple features I always missed) but could come in handy sometimes.

Signed-off-by: Peter Müller <peter.mueller@link38.eu>
---

Patch

diff --git a/html/cgi-bin/connections.cgi b/html/cgi-bin/connections.cgi
index 96f09012b..b3348f04a 100644
--- a/html/cgi-bin/connections.cgi
+++ b/html/cgi-bin/connections.cgi
@@ -23,12 +23,14 @@  use strict;
 
 use Net::IPv4Addr qw( :all );
 use Switch;
+use Geo::IP::PurePerl;
 
 # enable only the following on debugging purpose
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
 
 require '/var/ipfire/general-functions.pl';
+require "${General::swroot}/geoip-functions.pl";
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
@@ -379,6 +382,7 @@  print <<END;
 				<a href="?sort_field=3&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
 				<a href="?sort_field=3&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
 			</th>
+			<th>&nbsp;</th>
 			<th style='text-align:center' colspan='2'>
 				<a href="?sort_field=2&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
 				<a href="?sort_field=2&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
@@ -386,6 +390,7 @@  print <<END;
 				<a href="?sort_field=4&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
 				<a href="?sort_field=4&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
 			</th>
+			<th>&nbsp;</th>
 			<th style='text-align:center'>
 				<a href="?sort_field=8&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
 				<a href="?sort_field=8&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
@@ -409,10 +414,16 @@  print <<END;
 			<th style='text-align:center' colspan='2'>
 				$Lang::tr{'source ip and port'}
 			</th>
+			<th style='text-align:center'>
+				$Lang::tr{'country'}
+			</th>	
 			<th style='text-align:center' colspan='2'>
 				$Lang::tr{'dest ip and port'}
 			</th>
 			<th style='text-align:center'>
+                                $Lang::tr{'country'}
+                        </th>
+			<th style='text-align:center'>
 				$Lang::tr{'download'} /
 				<br>$Lang::tr{'upload'}
 			</th>
@@ -551,6 +562,12 @@  foreach my $line (@conntrack) {
 		$sip_extra .= "</a>";
 	}
 
+        my $gi1 = Geo::IP::PurePerl->new();
+        my $ccode1 = $gi1->country_code_by_name($sip_ret);
+        my $fcode1 = lc($ccode1);
+        my $flag_icon1 = &GeoIP::get_flag_icon($fcode1);
+
+
 	my $dip_extra;
 	if ($dip_ret && $dip ne $dip_ret) {
 		$dip_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
@@ -559,6 +576,10 @@  foreach my $line (@conntrack) {
 		$dip_extra .= "</a>";
 	}
 
+        my $gi2 = Geo::IP::PurePerl->new();
+        my $ccode2 = $gi2->country_code_by_name($dip_ret);
+        my $fcode2 = lc($ccode2);
+        my $flag_icon2 = &GeoIP::get_flag_icon($fcode2);
 
 	my $sport_extra;
 	if ($sport ne $sport_ret) {
@@ -601,6 +622,9 @@  foreach my $line (@conntrack) {
 			</a>
 			$sport_extra
 		</td>
+		<td style='text-align:center; background-color:$sip_colour;'>
+                        <a href='country.cgi#$fcode1'><img src='$flag_icon1' border='0' align='absmiddle' title='$ccode1'></a>
+                </td>
 		<td style='text-align:center; background-color:$dip_colour;'>
 			<a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
 				<span style='color:#FFFFFF;'>$dip</span>
@@ -613,6 +637,9 @@  foreach my $line (@conntrack) {
 			</a>
 			$dport_extra
 		</td>
+		<td style='text-align:center; background-color:$dip_colour;'>
+                        <a href='country.cgi#$fcode2'><img src='$flag_icon2' border='0' align='absmiddle' title='$ccode2'></a>
+                </td>
 		<td style='text-align:center'>
 			$bytes_in / $bytes_out
 		</td>
@@ -683,3 +710,4 @@  sub ipcolour($) {
 }
 
 1;
+