[v2,2/3] ipinfo.cgi: display AS information as well

Message ID 376d040b-f1f9-d504-3a60-65472b2ee3af@ipfire.org
State Accepted
Commit 4dc8de6f2230a77634d2a43cfc0231d9369dfeea
Headers
Series [v2,1/3] location-functions.pl: add functions for fetching AS information |

Commit Message

Peter Müller Nov. 11, 2020, 2:15 p.m. UTC
  Signed-off-by: Peter Müller <peter.mueller@ipfire.org>
---
 html/cgi-bin/ipinfo.cgi | 142 +++++++++++++++++++++++++---------------
 1 file changed, 90 insertions(+), 52 deletions(-)
  

Patch

diff --git a/html/cgi-bin/ipinfo.cgi b/html/cgi-bin/ipinfo.cgi
index ecc7c436c..1b7ceeadc 100644
--- a/html/cgi-bin/ipinfo.cgi
+++ b/html/cgi-bin/ipinfo.cgi
@@ -2,7 +2,7 @@ 
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2010  IPFire Team  <info@ipfire.org>                          #
+# Copyright (C) 2007-2020  IPFire Team  <info@ipfire.org>                     #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
@@ -32,12 +32,16 @@  require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 require "${General::swroot}/location-functions.pl";
 
+# Load colours for current theme...
+my %color = ();
+my %mainsettings = ();
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+
 my %cgiparams=();
 
 &Header::showhttpheaders();
 
-&Header::openpage($Lang::tr{'ip info'}, 1, '');
-&Header::openbigbox('100%', 'left');
 my @lines=();
 my $extraquery='';
 
@@ -57,17 +61,37 @@  my $whois_server = "whois.arin.net";
 my $addr = CGI::param("ip") || "";
 
 if (&General::validip($addr)) {
+	# Write HTML page header...
+	&Header::openpage($Lang::tr{'ip info for'} . ' ' . $addr, 1, '');
+	&Header::openbigbox('100%', 'left');
+
 	my $iaddr = inet_aton($addr);
 	my $hostname = gethostbyaddr($iaddr, AF_INET);
-	if (!$hostname) { $hostname = $Lang::tr{'lookup failed'}; }
+	if (!$hostname) { $hostname = $Lang::tr{'ptr lookup failed'}; }
 
-	# enumerate location information for IP address...
+	# Enumerate location information for IP address...
 	my $ccode = &Location::Functions::lookup_country_code($addr);
+	my $cname = &Location::Functions::get_full_country_name($ccode);
 	my @network_flags = &Location::Functions::address_has_flags($addr);
 
 	# Try to get the continent of the country code.
 	my $continent = &Location::Functions::get_continent_code($ccode);
 
+	# Enumerate Autonomous System details for IP address...
+	my $asn = &Location::Functions::lookup_asn($addr);
+	my $as_name;
+	if ($asn) {
+		$as_name = &Location::Functions::get_as_name($asn);
+
+		# In case we have found an AS name, make output more readable...
+		if ($as_name) {
+			$as_name = "- " . $as_name;
+		}
+		$asn = "AS" . $asn;
+	} else {
+		$asn = $Lang::tr{'asn lookup failed'};
+	}
+
 	# Check if a whois server for the continent is known.
 	if($whois_servers_by_continent{$continent}) {
 		# Use it.
@@ -76,6 +100,62 @@  if (&General::validip($addr)) {
 
 	my $flag_icon = &Location::Functions::get_flag_icon($ccode);
 
+	&Header::openbox('100%', 'left', $Lang::tr{'ip basic info'});
+
+	print <<END;
+		<center>
+			<table class="tbl" width='100%'>
+				<tr>
+					<td bgcolor='$color{'color22'}'><strong>$Lang::tr{'country'}</strong></td>
+					<td bgcolor='$color{'color22'}'>$cname <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$cname" title="$cname" /></td>
+				</tr>
+				<tr>
+					<td bgcolor='$color{'color20'}'><strong>$Lang::tr{'ptr'}</strong></td>
+					<td bgcolor='$color{'color20'}'>$hostname</td>
+				</tr>
+				<tr>
+					<td bgcolor='$color{'color22'}'><strong>$Lang::tr{'autonomous system'}</strong></td>
+					<td bgcolor='$color{'color22'}'>$asn $as_name</td>
+				</tr>
+END
+
+	# Check if the address has a flag.
+	if (@network_flags) {
+		# Get amount of flags for this network.
+		my $flags_amount = @network_flags;
+		my $processed_flags;
+
+		# Loop through the array of network_flags.
+		foreach my $network_flag (@network_flags) {
+			# Increment value of processed flags.
+			$processed_flags++;
+
+			# Get the network flag name.
+			my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
+
+			# Colorize columns.
+			my $col;
+			if ($processed_flags % 2) {
+				$col = "bgcolor='$color{'color20'}'"; }
+			else {
+				$col = "bgcolor='$color{'color22'}'";
+			}
+
+			# Write table row...
+			print <<END;
+				<tr>
+					<td $col><strong>$network_flag_name</strong></td>
+					<td $col>$Lang::tr{'yes'}</td>
+				</tr>
+END
+		}
+	}
+
+	print "			</table>\n";
+	print "		</center>\n";
+
+	&Header::closebox();
+
 	my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
 	if ($sock)
 	{
@@ -107,53 +187,7 @@  if (&General::validip($addr)) {
 		@lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
 	}
 
-	&Header::openbox('100%', 'left', $addr . " <a href='country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode' /></a> (" . $hostname . ') : '.$whois_server);
-
-	# Check if the address has a flag.
-	if (@network_flags) {
-		# Get amount of flags for this network.
-		my $flags_amount = @network_flags;
-		my $processed_flags;
-
-		# The message string which will be displayed.
-		my $message_string = "This address is marked as";
-
-		# Loop through the array of network_flags.
-		foreach my $network_flag (@network_flags) {
-			# Increment value of processed flags.
-			$processed_flags++;
-
-			# Get the network flag name.
-			my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
-
-			# Add the flag name to the message string.
-			$message_string = "$message_string" . " $network_flag_name";
-
-			# Check if multiple flags are set for this network.
-			if ($flags_amount gt "1") {
-				# Check if the the current flag is the next-to-last one.
-				if ($processed_flags eq $flags_amount - 1) {
-					$message_string = "$message_string" . " and ";
-
-				# Check if the current flag it the last one.
-				} elsif ($processed_flags eq $flags_amount) {
-					# The message is finished add a dot for ending the sentence.
-					$message_string = "$message_string" . ".";
-
-				# Otherwise add a simple comma to the message string.
-				} else {
-					$message_string = "$message_string" . ", ";
-				}
-			} else {
-				# Nothing special to do, simple add a dot to finish the sentence.
-				$message_string = "$message_string" . ".";
-			}
-		}
-
-		# Display the generated notice.
-		print "<h3>$message_string</h3>\n";
-		print "<br>\n";
-	}
+	&Header::openbox('100%', 'left', $Lang::tr{'whois results from'} . " " . $whois_server);
 
 	print "<pre>\n";
 	foreach my $line (@lines) {
@@ -162,6 +196,10 @@  if (&General::validip($addr)) {
 	print "</pre>\n";
 	&Header::closebox();
 } else {
+	# Open HTML page header in case of invalid IP addresses
+	&Header::openpage($Lang::tr{'ip info'}, 1, '');
+	&Header::openbigbox('100%', 'left');
+
 	&Header::openbox('100%', 'left', $Lang::tr{'invalid ip'});
 	print <<EOF;
 		<p style="text-align: center;">