Skip to content

Commit

Permalink
Merge pull request #54: Added the ability to run the program listenin…
Browse files Browse the repository at this point in the history
…g on multiple ports
  • Loading branch information
roxblnfk committed Apr 22, 2024
2 parents aa66a15 + 8ff8358 commit 6bfa73a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -52,14 +58,32 @@ protected function execute(
$output->writeln(\sprintf('<fg=yellow;options=bold>%s</> <info>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 = [];
$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.', (string)$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 */
$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,
Expand Down

0 comments on commit 6bfa73a

Please sign in to comment.