Skip to content

Commit

Permalink
improved whois server limits handler
Browse files Browse the repository at this point in the history
  • Loading branch information
damianofalcioni authored Nov 27, 2017
1 parent a6dca05 commit 44352ce
Showing 1 changed file with 51 additions and 41 deletions.
92 changes: 51 additions & 41 deletions ipb.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ function getallheaders() {
trigger : 'hover click'
});
}, function(error){
Utils.showError(error, $('#reportsTable'));
Utils.showError(error, $('#trackReportMsgs'));
});

});

}, function(error){
Utils.showError(error, $('#reportsTable'));
Utils.showError(error, $('#trackReportMsgs'));
});
},

Expand Down Expand Up @@ -1163,49 +1163,59 @@ function getallheaders() {
if(!isset($_GET['ip']) || $_GET['ip']=='')
throw new Exception('ip parameter required');
$ip = $_GET['ip'];
/*WHOIS IP Server list
whois.afrinic.net -> Africa - but returns data for ALL locations worldwide
whois.lacnic.net -> Latin America and Caribbean but returns data for ALL locations worldwide
whois.apnic.net -> Asia/Pacific only
whois.arin.net -> North America only
whois.ripe.net -> Europe, Middle East and Central Asia only
*/
$whoisserver = 'whois.lacnic.net';
if(!($fp = fsockopen($whoisserver, 43, $errno, $errstr, 10)))
throw new Exception("Error contacting the WHOIS server $whoisserver : $errstr ($errno)");

fprintf($fp, "%s\r\n", $ip);
$whoisResultString = "";
$netNameString = "";
$lineCount = 0;
while(!feof($fp)){
$line = fgets($fp);
if(trim($line[0])!='' && $line[0]!='#' && $line[0]!='%'){
$whoisResultString.=$line;
$lineCount++;
if(strpos($line, ':') !== false){
$lineSplit = explode(':', $line);
if(strtolower($lineSplit[0])=='netname')
$netNameString = trim($lineSplit[1]);
} else {
if($lineCount == 2)
$netNameString = trim(explode('(NET', $line)[0]);
if(empty(session_id()))
session_start();

if(isset($_SESSION['whois_'.$ip])){
echo $_SESSION['whois_'.$ip];
} else {
/*WHOIS IP Server list
whois.afrinic.net -> Africa - but returns data for ALL locations worldwide
whois.lacnic.net -> Latin America and Caribbean but returns data for ALL locations worldwide
whois.apnic.net -> Asia/Pacific only
whois.arin.net -> North America only
whois.ripe.net -> Europe, Middle East and Central Asia only
*/
$whoisserver = 'whois.lacnic.net';
if(!($fp = fsockopen($whoisserver, 43, $errno, $errstr, 10)))
throw new Exception("Error contacting the WHOIS server $whoisserver : $errstr ($errno)");

fprintf($fp, "%s\r\n", $ip);
$whoisResultString = "";
$netNameString = "";
$lineCount = 0;
while(!feof($fp)){
$line = fgets($fp);
if(trim($line[0])!='' && $line[0]!='#' && $line[0]!='%'){
$whoisResultString.=$line;
$lineCount++;
if(strpos($line, ':') !== false){
$lineSplit = explode(':', $line);
if(strtolower($lineSplit[0])=='netname')
$netNameString = trim($lineSplit[1]);
} else {
if($lineCount == 2)
$netNameString = trim(explode('(NET', $line)[0]);
}
}
}
fclose($fp);

$ret = json_encode((object) array(
'status' => 0,
'whoisResults' => (object) array(
'output' => utf8_encode($whoisResultString),
'netName' => utf8_encode($netNameString)
)
));

if(json_last_error()!=0)
throw new Exception("Error encoding the JSON response");
$_SESSION['whois_'.$ip] = $ret;
echo $ret;
}
fclose($fp);

$ret = json_encode((object) array(
'status' => 0,
'whoisResults' => (object) array(
'output' => utf8_encode($whoisResultString),
'netName' => utf8_encode($netNameString)
)
));

if(json_last_error()!=0)
throw new Exception("Error encoding the JSON response");
echo $ret;
session_write_close();
}catch(Exception $ex){
echo '{"status" : -1, "error" : "'.$ex->getMessage().'"}';
$logError($ex->getMessage());
Expand Down

0 comments on commit 44352ce

Please sign in to comment.