diff --git a/src/Server/AccessLoggerMiddleware.php b/src/Server/AccessLoggerMiddleware.php new file mode 100644 index 0000000..a31194d --- /dev/null +++ b/src/Server/AccessLoggerMiddleware.php @@ -0,0 +1,79 @@ +getMethod(); + $uri = (string)$request->getUri(); + $protocolVersion = $request->getProtocolVersion(); + $remote = Server::getClientIp($request); + + $context = [ + 'method' => $method, + 'uri' => $uri, + 'client' => $remote, + ]; + + try { + $response = $requestHandler->handleRequest($request); + } catch (\Throwable $exception) { + $this->logger->warning( + \sprintf( + 'Client exception for "%s %s" HTTP/%s %s', + $method, + $uri, + $protocolVersion, + $remote + ), + $context + ); + + throw $exception; + } + + $status = $response->getStatus(); + $reason = $response->getReason(); + + $context = [ + 'request' => $context, + 'response' => [ + 'status' => $status, + 'reason' => $reason, + ], + ]; + + $level = $status < 400 ? LogLevel::INFO : LogLevel::NOTICE; + + $this->logger->log( + $level, + \sprintf( + '"%s %s" %d "%s" HTTP/%s %s', + $method, + $uri, + $status, + $reason, + $protocolVersion, + $remote + ), + $context + ); + + return $response; + } +} diff --git a/src/Server/Router.php b/src/Server/Router.php index e3b03da..7e96fb1 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -47,11 +47,14 @@ class Router private function setRoutes(): void { - $authorization = new Authorization(); - $apiHandler = stackMiddleware(ApiController::getRouterCallback(ApiExtensions::class), $authorization); - $systemApiHandler = stackMiddleware(SystemController::getRouterCallback(SystemApiExtensions::class), $authorization); - $eventsHandler = stackMiddleware(EventsController::getRouterCallback($this->server), $authorization); - $logHandler = stackMiddleware(LogController::getRouterCallback($this->server), $authorization); + $middlewares = [ + new AccessLoggerMiddleware(Logger::getInstance()), + new Authorization() + ]; + $apiHandler = stackMiddleware(ApiController::getRouterCallback(ApiExtensions::class), ...$middlewares); + $systemApiHandler = stackMiddleware(SystemController::getRouterCallback(SystemApiExtensions::class), ...$middlewares); + $eventsHandler = stackMiddleware(EventsController::getRouterCallback($this->server), ...$middlewares); + $logHandler = stackMiddleware(LogController::getRouterCallback($this->server), ...$middlewares); foreach (['GET', 'POST'] as $method) { $this->router->addRoute($method, '/api/{method}[/]', $apiHandler); diff --git a/src/Server/Server.php b/src/Server/Server.php index 1e63e69..30ec555 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -6,6 +6,7 @@ use Amp; use Amp\Http\Server\DefaultErrorHandler; use Amp\Http\Server\Driver\ConnectionLimitingServerSocketFactory; use Amp\Http\Server\Driver\DefaultHttpDriverFactory; +use Amp\Http\Server\Request; use Amp\Http\Server\SocketHttpServer; use Amp\Socket\InternetAddress; use Amp\Sync\LocalSemaphore; @@ -89,4 +90,18 @@ class Server return $config; } + public static function getClientIp(Request $request): string + { + $remote = $request->getClient()->getRemoteAddress()->toString(); + $hostArray = explode(':', $remote); + if (count($hostArray) >= 2) { + $port = (int)array_pop($hostArray); + if ($port > 0 && $port <= 65353) { + $remote = implode(':', $hostArray); + } + } + + return $remote; + } + } \ No newline at end of file