diff --git a/cp/app/Lib/Logger.php b/cp/app/Lib/Logger.php index 2e3c587..849a823 100644 --- a/cp/app/Lib/Logger.php +++ b/cp/app/Lib/Logger.php @@ -6,6 +6,7 @@ use Monolog\ErrorHandler; use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\StreamHandler; use Monolog\Formatter\LineFormatter; +use Monolog\Formatter\HtmlFormatter; use Monolog\Handler\FilterHandler; use Monolog\Handler\WhatFailureGroupHandler; use MonologPHPMailer\PHPMailerHandler; @@ -68,9 +69,6 @@ class Logger extends \Monolog\Logger $fileHandler->setFormatter($fileFormatter); $this->pushHandler($fileHandler); - // Archive Old Logs (Move older than 14 days to ZIP) - $this->archiveOldLogs($config['logFile']); - // Pushover Alerts (For CRITICAL, ALERT, EMERGENCY) if (!empty($_ENV['PUSHOVER_KEY'])) { try { @@ -90,14 +88,14 @@ class Logger extends \Monolog\Logger $mail->SMTPAuth = true; $mail->Username = $_ENV['MAIL_USERNAME']; $mail->Password = $_ENV['MAIL_PASSWORD']; - $mail->SMTPSecure = $_ENV['MAIL_ENCRYPTION']; + $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = $_ENV['MAIL_PORT']; $mail->setFrom($_ENV['MAIL_FROM_ADDRESS'], $_ENV['MAIL_FROM_NAME']); - $mail->addAddress($_ENV['MAIL_FROM_ADDRESS']); // Send to admin email + $mail->addAddress($_ENV['MAIL_TO_ADDRESS']); // Send to admin email // Attach PHPMailer to Monolog $mailerHandler = new PHPMailerHandler($mail); - $mailerHandler->setFormatter(new LineFormatter()); + $mailerHandler->setFormatter(new HtmlFormatter()); // Filter Emails to ALERT, CRITICAL, EMERGENCY Only $filteredMailHandler = new FilterHandler($mailerHandler, \Monolog\Logger::ALERT, \Monolog\Logger::EMERGENCY); @@ -147,47 +145,4 @@ class Logger extends \Monolog\Logger $run->register(); } - /** - * Archive Old Logs (Older than 14 Days) - */ - private function archiveOldLogs($logFilePath) - { - $logDir = dirname($logFilePath); - $backupDir = '/opt/backup'; - $lockFile = $backupDir . '/log_archive.lock'; - - // Prevent multiple processes from running archive at the same time - if (file_exists($lockFile)) { - return; // Another process is already archiving - } - touch($lockFile); // Create lock file - - if (!is_dir($backupDir)) { - mkdir($backupDir, 0755, true); - } - - $logFiles = glob($logDir . '/*.log'); // Get all log files - $thresholdDate = strtotime('-14 days'); // Logs older than 14 days - - foreach ($logFiles as $file) { - if (filemtime($file) < $thresholdDate) { - $filename = basename($file); - $monthYear = date('F-Y', filemtime($file)); - $zipPath = $backupDir . "/logs-{$monthYear}.zip"; - - // Open or create ZIP archive - $zip = new ZipArchive(); - if ($zip->open($zipPath, ZipArchive::CREATE) === true) { - if (!$zip->locateName($filename)) { // Prevent duplicate addition - $zip->addFile($file, $filename); - unlink($file); // Delete original log after archiving - } - $zip->close(); - } - } - } - - unlink($lockFile); // Remove lock when done - } - } \ No newline at end of file diff --git a/cp/bootstrap/database.php b/cp/bootstrap/database.php index 9e34d90..4e79140 100644 --- a/cp/bootstrap/database.php +++ b/cp/bootstrap/database.php @@ -1,28 +1,31 @@ "{$config['mysql']['driver']}:dbname={$config['mysql']['database']};host={$config['mysql']['host']};charset={$config['mysql']['charset']}", + 'sqlite' => "{$config['sqlite']['driver']}:{$config['sqlite']['driver']}", + 'pgsql' => "{$config['pgsql']['driver']}:dbname={$config['pgsql']['database']};host={$config['pgsql']['host']}" +]; + +$pdo = null; +$db = null; + +try { + // Select the correct database driver (fallback to MySQL) + $driver = $supportedDrivers[$defaultDriver] ?? $supportedDrivers['mysql']; + + // Create PDO connection + $pdo = new \PDO($driver, $config[$defaultDriver]['username'], $config[$defaultDriver]['password']); + $db = PdoDatabase::fromPdo($pdo); + +} catch (PDOException $e) { + $log->alert("Database connection failed: " . $e->getMessage(), ['driver' => $defaultDriver]); } \ No newline at end of file diff --git a/das/start_das.php b/das/start_das.php index df08a1b..5f5dd9f 100644 --- a/das/start_das.php +++ b/das/start_das.php @@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) { // Register a callback to handle incoming requests $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) { // Get a PDO connection from the pool - $pdo = $pool->get(); + try { + $pdo = $pool->get(); + if (!$pdo) { + throw new PDOException("Failed to retrieve a connection from Swoole PDOPool."); + } + } catch (PDOException $e) { + $log->alert("Swoole PDO Pool failed: " . $e->getMessage()); + $server->send($fd, "Database failure. Please try again later"); + $server->close($fd); + return; + } $domain = trim($data); $clientInfo = $server->getClientInfo($fd); diff --git a/epp/start_epp.php b/epp/start_epp.php index fb81f41..a73171a 100644 --- a/epp/start_epp.php +++ b/epp/start_epp.php @@ -95,11 +95,14 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi $log->info('new client from ' . $clientIP . ' connected'); sendGreeting($conn); - + while (true) { try { // Get a PDO connection from the pool $pdo = $pool->get(); + if (!$pdo) { + throw new PDOException("Failed to retrieve a connection from Swoole PDOPool."); + } $data = $conn->recv(); $connId = spl_object_id($conn); @@ -572,7 +575,7 @@ $server->handle(function (Connection $conn) use ($table, $pool, $c, $log, $permi } } catch (PDOException $e) { // Handle database exceptions - $log->error('Database error: ' . $e->getMessage()); + $log->alert('Database error: ' . $e->getMessage()); // Here we only retry if it's a *connection* failure. // Common MySQL connection error codes: 2002 (Can't connect), 2006 (Gone away), 2013 (Lost connection). diff --git a/rdap/start_rdap.php b/rdap/start_rdap.php index 7b1b3fd..a14df29 100644 --- a/rdap/start_rdap.php +++ b/rdap/start_rdap.php @@ -52,7 +52,17 @@ $log->info('server started.'); // Handle incoming HTTP requests $http->on('request', function ($request, $response) use ($c, $pool, $log, $rateLimiter) { // Get a PDO connection from the pool - $pdo = $pool->get(); + try { + $pdo = $pool->get(); + if (!$pdo) { + throw new PDOException("Failed to retrieve a connection from Swoole PDOPool."); + } + } catch (PDOException $e) { + $log->alert("Swoole PDO Pool failed: " . $e->getMessage()); + $response->header('Content-Type', 'application/json'); + $response->status(500); + $response->end(json_encode(['error' => 'Database failure. Please try again later.'])); + } $remoteAddr = $request->server['remote_addr']; if (!isIpWhitelisted($remoteAddr, $pdo)) { diff --git a/whois/port43/start_whois.php b/whois/port43/start_whois.php index dd3859e..a7e14cc 100644 --- a/whois/port43/start_whois.php +++ b/whois/port43/start_whois.php @@ -59,7 +59,17 @@ $server->on('connect', function ($server, $fd) use ($log) { // Register a callback to handle incoming requests $server->on('receive', function ($server, $fd, $reactorId, $data) use ($c, $pool, $log, $rateLimiter) { // Get a PDO connection from the pool - $pdo = $pool->get(); + try { + $pdo = $pool->get(); + if (!$pdo) { + throw new PDOException("Failed to retrieve a connection from Swoole PDOPool."); + } + } catch (PDOException $e) { + $log->alert("Swoole PDO Pool failed: " . $e->getMessage()); + $server->send($fd, "Database failure. Please try again later"); + $server->close($fd); + return; + } $privacy = $c['privacy']; $minimum_data = $c['minimum_data']; $parsedQuery = parseQuery($data);