From a8bef6b0510e0613f28cfe306766f916dc3148a5 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 22 Apr 2024 16:32:15 +0400 Subject: [PATCH 1/2] Make the PORT parameter a list --- src/Command/Run.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Command/Run.php b/src/Command/Run.php index 4cbc91ba..97552474 100644 --- a/src/Command/Run.php +++ b/src/Command/Run.php @@ -32,7 +32,13 @@ final class Run extends Command implements SignalableCommandInterface public function configure(): void { - $this->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to listen', 9912); + $this->addOption( + 'port', + 'p', + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Port to listen', + [9912], + ); $this->addOption( 'sender', 's', @@ -52,14 +58,25 @@ protected function execute( $output->writeln(\sprintf('%s v%s', Info::NAME, Info::VERSION)); $output->write(Info::LOGO_CLI_COLOR . "\n", true, OutputInterface::OUTPUT_RAW); - $port = (int)$input->getOption('port') ?: 9912; + /** + * Prepare port listeners + * @var SocketServer[] $servers + */ + $servers = []; + foreach ($input->getOption('port') ?: [9912] as $port) { + \is_numeric($port) or throw new \InvalidArgumentException( + \sprintf('Invalid port `%s`. It must be a number.', $port), + ); + $servers[] = new SocketServer((int)$port); + } + /** @var non-empty-string[] $senders */ $senders = (array)$input->getOption('sender'); $registry = $this->createRegistry($output); $this->app = new Application( - [new SocketServer($port)], + $servers, new Logger($output), senders: $registry->getSenders($senders), withFrontend: $input->getOption('ui') !== false, From 8ff8358e2ffac266567fdb4bd6c3e8e669733e41 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 22 Apr 2024 16:51:22 +0400 Subject: [PATCH 2/2] Fix psalm issues, update docs --- README.md | 15 +++++++++++---- src/Command/Run.php | 13 ++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 80e5e56e..f006d738 100644 --- a/README.md +++ b/README.md @@ -141,13 +141,20 @@ $responder->respond(trap($response)->return()); ### Default port -By default, the Trap server operates on port `9912`. However, if you wish to utilize a different port, you can easily -make this adjustment using the `-p` option. +Trap automatically recognizes the type of traffic. +Therefore, there is no need to open separate ports for different protocols. +By default, it operates on port `9912`. +However, if you wish to utilize a different port, you can easily make this adjustment using the `-p` option: -For example, to switch to port 8000, you would use the following command: +```bash +vendor/bin/trap -p8000 +``` + +Sometimes, it's convenient to run Trap on the same ports that [Buggregator](https://github.com/buggregator/server) +uses by default. Well, that's also possible: ```bash -vendor/bin/trap -p 8000 +vendor/bin/trap -p1025 -p9912 -p9913 -p8000 ``` ### Choosing Your Senders diff --git a/src/Command/Run.php b/src/Command/Run.php index 97552474..e10fe6eb 100644 --- a/src/Command/Run.php +++ b/src/Command/Run.php @@ -63,11 +63,18 @@ protected function execute( * @var SocketServer[] $servers */ $servers = []; - foreach ($input->getOption('port') ?: [9912] as $port) { + $ports = $input->getOption('port') ?: [9912]; + \assert(\is_array($ports)); + foreach ($ports as $port) { + \assert(\is_scalar($port)); \is_numeric($port) or throw new \InvalidArgumentException( - \sprintf('Invalid port `%s`. It must be a number.', $port), + \sprintf('Invalid port `%s`. It must be a number.', (string)$port), ); - $servers[] = new SocketServer((int)$port); + $port = (int)$port; + $port > 0 && $port < 65536 or throw new \InvalidArgumentException( + \sprintf('Invalid port `%s`. It must be in range 1-65535.', $port), + ); + $servers[] = new SocketServer($port); } /** @var non-empty-string[] $senders */