Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to run the program listening on multiple ports #54

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading