mirror of
https://github.com/getnamingo/registry.git
synced 2025-08-11 11:59:30 +02:00
Refactored whois and das for high load
This commit is contained in:
parent
94e0b9942b
commit
eebf61ca56
3 changed files with 653 additions and 734 deletions
|
@ -4,12 +4,27 @@ if (!extension_loaded('swoole')) {
|
||||||
die('Swoole extension must be installed');
|
die('Swoole extension must be installed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use Swoole\Server;
|
||||||
|
|
||||||
|
$c = require_once 'config.php';
|
||||||
require_once 'helpers.php';
|
require_once 'helpers.php';
|
||||||
$logFilePath = '/var/log/namingo/das.log';
|
$logFilePath = '/var/log/namingo/das.log';
|
||||||
$log = setupLogger($logFilePath, 'DAS');
|
$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
|
// Create a Swoole TCP server
|
||||||
$server = new Swoole\Server('0.0.0.0', 1043);
|
$server = new Server('0.0.0.0', 1043);
|
||||||
$server->set([
|
$server->set([
|
||||||
'daemonize' => false,
|
'daemonize' => false,
|
||||||
'log_file' => '/var/log/namingo/das_application.log',
|
'log_file' => '/var/log/namingo/das_application.log',
|
||||||
|
@ -30,89 +45,80 @@ $server->set([
|
||||||
]);
|
]);
|
||||||
$log->info('server started.');
|
$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
|
// Register a callback to handle incoming connections
|
||||||
$server->on('connect', function ($server, $fd) use ($log) {
|
$server->on('connect', function ($server, $fd) use ($log) {
|
||||||
$log->info('new client connected: ' . $fd);
|
$log->info('new client connected: ' . $fd);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register a callback to handle incoming requests
|
// 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
|
||||||
// Validate and sanitize the domain name
|
$pdo = $pool->get();
|
||||||
$domain = trim($data);
|
$domain = trim($data);
|
||||||
if (!$domain) {
|
|
||||||
$server->send($fd, "please enter a domain name");
|
|
||||||
$server->close($fd);
|
|
||||||
}
|
|
||||||
if (strlen($domain) > 68) {
|
|
||||||
$server->send($fd, "domain name is too long");
|
|
||||||
$server->close($fd);
|
|
||||||
}
|
|
||||||
$domain = strtoupper($domain);
|
|
||||||
if (preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $domain)) {
|
|
||||||
$server->send($fd, "domain name invalid format");
|
|
||||||
$server->close($fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract TLD from the domain and prepend a dot
|
|
||||||
$parts = explode('.', $domain);
|
|
||||||
$tld = "." . end($parts);
|
|
||||||
|
|
||||||
// Check if the TLD exists in the domain_tld table
|
|
||||||
$stmtTLD = $pdo->prepare("SELECT COUNT(*) FROM domain_tld WHERE tld = :tld");
|
|
||||||
$stmtTLD->bindParam(':tld', $tld, PDO::PARAM_STR);
|
|
||||||
$stmtTLD->execute();
|
|
||||||
$tldExists = $stmtTLD->fetchColumn();
|
|
||||||
|
|
||||||
if (!$tldExists) {
|
|
||||||
$server->send($fd, "Invalid TLD. Please search only allowed TLDs");
|
|
||||||
$server->close($fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if domain is reserved
|
|
||||||
$stmtReserved = $pdo->prepare("SELECT id FROM reserved_domain_names WHERE name = ? LIMIT 1");
|
|
||||||
$stmtReserved->execute([$parts[0]]);
|
|
||||||
$domain_already_reserved = $stmtReserved->fetchColumn();
|
|
||||||
|
|
||||||
if ($domain_already_reserved) {
|
|
||||||
$server->send($fd, "Domain name is reserved or restricted");
|
|
||||||
$server->close($fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch the IDN regex for the given TLD
|
|
||||||
$stmtRegex = $pdo->prepare("SELECT idn_table FROM domain_tld WHERE tld = :tld");
|
|
||||||
$stmtRegex->bindParam(':tld', $tld, PDO::PARAM_STR);
|
|
||||||
$stmtRegex->execute();
|
|
||||||
$idnRegex = $stmtRegex->fetchColumn();
|
|
||||||
|
|
||||||
if (!$idnRegex) {
|
|
||||||
$server->send($fd, "Failed to fetch domain IDN table");
|
|
||||||
$server->close($fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for invalid characters using fetched regex
|
|
||||||
if (!preg_match($idnRegex, $domain)) {
|
|
||||||
$server->send($fd, "Domain name invalid format");
|
|
||||||
$server->close($fd);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform the DAS lookup
|
// Perform the DAS lookup
|
||||||
try {
|
try {
|
||||||
|
// Validate and sanitize the domain name
|
||||||
|
if (!$domain) {
|
||||||
|
$server->send($fd, "please enter a domain name");
|
||||||
|
$server->close($fd);
|
||||||
|
}
|
||||||
|
if (strlen($domain) > 68) {
|
||||||
|
$server->send($fd, "domain name is too long");
|
||||||
|
$server->close($fd);
|
||||||
|
}
|
||||||
|
$domain = strtoupper($domain);
|
||||||
|
if (preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $domain)) {
|
||||||
|
$server->send($fd, "domain name invalid format");
|
||||||
|
$server->close($fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract TLD from the domain and prepend a dot
|
||||||
|
$parts = explode('.', $domain);
|
||||||
|
$tld = "." . end($parts);
|
||||||
|
|
||||||
|
// Check if the TLD exists in the domain_tld table
|
||||||
|
$stmtTLD = $pdo->prepare("SELECT COUNT(*) FROM domain_tld WHERE tld = :tld");
|
||||||
|
$stmtTLD->bindParam(':tld', $tld, PDO::PARAM_STR);
|
||||||
|
$stmtTLD->execute();
|
||||||
|
$tldExists = $stmtTLD->fetchColumn();
|
||||||
|
|
||||||
|
if (!$tldExists) {
|
||||||
|
$server->send($fd, "Invalid TLD. Please search only allowed TLDs");
|
||||||
|
$server->close($fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if domain is reserved
|
||||||
|
$stmtReserved = $pdo->prepare("SELECT id FROM reserved_domain_names WHERE name = ? LIMIT 1");
|
||||||
|
$stmtReserved->execute([$parts[0]]);
|
||||||
|
$domain_already_reserved = $stmtReserved->fetchColumn();
|
||||||
|
|
||||||
|
if ($domain_already_reserved) {
|
||||||
|
$server->send($fd, "Domain name is reserved or restricted");
|
||||||
|
$server->close($fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the IDN regex for the given TLD
|
||||||
|
$stmtRegex = $pdo->prepare("SELECT idn_table FROM domain_tld WHERE tld = :tld");
|
||||||
|
$stmtRegex->bindParam(':tld', $tld, PDO::PARAM_STR);
|
||||||
|
$stmtRegex->execute();
|
||||||
|
$idnRegex = $stmtRegex->fetchColumn();
|
||||||
|
|
||||||
|
if (!$idnRegex) {
|
||||||
|
$server->send($fd, "Failed to fetch domain IDN table");
|
||||||
|
$server->close($fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for invalid characters using fetched regex
|
||||||
|
if (!preg_match($idnRegex, $domain)) {
|
||||||
|
$server->send($fd, "Domain name invalid format");
|
||||||
|
$server->close($fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$query = "SELECT name FROM registry.domain WHERE name = :domain";
|
$query = "SELECT name FROM registry.domain WHERE name = :domain";
|
||||||
$stmt = $pdo->prepare($query);
|
$stmt = $pdo->prepare($query);
|
||||||
$stmt->bindParam(':domain', $domain, PDO::PARAM_STR);
|
$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);
|
$clientInfo = $server->getClientInfo($fd);
|
||||||
$remoteAddr = $clientInfo['remote_ip'];
|
$remoteAddr = $clientInfo['remote_ip'];
|
||||||
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | FOUND');
|
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | FOUND');
|
||||||
$server->close($fd);
|
|
||||||
} else {
|
} else {
|
||||||
$server->send($fd, "0");
|
$server->send($fd, "0");
|
||||||
|
|
||||||
$clientInfo = $server->getClientInfo($fd);
|
$clientInfo = $server->getClientInfo($fd);
|
||||||
$remoteAddr = $clientInfo['remote_ip'];
|
$remoteAddr = $clientInfo['remote_ip'];
|
||||||
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | NOT FOUND');
|
$log->notice('new request from ' . $remoteAddr . ' | ' . $domain . ' | NOT FOUND');
|
||||||
$server->close($fd);
|
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
$log->error('DB Connection failed: ' . $e->getMessage());
|
// Handle database exceptions
|
||||||
$server->send($fd, "Error connecting to the das database");
|
$log->error('Database error: ' . $e->getMessage());
|
||||||
|
$server->send($fd, "Error connecting to the DAS database");
|
||||||
$server->close($fd);
|
$server->close($fd);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
|
// Catch any other exceptions or errors
|
||||||
$log->error('Error: ' . $e->getMessage());
|
$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);
|
$server->close($fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the connection
|
|
||||||
$pdo = null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register a callback to handle client disconnections
|
// Register a callback to handle client disconnections
|
||||||
|
|
|
@ -40,3 +40,15 @@ function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
|
|
||||||
return $log;
|
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];
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue