mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-11 01:08:34 +02:00
Fixes for high load situations
This commit is contained in:
parent
9ce1196c2d
commit
94e0b9942b
2 changed files with 180 additions and 158 deletions
|
@ -40,3 +40,55 @@ function setupLogger($logFilePath, $channelName = 'app') {
|
||||||
|
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapContactToVCard($contactDetails, $role, $c) {
|
||||||
|
return [
|
||||||
|
'objectClassName' => 'entity',
|
||||||
|
'handle' => ['C' . $contactDetails['identifier'] . '-' . $c['roid']],
|
||||||
|
'roles' => [$role],
|
||||||
|
'remarks' => [
|
||||||
|
[
|
||||||
|
"description" => [
|
||||||
|
"This object's data has been partially omitted for privacy.",
|
||||||
|
"Only the registrar managing the record can view personal contact data."
|
||||||
|
],
|
||||||
|
"links" => [
|
||||||
|
[
|
||||||
|
"href" => "https://namingo.org",
|
||||||
|
"rel" => "alternate",
|
||||||
|
"type" => "text/html"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"title" => "REDACTED FOR PRIVACY",
|
||||||
|
"type" => "Details are withheld due to privacy restrictions."
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"description" => [
|
||||||
|
"To obtain contact information for the domain registrant, please refer to the Registrar of Record's RDDS service as indicated in this report."
|
||||||
|
],
|
||||||
|
"title" => "EMAIL REDACTED FOR PRIVACY",
|
||||||
|
"type" => "Details are withheld due to privacy restrictions."
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'vcardArray' => [
|
||||||
|
"vcard",
|
||||||
|
[
|
||||||
|
['version', new stdClass(), 'text', '4.0'],
|
||||||
|
["fn", new stdClass(), 'text', $contactDetails['name']],
|
||||||
|
["org", $contactDetails['org']],
|
||||||
|
["adr", [
|
||||||
|
"", // Post office box
|
||||||
|
$contactDetails['street1'], // Extended address
|
||||||
|
$contactDetails['street2'], // Street address
|
||||||
|
$contactDetails['city'], // Locality
|
||||||
|
$contactDetails['sp'], // Region
|
||||||
|
$contactDetails['pc'], // Postal code
|
||||||
|
$contactDetails['cc'] // Country name
|
||||||
|
]],
|
||||||
|
["tel", $contactDetails['voice'], ["type" => "voice"]],
|
||||||
|
["tel", $contactDetails['fax'], ["type" => "fax"]],
|
||||||
|
["email", $contactDetails['email']],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
|
@ -4,64 +4,29 @@ if (!extension_loaded('swoole')) {
|
||||||
die('Swoole extension must be installed');
|
die('Swoole extension must be installed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use Swoole\Http\Server;
|
||||||
|
use Swoole\Http\Request;
|
||||||
|
use Swoole\Http\Response;
|
||||||
|
|
||||||
|
$c = require_once 'config.php';
|
||||||
require_once 'helpers.php';
|
require_once 'helpers.php';
|
||||||
$logFilePath = '/var/log/namingo/rdap.log';
|
$logFilePath = '/var/log/namingo/rdap.log';
|
||||||
$log = setupLogger($logFilePath, 'RDAP');
|
$log = setupLogger($logFilePath, 'RDAP');
|
||||||
|
|
||||||
function mapContactToVCard($contactDetails, $role, $c) {
|
// Initialize the PDO connection pool
|
||||||
return [
|
$pool = new Swoole\Database\PDOPool(
|
||||||
'objectClassName' => 'entity',
|
(new Swoole\Database\PDOConfig())
|
||||||
'handle' => ['C' . $contactDetails['identifier'] . '-' . $c['roid']],
|
->withDriver($c['db_type'])
|
||||||
'roles' => [$role],
|
->withHost($c['db_host'])
|
||||||
'remarks' => [
|
->withPort($c['db_port'])
|
||||||
[
|
->withDbName($c['db_database'])
|
||||||
"description" => [
|
->withUsername($c['db_username'])
|
||||||
"This object's data has been partially omitted for privacy.",
|
->withPassword($c['db_password'])
|
||||||
"Only the registrar managing the record can view personal contact data."
|
->withCharset('utf8mb4')
|
||||||
],
|
);
|
||||||
"links" => [
|
|
||||||
[
|
|
||||||
"href" => "https://namingo.org",
|
|
||||||
"rel" => "alternate",
|
|
||||||
"type" => "text/html"
|
|
||||||
]
|
|
||||||
],
|
|
||||||
"title" => "REDACTED FOR PRIVACY",
|
|
||||||
"type" => "Details are withheld due to privacy restrictions."
|
|
||||||
],
|
|
||||||
[
|
|
||||||
"description" => [
|
|
||||||
"To obtain contact information for the domain registrant, please refer to the Registrar of Record's RDDS service as indicated in this report."
|
|
||||||
],
|
|
||||||
"title" => "EMAIL REDACTED FOR PRIVACY",
|
|
||||||
"type" => "Details are withheld due to privacy restrictions."
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'vcardArray' => [
|
|
||||||
"vcard",
|
|
||||||
[
|
|
||||||
['version', new stdClass(), 'text', '4.0'],
|
|
||||||
["fn", new stdClass(), 'text', $contactDetails['name']],
|
|
||||||
["org", $contactDetails['org']],
|
|
||||||
["adr", [
|
|
||||||
"", // Post office box
|
|
||||||
$contactDetails['street1'], // Extended address
|
|
||||||
$contactDetails['street2'], // Street address
|
|
||||||
$contactDetails['city'], // Locality
|
|
||||||
$contactDetails['sp'], // Region
|
|
||||||
$contactDetails['pc'], // Postal code
|
|
||||||
$contactDetails['cc'] // Country name
|
|
||||||
]],
|
|
||||||
["tel", $contactDetails['voice'], ["type" => "voice"]],
|
|
||||||
["tel", $contactDetails['fax'], ["type" => "fax"]],
|
|
||||||
["email", $contactDetails['email']],
|
|
||||||
]
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a Swoole HTTP server
|
// Create a Swoole HTTP server
|
||||||
$http = new Swoole\Http\Server('0.0.0.0', 7500);
|
$http = new Server('0.0.0.0', 7500);
|
||||||
$http->set([
|
$http->set([
|
||||||
'daemonize' => false,
|
'daemonize' => false,
|
||||||
'log_file' => '/var/log/namingo/rdap_application.log',
|
'log_file' => '/var/log/namingo/rdap_application.log',
|
||||||
|
@ -81,120 +46,125 @@ $http->set([
|
||||||
]);
|
]);
|
||||||
$log->info('server started.');
|
$log->info('server started.');
|
||||||
|
|
||||||
// Connect to the database
|
// Handle incoming HTTP requests
|
||||||
try {
|
$http->on('request', function ($request, $response) use ($c, $pool, $log) {
|
||||||
$c = require_once 'config.php';
|
// Get a PDO connection from the pool
|
||||||
$pdo = new PDO("{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}", $c['db_username'], $c['db_password']);
|
$pdo = $pool->get();
|
||||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
$log->error('DB Connection failed: ' . $e->getMessage());
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->end(json_encode(['error' => 'Error connecting to database']));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register a callback to handle incoming requests
|
try {
|
||||||
$http->on('request', function ($request, $response) use ($c, $pdo, $log) {
|
// Extract the request path
|
||||||
|
$requestPath = $request->server['request_uri'];
|
||||||
|
|
||||||
// Extract the request path
|
// Handle domain query
|
||||||
$requestPath = $request->server['request_uri'];
|
if (preg_match('#^/domain/([^/?]+)#', $requestPath, $matches)) {
|
||||||
|
$domainName = $matches[1];
|
||||||
// Handle domain query
|
handleDomainQuery($request, $response, $pdo, $domainName, $c, $log);
|
||||||
if (preg_match('#^/domain/([^/?]+)#', $requestPath, $matches)) {
|
|
||||||
$domainName = $matches[1];
|
|
||||||
handleDomainQuery($request, $response, $pdo, $domainName, $c, $log);
|
|
||||||
}
|
|
||||||
// Handle entity (contacts) query
|
|
||||||
elseif (preg_match('#^/entity/([^/?]+)#', $requestPath, $matches)) {
|
|
||||||
$entityHandle = $matches[1];
|
|
||||||
handleEntityQuery($request, $response, $pdo, $entityHandle, $c, $log);
|
|
||||||
}
|
|
||||||
// Handle nameserver query
|
|
||||||
elseif (preg_match('#^/nameserver/([^/?]+)#', $requestPath, $matches)) {
|
|
||||||
$nameserverHandle = $matches[1];
|
|
||||||
handleNameserverQuery($request, $response, $pdo, $nameserverHandle, $c, $log);
|
|
||||||
}
|
|
||||||
// Handle domain search query
|
|
||||||
elseif ($requestPath === '/domains') {
|
|
||||||
if (isset($request->server['query_string'])) {
|
|
||||||
parse_str($request->server['query_string'], $queryParams);
|
|
||||||
|
|
||||||
if (isset($queryParams['name'])) {
|
|
||||||
$searchPattern = $queryParams['name'];
|
|
||||||
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'name');
|
|
||||||
} elseif (isset($queryParams['nsLdhName'])) {
|
|
||||||
$searchPattern = $queryParams['nsLdhName'];
|
|
||||||
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'nsLdhName');
|
|
||||||
} elseif (isset($queryParams['nsIp'])) {
|
|
||||||
$searchPattern = $queryParams['nsIp'];
|
|
||||||
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'nsIp');
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
}
|
||||||
}
|
// Handle entity (contacts) query
|
||||||
// Handle nameserver search query
|
elseif (preg_match('#^/entity/([^/?]+)#', $requestPath, $matches)) {
|
||||||
elseif ($requestPath === '/nameservers') {
|
$entityHandle = $matches[1];
|
||||||
if (isset($request->server['query_string'])) {
|
handleEntityQuery($request, $response, $pdo, $entityHandle, $c, $log);
|
||||||
parse_str($request->server['query_string'], $queryParams);
|
|
||||||
|
|
||||||
if (isset($queryParams['name'])) {
|
|
||||||
$searchPattern = $queryParams['name'];
|
|
||||||
handleNameserverSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'name');
|
|
||||||
} elseif (isset($queryParams['ip'])) {
|
|
||||||
$searchPattern = $queryParams['ip'];
|
|
||||||
handleNameserverSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'ip');
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
}
|
||||||
}
|
// Handle nameserver query
|
||||||
// Handle entity search query
|
elseif (preg_match('#^/nameserver/([^/?]+)#', $requestPath, $matches)) {
|
||||||
elseif ($requestPath === '/entities') {
|
$nameserverHandle = $matches[1];
|
||||||
if (isset($request->server['query_string'])) {
|
handleNameserverQuery($request, $response, $pdo, $nameserverHandle, $c, $log);
|
||||||
parse_str($request->server['query_string'], $queryParams);
|
|
||||||
|
|
||||||
if (isset($queryParams['fn'])) {
|
|
||||||
$searchPattern = $queryParams['fn'];
|
|
||||||
handleEntitySearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'fn');
|
|
||||||
} elseif (isset($queryParams['handle'])) {
|
|
||||||
$searchPattern = $queryParams['handle'];
|
|
||||||
handleEntitySearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'handle');
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$response->header('Content-Type', 'application/json');
|
|
||||||
$response->status(404);
|
|
||||||
$response->end(json_encode(['error' => 'Object not found']));
|
|
||||||
}
|
}
|
||||||
}
|
// Handle domain search query
|
||||||
// Handle help query
|
elseif ($requestPath === '/domains') {
|
||||||
elseif ($requestPath === '/help') {
|
if (isset($request->server['query_string'])) {
|
||||||
handleHelpQuery($request, $response, $pdo, $c);
|
parse_str($request->server['query_string'], $queryParams);
|
||||||
}
|
|
||||||
else {
|
if (isset($queryParams['name'])) {
|
||||||
|
$searchPattern = $queryParams['name'];
|
||||||
|
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'name');
|
||||||
|
} elseif (isset($queryParams['nsLdhName'])) {
|
||||||
|
$searchPattern = $queryParams['nsLdhName'];
|
||||||
|
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'nsLdhName');
|
||||||
|
} elseif (isset($queryParams['nsIp'])) {
|
||||||
|
$searchPattern = $queryParams['nsIp'];
|
||||||
|
handleDomainSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'nsIp');
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle nameserver search query
|
||||||
|
elseif ($requestPath === '/nameservers') {
|
||||||
|
if (isset($request->server['query_string'])) {
|
||||||
|
parse_str($request->server['query_string'], $queryParams);
|
||||||
|
|
||||||
|
if (isset($queryParams['name'])) {
|
||||||
|
$searchPattern = $queryParams['name'];
|
||||||
|
handleNameserverSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'name');
|
||||||
|
} elseif (isset($queryParams['ip'])) {
|
||||||
|
$searchPattern = $queryParams['ip'];
|
||||||
|
handleNameserverSearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'ip');
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle entity search query
|
||||||
|
elseif ($requestPath === '/entities') {
|
||||||
|
if (isset($request->server['query_string'])) {
|
||||||
|
parse_str($request->server['query_string'], $queryParams);
|
||||||
|
|
||||||
|
if (isset($queryParams['fn'])) {
|
||||||
|
$searchPattern = $queryParams['fn'];
|
||||||
|
handleEntitySearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'fn');
|
||||||
|
} elseif (isset($queryParams['handle'])) {
|
||||||
|
$searchPattern = $queryParams['handle'];
|
||||||
|
handleEntitySearchQuery($request, $response, $pdo, $searchPattern, $c, $log, 'handle');
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['error' => 'Object not found']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle help query
|
||||||
|
elseif ($requestPath === '/help') {
|
||||||
|
handleHelpQuery($request, $response, $pdo, $c);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->status(404);
|
||||||
|
$response->end(json_encode(['errorCode' => 404,'title' => 'Not Found','error' => 'Endpoint not found']));
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// Handle database exceptions
|
||||||
|
$log->error('Database error: ' . $e->getMessage());
|
||||||
|
$response->status(500);
|
||||||
$response->header('Content-Type', 'application/json');
|
$response->header('Content-Type', 'application/json');
|
||||||
$response->status(404);
|
$response->end(json_encode(['Database error:' => $e->getMessage()]));
|
||||||
$response->end(json_encode(['errorCode' => 404,'title' => 'Not Found','error' => 'Endpoint not found']));
|
} catch (Throwable $e) {
|
||||||
|
// Catch any other exceptions or errors
|
||||||
|
$log->error('Error: ' . $e->getMessage());
|
||||||
|
$response->status(500);
|
||||||
|
$response->header('Content-Type', 'application/json');
|
||||||
|
$response->end(json_encode(['Error:' => $e->getMessage()]));
|
||||||
|
} finally {
|
||||||
|
// Return the connection to the pool
|
||||||
|
$pool->put($pdo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the connection
|
|
||||||
$pdo = null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
|
|
Loading…
Add table
Reference in a new issue