From b730b82ef789bc07f381d8b403c3b34ff93ae859 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 13 Mar 2024 01:28:14 +0100 Subject: [PATCH] More tests, misc fixes --- src/Configuration.php | 4 ++ src/Daemon.php | 17 ++++--- src/Http/Middleware/ApiHandler.php | 5 ++ src/Http/Middleware/ResponseMiddleware.php | 7 ++- src/Http/Server.php | 8 ++-- tests/ConfigurationTest.php | 48 +++++++++++++++++++ tests/Http/Middleware/NotFoundHandlerTest.php | 29 +++++++++++ 7 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 tests/ConfigurationTest.php create mode 100644 tests/Http/Middleware/NotFoundHandlerTest.php diff --git a/src/Configuration.php b/src/Configuration.php index 33931ed..b54d859 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -8,6 +8,10 @@ class Configuration { private array $config = []; + public function __construct(array $config=[]) + { + $this->config = $config; + } public static function createDefault(): Configuration { diff --git a/src/Daemon.php b/src/Daemon.php index 5c4ca16..a17abad 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -11,6 +11,7 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Socket\SecureServer; use React\Socket\SocketServer; +use React\Socket\TcpServer; class Daemon { @@ -20,10 +21,11 @@ class Daemon private Server $server; - private LoggerInterface $logger; + private Logger $logger; public function __construct(Configuration $config, bool $verbose=false, ?LoopInterface $loop=null) { + if (!defined("MERCUREACT_VERSION")) define("MERCUREACT_VERSION", "0.0.0"); $this->config = $config; $this->loop = $loop??Loop::get(); $this->logger = $this->createLogger($verbose); @@ -42,7 +44,7 @@ class Daemon { $this->server = new Server($this->config, $this->logger, $this->loop); - $this->logger->info("NoccyLabs Mercureact Daemon v".MERCUREACT_VERSION); + $this->logger->info("Starting NoccyLabs Mercureact Daemon v".MERCUREACT_VERSION." (".MERCUREACT_BUILDTIME.")"); $listenAddress = $this->config->getListenAddress(); if (!$listenAddress) { @@ -53,16 +55,19 @@ class Daemon $certificate = $this->config->get("server.encryption.local_cert"); if ($certificate) { + $this->logger->warning("SSL/TLS suport is broken."); $this->logger->info("Using local cert: {$certificate}"); - $context = [ - 'tls' => $this->config->get("server.encryption.") - ]; + $context = $this->config->get("server.encryption."); $uri = 'tls://' . $listenAddress; + $context['crypto_method'] = STREAM_CRYPTO_METHOD_TLSv1_3_SERVER; + $socket = new TcpServer($listenAddress); + $socket = new SecureServer($socket, null, $context??[]); } else { $uri = 'tcp://' . $listenAddress; + $context = []; + $socket = new SocketServer($uri); } - $socket = new SocketServer($uri, $context??[]); $this->server->listen($socket); } diff --git a/src/Http/Middleware/ApiHandler.php b/src/Http/Middleware/ApiHandler.php index 6bc5f52..5cf56a4 100644 --- a/src/Http/Middleware/ApiHandler.php +++ b/src/Http/Middleware/ApiHandler.php @@ -32,6 +32,11 @@ class ApiHandler return new Promise( function (callable $resolve, callable $reject) use ($next, $request) { + if (!$this->config->get('server.enable_api', true)) { + $resolve($next($request)); + return; + } + $path = $request->getUri()->getPath(); // FIXME remove this when done debugging diff --git a/src/Http/Middleware/ResponseMiddleware.php b/src/Http/Middleware/ResponseMiddleware.php index e9a6116..addf9ce 100644 --- a/src/Http/Middleware/ResponseMiddleware.php +++ b/src/Http/Middleware/ResponseMiddleware.php @@ -75,11 +75,14 @@ class ResponseMiddleware 'method' => $request->getMethod(), 'path' => $request->getUri()->getPath(), ]); + return $response // ->withAddedHeader('Link', '; rel="mercure"') // ->withAddedHeader('Link', '; rel="mercure+ws"') - ->withHeader('Access-Control-Allow-Origin', '*') - ->withHeader('Content-Security-Policy', "default-src * 'self' http: 'unsafe-eval' 'unsafe-inline'; connect-src * 'self'") + ->withHeader('Access-Control-Allow-Origin', + $this->config->get('server.cors.allow_origin', '*')) + ->withHeader('Content-Security-Policy', + $this->config->get('server.cors.csp', "default-src * 'self' http: 'unsafe-eval' 'unsafe-inline'; connect-src * 'self'")) ->withHeader('Cache-Control', 'must-revalidate') ->withHeader('Server', 'Mercureact/0.1.0'); } diff --git a/src/Http/Server.php b/src/Http/Server.php index 6687c8e..117015b 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -32,7 +32,7 @@ class Server private SplObjectStorage $webSocketClients; private TopicManager $topicManager; - + private Logger $logger; private ResponseMiddleware $responseMiddleware; @@ -47,13 +47,13 @@ class Server * * */ - public function __construct(Configuration $config, ?LoggerInterface $logger, ?LoopInterface $loop=null) + public function __construct(Configuration $config, ?Logger $logger, ?LoopInterface $loop=null) { $this->loop = $loop??Loop::get(); $this->config = $config; - $this->logger = $logger ?? new NullLogger(); + $this->logger = $logger ?? new Logger("main"); if ($logger instanceof Logger) { $topicLogger = $logger->withName("broker"); } else { @@ -89,7 +89,7 @@ class Server * @return HttpServer */ private function createHttpServer(): HttpServer - { + { $stack = [ $this->responseMiddleware = new ResponseMiddleware( config: $this->config, diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php new file mode 100644 index 0000000..7c2372e --- /dev/null +++ b/tests/ConfigurationTest.php @@ -0,0 +1,48 @@ +assertInstanceOf(Configuration::class, $config); + } + + public function testGettingSingleValues() + { + $config = new Configuration([ + 'foo.bar' => 'baz', + 'foo.baz' => true, + 'foo.bin' => 42, + ]); + + $this->assertEquals('baz', $config->get("foo.bar")); + $this->assertEquals(true, $config->get("foo.baz")); + $this->assertEquals(42, $config->get("foo.bin")); + } + + public function testGettingMultipleValues() + { + $config = new Configuration([ + 'foo.bar' => 'baz', + 'foo.baz' => true, + 'foo.bin' => 42, + 'bar' => false, + ]); + + $this->assertEquals([ + 'foo.bar' => 'baz', + 'foo.baz' => true, + 'foo.bin' => 42, + ], $config->get("foo.")); + $this->assertEquals(false, $config->get("bar")); + $this->assertEquals([], $config->get("bar.")); + } + +} \ No newline at end of file diff --git a/tests/Http/Middleware/NotFoundHandlerTest.php b/tests/Http/Middleware/NotFoundHandlerTest.php new file mode 100644 index 0000000..72821f4 --- /dev/null +++ b/tests/Http/Middleware/NotFoundHandlerTest.php @@ -0,0 +1,29 @@ +__invoke($request)->then(function ($r) use (&$result) { + $result = $r; + }); + + $this->assertInstanceOf(ResponseInterface::class, $result); + /** @var ResponseInterface $result */ + $this->assertEquals(404, $result->getStatusCode()); + } + +} \ No newline at end of file