Many whois improvements

This commit is contained in:
Pinga 2023-08-04 15:26:05 +03:00
parent a0fcc66270
commit 9b961aba55

View file

@ -6,10 +6,17 @@ if (!extension_loaded('swoole')) {
// Create a Swoole TCP server // Create a Swoole TCP server
$server = new Swoole\Server('0.0.0.0', 43); $server = new Swoole\Server('0.0.0.0', 43);
$server->set([
'daemonize' => false,
'log_file' => '/var/log/whois/whois.log',
'log_level' => SWOOLE_LOG_INFO,
'worker_num' => swoole_cpu_num() * 2,
'pid_file' => '/var/log/whois/whois.pid'
]);
// Register a callback to handle incoming connections // Register a callback to handle incoming connections
$server->on('connect', function ($server, $fd) { $server->on('connect', function ($server, $fd) {
echo "Client connected: {$fd}"; echo "Client connected: {$fd}\r\n";
}); });
// Register a callback to handle incoming requests // Register a callback to handle incoming requests
@ -18,24 +25,24 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
$domain = trim($data); $domain = trim($data);
if (!$domain) { if (!$domain) {
$server->send($fd, "please enter a domain name"); $server->send($fd, "please enter a domain name");
return; $server->close($fd);
} }
if (strlen($domain) > 68) { if (strlen($domain) > 68) {
$server->send($fd, "domain name is too long"); $server->send($fd, "domain name is too long");
return; $server->close($fd);
} }
$domain = strtoupper($domain); $domain = strtoupper($domain);
if (preg_match("/[^A-Z0-9\.\-]/", $domain)) { if (preg_match("/[^A-Z0-9\.\-]/", $domain)) {
$server->send($fd, "domain name invalid format"); $server->send($fd, "domain name invalid format");
return; $server->close($fd);
} }
if (preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $domain)) { if (preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $domain)) {
$server->send($fd, "domain name invalid format"); $server->send($fd, "domain name invalid format");
return; $server->close($fd);
} }
if (!preg_match("/^[A-Z0-9-]+\.(XX|COM\.XX|ORG\.XX|INFO\.XX|PRO\.XX)$/", $domain)) { if (!preg_match("/^[A-Z0-9-]+\.(XX|COM\.XX|ORG\.XX|INFO\.XX|PRO\.XX)$/", $domain)) {
$server->send($fd, "please search only XX domains at least 2 letters"); $server->send($fd, "please search only XX domains at least 2 letters");
return; $server->close($fd);
} }
// Connect to the database // Connect to the database
@ -44,7 +51,7 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { } catch (PDOException $e) {
$server->send($fd, "Error connecting to database"); $server->send($fd, "Error connecting to database");
return; $server->close($fd);
} }
// Perform the WHOIS lookup // Perform the WHOIS lookup
@ -70,24 +77,6 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
$tld = $stmt2->fetch(PDO::FETCH_ASSOC); $tld = $stmt2->fetch(PDO::FETCH_ASSOC);
$res = "Access to {$tld['tld']} WHOIS information is provided to assist persons in"
."\ndetermining the contents of a domain name registration record in the"
."\nDomain Name Registry registry database. The data in this record is provided by"
."\nDomain Name Registry for informational purposes only, and Domain Name Registry does not"
."\nguarantee its accuracy. This service is intended only for query-based"
."\naccess. You agree that you will use this data only for lawful purposes"
."\nand that, under no circumstances will you use this data to: (a) allow,"
."\nenable, or otherwise support the transmission by e-mail, telephone, or"
."\nfacsimile of mass unsolicited, commercial advertising or solicitations"
."\nto entities other than the data recipient's own existing customers; or"
."\n(b) enable high volume, automated, electronic processes that send"
."\nqueries or data to the systems of Registry Operator, a Registrar, or"
."\nNIC except as reasonably necessary to register domain names or"
."\nmodify existing registrations. All rights reserved. Domain Name Registry reserves"
."\nthe right to modify these terms at any time. By submitting this query,"
."\nyou agree to abide by this policy."
."\n";
$query3 = "SELECT `name`,`whois_server`,`url`,`abuse_email`,`abuse_phone` FROM `registrar` WHERE `id` = :clid"; $query3 = "SELECT `name`,`whois_server`,`url`,`abuse_email`,`abuse_phone` FROM `registrar` WHERE `id` = :clid";
$stmt3 = $pdo->prepare($query3); $stmt3 = $pdo->prepare($query3);
$stmt3->bindParam(':clid', $f['clid'], PDO::PARAM_INT); $stmt3->bindParam(':clid', $f['clid'], PDO::PARAM_INT);
@ -95,9 +84,8 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
$clidF = $stmt3->fetch(PDO::FETCH_ASSOC); $clidF = $stmt3->fetch(PDO::FETCH_ASSOC);
$res .= "" $res = "Domain Name: ".strtoupper($f['name'])
."\nRegistry Domain ID: ".$f['id'] ."\nRegistry Domain ID: ".$f['id']
."\nDomain Name:".strtoupper($f['name'])
."\nCreated On: ".$f['crdate'] ."\nCreated On: ".$f['crdate']
."\nLast Updated On: ".$f['update'] ."\nLast Updated On: ".$f['update']
."\nExpiration Date: ".$f['exdate'] ."\nExpiration Date: ".$f['exdate']
@ -213,31 +201,57 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
$stmt9->bindParam(':domain_id', $f['id'], PDO::PARAM_INT); $stmt9->bindParam(':domain_id', $f['id'], PDO::PARAM_INT);
$stmt9->execute(); $stmt9->execute();
for ($i=0; $i<13; $i++) { $counter = 0;
while ($counter < 13) {
$f2 = $stmt9->fetch(PDO::FETCH_ASSOC); $f2 = $stmt9->fetch(PDO::FETCH_ASSOC);
if ($f2 === false) break; // Break if there are no more rows
$res .= "\nName Server: ".$f2['name']; $res .= "\nName Server: ".$f2['name'];
$counter++;
} }
$res .= "\nDNSSEC: Unsigned"; $res .= "\nDNSSEC: Unsigned";
$res .= "\n\n"; $res .= "\n\n";
$res .= "Access to {$tld['tld']} WHOIS information is provided to assist persons in"
."\ndetermining the contents of a domain name registration record in the"
."\nDomain Name Registry registry database. The data in this record is provided by"
."\nDomain Name Registry for informational purposes only, and Domain Name Registry does not"
."\nguarantee its accuracy. This service is intended only for query-based"
."\naccess. You agree that you will use this data only for lawful purposes"
."\nand that, under no circumstances will you use this data to: (a) allow,"
."\nenable, or otherwise support the transmission by e-mail, telephone, or"
."\nfacsimile of mass unsolicited, commercial advertising or solicitations"
."\nto entities other than the data recipient's own existing customers; or"
."\n(b) enable high volume, automated, electronic processes that send"
."\nqueries or data to the systems of Registry Operator, a Registrar, or"
."\nNIC except as reasonably necessary to register domain names or"
."\nmodify existing registrations. All rights reserved. Domain Name Registry reserves"
."\nthe right to modify these terms at any time. By submitting this query,"
."\nyou agree to abide by this policy."
."\n";
$server->send($fd, $res . ""); $server->send($fd, $res . "");
if ($fp = @fopen("/var/log/whois/whois.log",'a')) { if ($fp = @fopen("/var/log/whois/whois_request.log",'a')) {
fwrite($fp,date('Y-m-d H:i:s')."\t-\t".getenv('REMOTE_ADDR')."\t-\t".$domain."\n"); $clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
fwrite($fp,date('Y-m-d H:i:s')."\t-\t".$remoteAddr."\t-\t".$domain."\n");
fclose($fp); fclose($fp);
} }
$server->close($fd);
} else { } else {
//NOT FOUND or No match for; //NOT FOUND or No match for;
$server->send($fd, "NOT FOUND"); $server->send($fd, "NOT FOUND");
if ($fp = @fopen("/var/log/whois/whois_not_found.log",'a')) { if ($fp = @fopen("/var/log/whois/whois_not_found.log",'a')) {
fwrite($fp,date('Y-m-d H:i:s')."\t-\t".getenv('REMOTE_ADDR')."\t-\t".$domain."\n"); $clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
fwrite($fp,date('Y-m-d H:i:s')."\t-\t".$remoteAddr."\t-\t".$domain."\n");
fclose($fp); fclose($fp);
} }
$server->close($fd);
} }
} catch (PDOException $e) { } catch (PDOException $e) {
$server->send($fd, "Error connecting to the whois database"); $server->send($fd, "Error connecting to the whois database");
return; $server->close($fd);
} }
// Close the connection // Close the connection
@ -246,10 +260,8 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) {
// Register a callback to handle client disconnections // Register a callback to handle client disconnections
$server->on('close', function ($server, $fd) { $server->on('close', function ($server, $fd) {
echo "Client disconnected: {$fd} echo "Client disconnected: {$fd}\r\n";
";
}); });
// Start the server // Start the server
$server->start(); $server->start();
?>