From: Stefan Schantl Date: Thu, 12 Dec 2019 11:08:18 +0000 (+0100) Subject: ipinfo.cgi: Use continent RIR whois server. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Fswitch-to-libloc;p=people%2Fstevee%2Fipfire-2.x.git ipinfo.cgi: Use continent RIR whois server. Determine the continent for the current processed IP-Address and send the request to the responsible whois server of the local RIR instead of sending all requests to ARIN. Fixes #11267. Signed-off-by: Stefan Schantl --- diff --git a/html/cgi-bin/ipinfo.cgi b/html/cgi-bin/ipinfo.cgi index 6465bc3e4c..abe8a0b91b 100644 --- a/html/cgi-bin/ipinfo.cgi +++ b/html/cgi-bin/ipinfo.cgi @@ -41,12 +41,22 @@ my %cgiparams=(); my @lines=(); my $extraquery=''; +# Hash which contains the whois servers from +# the responisible RIR of the continent. +my %whois_servers_by_continent = ( + "AF" => "whois.afrinic.net", + "AS" => "whois.apnic.net", + "EU" => "whois.ripe.net", + "NA" => "whois.arin.net", + "SA" => "whois.lacnic.net" +); + +# Default whois server if no continent could be determined. +my $whois_server = "whois.arin.net"; + my $addr = CGI::param("ip") || ""; if (&General::validip($addr)) { - $extraquery=''; - @lines=(); - my $whoisname = "whois.arin.net"; my $iaddr = inet_aton($addr); my $hostname = gethostbyaddr($iaddr, AF_INET); if (!$hostname) { $hostname = $Lang::tr{'lookup failed'}; } @@ -54,21 +64,31 @@ if (&General::validip($addr)) { # enumerate GeoIP information for IP address... my $db_handle = &GeoIP::init(); my $ccode = &GeoIP::lookup_country_code($db_handle, $addr); + + # Try to get the continent of the country code. + my $continent = &GeoIP::get_continent_code($db_handle, $ccode); + + # Check if a whois server for the continent is known. + if($whois_servers_by_continent{$continent}) { + # Use it. + $whois_server = $whois_servers_by_continent{$continent}; + } + my $flag_icon = &GeoIP::get_flag_icon($ccode); - my $sock = new IO::Socket::INET ( PeerAddr => $whoisname, PeerPort => 43, Proto => 'tcp'); + my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp'); if ($sock) { - print $sock "n $addr\n"; + print $sock "$addr\n"; while (<$sock>) { - $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/); + $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/); push(@lines,$_); } close($sock); if ($extraquery) { undef (@lines); - $whoisname = $extraquery; - my $sock = new IO::Socket::INET ( PeerAddr => $whoisname, PeerPort => 43, Proto => 'tcp'); + $whois_server = $extraquery; + my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp'); if ($sock) { print $sock "$addr\n"; @@ -78,16 +98,16 @@ if (&General::validip($addr)) { } else { - @lines = ( "$Lang::tr{'unable to contact'} $whoisname" ); + @lines = ( "$Lang::tr{'unable to contact'} $whois_server" ); } } } else { - @lines = ( "$Lang::tr{'unable to contact'} $whoisname" ); + @lines = ( "$Lang::tr{'unable to contact'} $whois_server" ); } - &Header::openbox('100%', 'left', $addr . " $ccode (" . $hostname . ') : '.$whoisname); + &Header::openbox('100%', 'left', $addr . " $ccode (" . $hostname . ') : '.$whois_server); print "
\n";
 	foreach my $line (@lines) {
 		print &Header::cleanhtml($line,"y");