Refactored whois and das for high load

This commit is contained in:
Pinga 2023-12-02 12:00:03 +02:00
parent 94e0b9942b
commit eebf61ca56
3 changed files with 653 additions and 734 deletions

View file

@ -4,12 +4,27 @@ if (!extension_loaded('swoole')) {
die('Swoole extension must be installed');
}
use Swoole\Server;
$c = require_once 'config.php';
require_once 'helpers.php';
$logFilePath = '/var/log/namingo/das.log';
$log = setupLogger($logFilePath, 'DAS');
// Initialize the PDO connection pool
$pool = new Swoole\Database\PDOPool(
(new Swoole\Database\PDOConfig())
->withDriver($c['db_type'])
->withHost($c['db_host'])
->withPort($c['db_port'])
->withDbName($c['db_database'])
->withUsername($c['db_username'])
->withPassword($c['db_password'])
->withCharset('utf8mb4')
);
// Create a Swoole TCP server
$server = new Swoole\Server('0.0.0.0', 1043);
$server = new Server('0.0.0.0', 1043);
$server->set([
'daemonize' => false,
'log_file' => '/var/log/namingo/das_application.log',
@ -30,27 +45,20 @@ $server->set([
]);
$log->info('server started.');
// Connect to the database
try {
$c = require_once 'config.php';
$pdo = new PDO("{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}", $c['db_username'], $c['db_password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to database");
$server->close($fd);
}
// Register a callback to handle incoming connections
$server->on('connect', function ($server, $fd) use ($log) {
$log->info('new client connected: ' . $fd);
});
// Register a callback to handle incoming requests
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo, $log) {
// Validate and sanitize the domain name
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log) {
// Get a PDO connection from the pool
$pdo = $pool->get();
$domain = trim($data);
// Perform the DAS lookup
try {
// Validate and sanitize the domain name
if (!$domain) {
$server->send($fd, "please enter a domain name");
$server->close($fd);
@ -111,8 +119,6 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo,
return;
}
// Perform the DAS lookup
try {
$query = "SELECT name FROM registry.domain WHERE name = :domain";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':domain', $domain, PDO::PARAM_STR);
@ -124,27 +130,28 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo,
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | FOUND');
$server->close($fd);
} else {
$server->send($fd, "0");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | NOT FOUND');
$server->close($fd);
}
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the das database");
// Handle database exceptions
$log->error('Database error: ' . $e->getMessage());
$server->send($fd, "Error connecting to the DAS database");
$server->close($fd);
} catch (Throwable $e) {
// Catch any other exceptions or errors
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->send($fd, "Error");
$server->close($fd);
} finally {
// Return the connection to the pool
$pool->put($pdo);
$server->close($fd);
}
// Close the connection
$pdo = null;
});
// Register a callback to handle client disconnections

View file

@ -40,3 +40,15 @@ function setupLogger($logFilePath, $channelName = 'app') {
return $log;
}
function parseQuery($data) {
$data = trim($data);
if (strpos($data, 'nameserver ') === 0) {
return ['type' => 'nameserver', 'data' => substr($data, 11)];
} elseif (strpos($data, 'registrar ') === 0) {
return ['type' => 'registrar', 'data' => substr($data, 10)];
} else {
return ['type' => 'domain', 'data' => $data];
}
}

View file

@ -4,12 +4,27 @@ if (!extension_loaded('swoole')) {
die('Swoole extension must be installed');
}
use Swoole\Server;
$c = require_once 'config.php';
require_once 'helpers.php';
$logFilePath = '/var/log/namingo/whois.log';
$log = setupLogger($logFilePath, 'WHOIS');
// Initialize the PDO connection pool
$pool = new Swoole\Database\PDOPool(
(new Swoole\Database\PDOConfig())
->withDriver($c['db_type'])
->withHost($c['db_host'])
->withPort($c['db_port'])
->withDbName($c['db_database'])
->withUsername($c['db_username'])
->withPassword($c['db_password'])
->withCharset('utf8mb4')
);
// Create a Swoole TCP server
$server = new Swoole\Server('0.0.0.0', 43);
$server = new Server('0.0.0.0', 43);
$server->set([
'daemonize' => false,
'log_file' => '/var/log/namingo/whois_application.log',
@ -30,307 +45,24 @@ $server->set([
]);
$log->info('server started.');
// Connect to the database
try {
$c = require_once 'config.php';
$pdo = new PDO("{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}", $c['db_username'], $c['db_password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to database");
$server->close($fd);
}
// Register a callback to handle incoming connections
$server->on('connect', function ($server, $fd) use ($log) {
$log->info('new client connected: ' . $fd);
});
// Register a callback to handle incoming requests
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo, $log) {
$server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log) {
// Get a PDO connection from the pool
$pdo = $pool->get();
$privacy = $c['privacy'];
// Validate and sanitize the data
$data = trim($data);
// Check if the query is for a nameserver
if (strpos($data, 'nameserver ') === 0) {
$queryType = 'nameserver';
$queryData = str_replace('nameserver ', '', $data);
}
// Check if the query is for a registrar
elseif (strpos($data, 'registrar ') === 0) {
$queryType = 'registrar';
$queryData = str_replace('registrar ', '', $data);
}
// If none of the above, assume it's a domain query
else {
$queryType = 'domain';
$queryData = $data;
}
$parsedQuery = parseQuery($data);
$queryType = $parsedQuery['type'];
$queryData = $parsedQuery['data'];
// Handle the WHOIS query
if ($queryType == 'nameserver') {
// Handle nameserver query
$nameserver = $queryData;
if (!$nameserver) {
$server->send($fd, "please enter a nameserver");
$server->close($fd);
}
if (strlen($nameserver) > 63) {
$server->send($fd, "nameserver is too long");
$server->close($fd);
}
if (!preg_match('/^([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,}$/', $nameserver)) {
$server->send($fd, "Nameserver contains invalid characters or is not in the correct format.");
$server->close($fd);
}
// Perform the WHOIS lookup
try {
$query = "SELECT name,clid FROM host WHERE name = :nameserver";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':nameserver', $nameserver, PDO::PARAM_STR);
$stmt->execute();
if ($f = $stmt->fetch(PDO::FETCH_ASSOC)) {
$res = "Server Name: ".$f['name'];
// Fetch the registrar details for this registrar using the id
$regQuery = "SELECT id,name,iana_id,whois_server,url,abuse_email,abuse_phone FROM registrar WHERE id = :clid";
$regStmt = $pdo->prepare($regQuery);
$regStmt->bindParam(':clid', $f['clid'], PDO::PARAM_INT);
$regStmt->execute();
if ($registrar = $regStmt->fetch(PDO::FETCH_ASSOC)) {
// Append the registrar details to the response
$res .= "\nRegistrar Name: ".$registrar['name'];
$res .= "\nRegistrar WHOIS Server: ".$registrar['whois_server'];
$res .= "\nRegistrar URL: ".$registrar['url'];
$res .= "\nRegistrar IANA ID: ".$registrar['iana_id'];
$res .= "\nRegistrar Abuse Contact Email: ".$registrar['abuse_email'];
$res .= "\nRegistrar Abuse Contact Phone: ".$registrar['abuse_phone'];
}
$res .= "\nURL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/";
$currentDateTime = new DateTime();
$currentTimestamp = $currentDateTime->format("Y-m-d\TH:i:s.v\Z");
$res .= "\n>>> Last update of WHOIS database: {$currentTimestamp} <<<";
$res .= "\n";
$res .= "\nFor more information on Whois status codes, please visit https://icann.org/epp";
$res .= "\n\n";
$res .= "Access to 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 . "");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $nameserver . ' | FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
$server->close($fd);
} else {
//NOT FOUND or No match for;
$server->send($fd, "NOT FOUND");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $nameserver . ' | NOT FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
$server->close($fd);
}
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
}
elseif ($queryType == 'registrar') {
// Handle registrar query
$registrar = $queryData;
if (!$registrar) {
$server->send($fd, "please enter a registrar name");
$server->close($fd);
}
if (strlen($registrar) > 50) {
$server->send($fd, "registrar name is too long");
$server->close($fd);
}
if (!preg_match('/^[a-zA-Z0-9\s\-]+$/', $registrar)) {
$server->send($fd, "Registrar name contains invalid characters.");
$server->close($fd);
}
// Perform the WHOIS lookup
try {
$query = "SELECT id,name,iana_id,whois_server,url,abuse_email,abuse_phone FROM registrar WHERE name = :registrar";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':registrar', $registrar, PDO::PARAM_STR);
$stmt->execute();
if ($f = $stmt->fetch(PDO::FETCH_ASSOC)) {
$res = "Registrar: ".$f['name']
."\nRegistrar WHOIS Server: ".$f['whois_server']
."\nRegistrar URL: ".$f['url']
."\nRegistrar IANA ID: ".$f['iana_id']
."\nRegistrar Abuse Contact Email: ".$f['abuse_email']
."\nRegistrar Abuse Contact Phone: ".$f['abuse_phone'];
// Fetch the contact details for this registrar using the id
$contactQuery = "SELECT * FROM registrar_contact WHERE id = :registrar_id";
$contactStmt = $pdo->prepare($contactQuery);
$contactStmt->bindParam(':registrar_id', $f['id'], PDO::PARAM_INT);
$contactStmt->execute();
if ($contact = $contactStmt->fetch(PDO::FETCH_ASSOC)) {
// Append the contact details to the response
$res .= "\nStreet: " . $contact['street1'];
$res .= "\nCity: " . $contact['city'];
$res .= "\nPostal Code: " . $contact['pc'];
$res .= "\nCountry: " . $contact['cc'];
$res .= "\nPhone: " . $contact['voice'];
$res .= "\nFax: " . $contact['fax'];
$res .= "\nPublic Email: " . $contact['email'];
}
$res .= "\nURL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/";
$currentDateTime = new DateTime();
$currentTimestamp = $currentDateTime->format("Y-m-d\TH:i:s.v\Z");
$res .= "\n>>> Last update of WHOIS database: {$currentTimestamp} <<<";
$res .= "\n";
$res .= "\nFor more information on Whois status codes, please visit https://icann.org/epp";
$res .= "\n\n";
$res .= "Access to 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 . "");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $registrar . ' | FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
$server->close($fd);
} else {
//NOT FOUND or No match for;
$server->send($fd, "NOT FOUND");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $registrar . ' | NOT FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
$server->close($fd);
}
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
}
else {
switch ($queryType) {
case 'domain':
// Handle domain query
$domain = $queryData;
@ -394,8 +126,6 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo,
return;
}
// Perform the WHOIS lookup
try {
$query = "SELECT * FROM registry.domain WHERE name = :domain";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':domain', $domain, PDO::PARAM_STR);
@ -648,22 +378,10 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo,
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->close($fd);
}
$server->close($fd);
} else {
//NOT FOUND or No match for;
$server->send($fd, "NOT FOUND");
@ -672,36 +390,218 @@ $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pdo,
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | NOT FOUND');
try {
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
}
break;
case 'nameserver':
// Handle nameserver query
$nameserver = $queryData;
if (!$nameserver) {
$server->send($fd, "please enter a nameserver");
$server->close($fd);
} catch (Throwable $e) {
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
}
if (strlen($nameserver) > 63) {
$server->send($fd, "nameserver is too long");
$server->close($fd);
}
if (!preg_match('/^([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,}$/', $nameserver)) {
$server->send($fd, "Nameserver contains invalid characters or is not in the correct format.");
$server->close($fd);
}
$query = "SELECT name,clid FROM host WHERE name = :nameserver";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':nameserver', $nameserver, PDO::PARAM_STR);
$stmt->execute();
if ($f = $stmt->fetch(PDO::FETCH_ASSOC)) {
$res = "Server Name: ".$f['name'];
// Fetch the registrar details for this registrar using the id
$regQuery = "SELECT id,name,iana_id,whois_server,url,abuse_email,abuse_phone FROM registrar WHERE id = :clid";
$regStmt = $pdo->prepare($regQuery);
$regStmt->bindParam(':clid', $f['clid'], PDO::PARAM_INT);
$regStmt->execute();
if ($registrar = $regStmt->fetch(PDO::FETCH_ASSOC)) {
// Append the registrar details to the response
$res .= "\nRegistrar Name: ".$registrar['name'];
$res .= "\nRegistrar WHOIS Server: ".$registrar['whois_server'];
$res .= "\nRegistrar URL: ".$registrar['url'];
$res .= "\nRegistrar IANA ID: ".$registrar['iana_id'];
$res .= "\nRegistrar Abuse Contact Email: ".$registrar['abuse_email'];
$res .= "\nRegistrar Abuse Contact Phone: ".$registrar['abuse_phone'];
}
$res .= "\nURL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/";
$currentDateTime = new DateTime();
$currentTimestamp = $currentDateTime->format("Y-m-d\TH:i:s.v\Z");
$res .= "\n>>> Last update of WHOIS database: {$currentTimestamp} <<<";
$res .= "\n";
$res .= "\nFor more information on Whois status codes, please visit https://icann.org/epp";
$res .= "\n\n";
$res .= "Access to 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 . "");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $nameserver . ' | FOUND');
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} else {
//NOT FOUND or No match for;
$server->send($fd, "NOT FOUND");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $nameserver . ' | NOT FOUND');
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
}
break;
case 'registrar':
// Handle registrar query
$registrar = $queryData;
if (!$registrar) {
$server->send($fd, "please enter a registrar name");
$server->close($fd);
}
if (strlen($registrar) > 50) {
$server->send($fd, "registrar name is too long");
$server->close($fd);
}
if (!preg_match('/^[a-zA-Z0-9\s\-]+$/', $registrar)) {
$server->send($fd, "Registrar name contains invalid characters.");
$server->close($fd);
}
$query = "SELECT id,name,iana_id,whois_server,url,abuse_email,abuse_phone FROM registrar WHERE name = :registrar";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':registrar', $registrar, PDO::PARAM_STR);
$stmt->execute();
if ($f = $stmt->fetch(PDO::FETCH_ASSOC)) {
$res = "Registrar: ".$f['name']
."\nRegistrar WHOIS Server: ".$f['whois_server']
."\nRegistrar URL: ".$f['url']
."\nRegistrar IANA ID: ".$f['iana_id']
."\nRegistrar Abuse Contact Email: ".$f['abuse_email']
."\nRegistrar Abuse Contact Phone: ".$f['abuse_phone'];
// Fetch the contact details for this registrar using the id
$contactQuery = "SELECT * FROM registrar_contact WHERE id = :registrar_id";
$contactStmt = $pdo->prepare($contactQuery);
$contactStmt->bindParam(':registrar_id', $f['id'], PDO::PARAM_INT);
$contactStmt->execute();
if ($contact = $contactStmt->fetch(PDO::FETCH_ASSOC)) {
// Append the contact details to the response
$res .= "\nStreet: " . $contact['street1'];
$res .= "\nCity: " . $contact['city'];
$res .= "\nPostal Code: " . $contact['pc'];
$res .= "\nCountry: " . $contact['cc'];
$res .= "\nPhone: " . $contact['voice'];
$res .= "\nFax: " . $contact['fax'];
$res .= "\nPublic Email: " . $contact['email'];
}
$res .= "\nURL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/";
$currentDateTime = new DateTime();
$currentTimestamp = $currentDateTime->format("Y-m-d\TH:i:s.v\Z");
$res .= "\n>>> Last update of WHOIS database: {$currentTimestamp} <<<";
$res .= "\n";
$res .= "\nFor more information on Whois status codes, please visit https://icann.org/epp";
$res .= "\n\n";
$res .= "Access to 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 . "");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $registrar . ' | FOUND');
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
} else {
//NOT FOUND or No match for;
$server->send($fd, "NOT FOUND");
$clientInfo = $server->getClientInfo($fd);
$remoteAddr = $clientInfo['remote_ip'];
$log->notice('new request from ' . $remoteAddr . ' | ' . $registrar . ' | NOT FOUND');
$stmt = $pdo->prepare("UPDATE settings SET value = value + 1 WHERE name = :name");
$settingName = 'whois-43-queries';
$stmt->bindParam(':name', $settingName);
$stmt->execute();
}
break;
default:
// Handle unknown query type
$log->error('Error');
$server->send($fd, "Error");
}
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
// Handle database exceptions
$log->error('Database error: ' . $e->getMessage());
$server->send($fd, "Error connecting to the whois database");
$server->close($fd);
} catch (Throwable $e) {
// Catch any other exceptions or errors
$log->error('Error: ' . $e->getMessage());
$server->send($fd, "General error");
$server->send($fd, "Error");
$server->close($fd);
} finally {
// Return the connection to the pool
$pool->put($pdo);
$server->close($fd);
}
}
// Close the connection
$pdo = null;
});
// Register a callback to handle client disconnections