Monolog also for EPP

This commit is contained in:
Pinga 2023-12-01 21:26:48 +02:00
parent e39940afc6
commit 67c5a85bc4
3 changed files with 57 additions and 7 deletions

5
epp/composer.json Normal file
View file

@ -0,0 +1,5 @@
{
"require": {
"monolog/monolog": "^3.5"
}
}

View file

@ -1,5 +1,46 @@
<?php <?php
require_once '../vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
/**
* Sets up and returns a Logger instance.
*
* @param string $logFilePath Full path to the log file.
* @param string $channelName Name of the log channel (optional).
* @return Logger
*/
function setupLogger($logFilePath, $channelName = 'app') {
// Create a log channel
$log = new Logger($channelName);
// Set up the console handler
$consoleHandler = new StreamHandler('php://stdout', Logger::DEBUG);
$consoleFormatter = new LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
"Y-m-d H:i:s.u", // Date format
true, // Allow inline line breaks
true // Ignore empty context and extra
);
$consoleHandler->setFormatter($consoleFormatter);
$log->pushHandler($consoleHandler);
// Set up the file handler
$fileHandler = new RotatingFileHandler($logFilePath, 0, Logger::DEBUG);
$fileFormatter = new LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
"Y-m-d H:i:s.u" // Date format
);
$fileHandler->setFormatter($fileFormatter);
$log->pushHandler($fileHandler);
return $log;
}
function checkLogin($db, $clID, $pw) { function checkLogin($db, $clID, $pw) {
$stmt = $db->prepare("SELECT pw FROM registrar WHERE clid = :username"); $stmt = $db->prepare("SELECT pw FROM registrar WHERE clid = :username");
$stmt->execute(['username' => $clID]); $stmt->execute(['username' => $clID]);

View file

@ -1,6 +1,5 @@
<?php <?php
//require 'vendor/autoload.php';
global $c; global $c;
$c = require_once 'config.php'; $c = require_once 'config.php';
require_once 'src/EppWriter.php'; require_once 'src/EppWriter.php';
@ -14,6 +13,9 @@ require_once 'src/epp-poll.php';
require_once 'src/epp-transfer.php'; require_once 'src/epp-transfer.php';
require_once 'src/epp-delete.php'; require_once 'src/epp-delete.php';
$logFilePath = '/var/log/namingo/epp.log';
$log = setupLogger($logFilePath, 'EPP');
use Swoole\Coroutine\Server; use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection; use Swoole\Coroutine\Server\Connection;
use Swoole\Table; use Swoole\Table;
@ -31,14 +33,14 @@ Swoole\Runtime::enableCoroutine();
$server = new Server($c['epp_host'], $c['epp_port']); $server = new Server($c['epp_host'], $c['epp_port']);
$server->set([ $server->set([
'enable_coroutine' => true, 'enable_coroutine' => true,
'log_file' => '/var/log/namingo/epp.log', 'log_file' => '/var/log/namingo/epp_application.log',
'log_level' => SWOOLE_LOG_INFO, 'log_level' => SWOOLE_LOG_INFO,
'worker_num' => swoole_cpu_num() * 4, 'worker_num' => swoole_cpu_num() * 4,
'pid_file' => $c['epp_pid'], 'pid_file' => $c['epp_pid'],
'tcp_user_timeout' => 10, 'tcp_user_timeout' => 10,
'max_request' => 1000, 'max_request' => 1000,
'open_tcp_nodelay' => true, 'open_tcp_nodelay' => true,
'max_conn' => 10000, 'max_conn' => 1024,
'heartbeat_check_interval' => 60, 'heartbeat_check_interval' => 60,
'heartbeat_idle_time' => 120, 'heartbeat_idle_time' => 120,
'open_ssl' => true, 'open_ssl' => true,
@ -49,9 +51,10 @@ $server->set([
'ssl_protocols' => SWOOLE_SSL_TLSv1_2 | SWOOLE_SSL_TLSv1_3, 'ssl_protocols' => SWOOLE_SSL_TLSv1_2 | SWOOLE_SSL_TLSv1_3,
'ssl_ciphers' => 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES256-GCM-SHA384', 'ssl_ciphers' => 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES256-GCM-SHA384',
]); ]);
$log->info('server started.');
$server->handle(function (Connection $conn) use ($table, $db, $c) { $server->handle(function (Connection $conn) use ($table, $db, $c, $log) {
echo "Client connected.\n"; $log->info('new client connected');
sendGreeting($conn); sendGreeting($conn);
while (true) { while (true) {
@ -494,10 +497,11 @@ $server->handle(function (Connection $conn) use ($table, $db, $c) {
} }
sendEppError($conn, $db, 2100, 'Unknown command'); sendEppError($conn, $db, 2100, 'Unknown command');
echo "Client disconnected.\n"; $log->info('client disconnected');
}); });
echo "Namingo EPP server started.\n"; $log->info('Namingo EPP server started');
Swoole\Coroutine::create(function () use ($server) { Swoole\Coroutine::create(function () use ($server) {
$server->start(); $server->start();
}); });