]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ipinfo.cgi: Use continent RIR whois server. switch-to-libloc
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 12 Dec 2019 11:08:18 +0000 (12:08 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 12 Dec 2019 11:12:59 +0000 (12:12 +0100)
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 <stefan.schantl@ipfire.org>
html/cgi-bin/ipinfo.cgi

index 6465bc3e4c0f4e1baea967ba885e5183edf7e015..abe8a0b91b4b6f40d167a3f057b5514a1f13c4ef 100644 (file)
@@ -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 "$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 . " <a href='country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode' /></a> (" . $hostname . ') : '.$whoisname);
+       &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);
        print "<pre>\n";
        foreach my $line (@lines) {
                print &Header::cleanhtml($line,"y");