Skip to content

Commit

Permalink
Inject TLS options directly to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Nov 3, 2021
1 parent b3b3007 commit d8a9988
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 46 deletions.
4 changes: 2 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ services:
- '%gos_web_socket.server.host%'
- '%gos_web_socket.server.port%'
- '@gos_web_socket.registry.server'
- '%gos_web_socket.server.tls.enabled%'
- '%gos_web_socket.server.tls.options%'
tags:
- { name: console.command, command: gos:websocket:server }

Expand Down Expand Up @@ -376,6 +374,8 @@ services:
- '@gos_web_socket.server.builder'
- '@gos_web_socket.server.event_loop'
- '@event_dispatcher'
- '%gos_web_socket.server.tls.enabled%'
- '%gos_web_socket.server.tls.options%'
calls:
- [ setLogger, ['@logger'] ]
tags:
Expand Down
16 changes: 2 additions & 14 deletions src/Command/WebsocketServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ final class WebsocketServerCommand extends Command

private int $port;

private bool $tlsEnabled;

private array $tlsOptions;

private ?ServerRegistry $serverRegistry;

public function __construct(
ServerLauncherInterface $entryPoint,
string $host,
int $port,
?ServerRegistry $serverRegistry = null,
bool $tlsEnabled,
array $tlsOptions)
public function __construct(ServerLauncherInterface $entryPoint, string $host, int $port, ?ServerRegistry $serverRegistry = null)
{
parent::__construct();

Expand All @@ -49,8 +39,6 @@ public function __construct(
$this->serverLauncher = $entryPoint;
$this->port = $port;
$this->host = $host;
$this->tlsEnabled = $tlsEnabled;
$this->tlsOptions = $tlsOptions;
$this->serverRegistry = $serverRegistry;
}

Expand Down Expand Up @@ -82,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var bool $profile */
$profile = $input->getOption('profile');

$this->serverLauncher->launch($name, $host, (int) $port, $profile, $this->tlsEnabled, $this->tlsOptions);
$this->serverLauncher->launch($name, $host, (int) $port, $profile);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ private function addServerSection(ArrayNodeDefinition $rootNode): void
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->info('Enables native tls support.')
->info('Enables native TLS support.')
->defaultFalse()
->end()
->variableNode('options')
->info('An array of options for tls context. See https://www.php.net/manual/en/context.ssl.php for available options.')
->info('An array of options for the TLS context, see https://www.php.net/manual/en/context.ssl.php for available options.')
->defaultValue([])
->end()
->end()
Expand Down
4 changes: 2 additions & 2 deletions src/Server/ServerLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(ServerRegistry $serverRegistry)
* @throws \InvalidArgumentException if the given server name is not registered
* @throws \RuntimeException if there are no servers registered to launch
*/
public function launch(?string $serverName, string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []): void
public function launch(?string $serverName, string $host, int $port, bool $profile): void
{
if (null === $serverName) {
$servers = $this->serverRegistry->getServers();
Expand All @@ -39,6 +39,6 @@ public function launch(?string $serverName, string $host, int $port, bool $profi
$server = $this->serverRegistry->getServer($serverName);
}

$server->launch($host, $port, $profile, $tlsEnabled, $tlsOptions);
$server->launch($host, $port, $profile);
}
}
2 changes: 1 addition & 1 deletion src/Server/ServerLauncherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface ServerLauncherInterface
{
public function launch(?string $serverName, string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []): void;
public function launch(?string $serverName, string $host, int $port, bool $profile): void;
}
2 changes: 1 addition & 1 deletion src/Server/Type/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface ServerInterface
{
public function launch(string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []);
public function launch(string $host, int $port, bool $profile);

public function getName(): string;
}
15 changes: 11 additions & 4 deletions src/Server/Type/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,33 @@ final class WebSocketServer implements ServerInterface, LoggerAwareInterface
private ServerBuilderInterface $serverBuilder;
private LoopInterface $loop;
private EventDispatcherInterface $eventDispatcher;
private bool $tlsEnabled;
private array $tlsOptions;

public function __construct(
ServerBuilderInterface $serverBuilder,
LoopInterface $loop,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
bool $tlsEnabled = false,
array $tlsOptions = []
) {
$this->serverBuilder = $serverBuilder;
$this->loop = $loop;
$this->eventDispatcher = $eventDispatcher;
$this->tlsEnabled = $tlsEnabled;
$this->tlsOptions = $tlsOptions;
}

public function launch(string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []): void
public function launch(string $host, int $port, bool $profile): void
{
if (null !== $this->logger) {
$this->logger->info('Starting web socket');
}

$server = new SocketServer("$host:$port", [], $this->loop);
if ($tlsEnabled) {
$server = new SecureServer($server, $this->loop, $tlsOptions);

if ($this->tlsEnabled) {
$server = new SecureServer($server, $this->loop, $this->tlsOptions);
}

$app = new IoServer(
Expand Down
8 changes: 4 additions & 4 deletions tests/Command/WebsocketServerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testCommandLaunchesWebSocketServer(): void
->method('launch')
->with(null, 'localhost', 1337, false);

$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, new ServerRegistry(), false, []);
$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, new ServerRegistry());

$commandTester = new CommandTester($command);
$commandTester->execute([]);
Expand All @@ -33,9 +33,9 @@ public function testCommandLaunchesWebSocketServerWithConsoleArgumentsAndOptions
$entryPoint = $this->createMock(ServerLauncherInterface::class);
$entryPoint->expects(self::once())
->method('launch')
->with('websocket', 'web.socket', 8443, true, true, ['verify_peer' => false]);
->with('websocket', 'web.socket', 8443, true);

$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, new ServerRegistry(), true, ['verify_peer' => false]);
$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, new ServerRegistry());

$commandTester = new CommandTester($command);
$commandTester->execute(
Expand Down Expand Up @@ -77,7 +77,7 @@ public function testCommandAutocompletion(array $input, array $suggestions): voi
/** @var MockObject&ServerLauncherInterface $entryPoint */
$entryPoint = $this->createMock(ServerLauncherInterface::class);

$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, $registry, false, []);
$command = new WebsocketServerCommand($entryPoint, 'localhost', 1337, $registry);

$tester = new CommandCompletionTester($command);

Expand Down
4 changes: 2 additions & 2 deletions tests/Server/App/Registry/ServerRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void
public function testServersAreAddedToTheRegistry(): void
{
$server = new class() implements ServerInterface {
public function launch(string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []): void
public function launch(string $host, int $port, bool $profile): void
{
// no-op
}
Expand All @@ -47,7 +47,7 @@ public function testRetrievingAServerFailsIfTheNamedServerDoesNotExist(): void
$this->expectExceptionMessage('A server named "main" has not been registered.');

$server = new class() implements ServerInterface {
public function launch(string $host, int $port, bool $profile, bool $tlsEnabled = false, array $tlsOptions = []): void
public function launch(string $host, int $port, bool $profile): void
{
// no-op
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Server/ServerLauncherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testTheFirstServerIsLaunchedWhenNoNameIsGiven(): void

$server->expects(self::once())
->method('launch')
->with($host, $port, $profile, false, []);
->with($host, $port, $profile);

$this->serverRegistry->addServer($server);

Expand All @@ -66,7 +66,7 @@ public function testTheNamedServerIsLaunched(): void

$server->expects(self::once())
->method('launch')
->with($host, $port, $profile, false, []);
->with($host, $port, $profile);

$this->serverRegistry->addServer($server);

Expand Down
31 changes: 19 additions & 12 deletions tests/Server/Type/WebSocketServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,13 @@ class WebSocketServerTest extends TestCase
*/
private $eventDispatcher;

/**
* @var WebSocketServer
*/
private $server;

protected function setUp(): void
{
parent::setUp();

$this->serverBuilder = $this->createMock(ServerBuilderInterface::class);
$this->loop = $this->createMock(LoopInterface::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);

$this->server = new WebSocketServer(
$this->serverBuilder,
$this->loop,
$this->eventDispatcher
);
}

public function testTheServerIsLaunched(): void
Expand All @@ -62,6 +51,24 @@ public function testTheServerIsLaunched(): void
$this->loop->expects(self::once())
->method('run');

$this->server->launch('127.0.0.1', 1337, false);
(new WebSocketServer($this->serverBuilder, $this->loop, $this->eventDispatcher))
->launch('127.0.0.1', 1337, false);
}

public function testTheServerIsLaunchedWithTlsSupport(): void
{
$this->serverBuilder->expects(self::once())
->method('buildMessageStack')
->willReturn($this->createMock(MessageComponentInterface::class));

$this->eventDispatcher->expects(self::once())
->method('dispatch')
->with(self::isInstanceOf(ServerLaunchedEvent::class), GosWebSocketEvents::SERVER_LAUNCHED);

$this->loop->expects(self::once())
->method('run');

(new WebSocketServer($this->serverBuilder, $this->loop, $this->eventDispatcher, true, ['verify_peer' => false]))
->launch('127.0.0.1', 1337, false);
}
}

0 comments on commit d8a9988

Please sign in to comment.