-
Notifications
You must be signed in to change notification settings - Fork 14
Add the possibility to bind ServeCommand to all interfaces
#871
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
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,20 @@ | |
|
|
||
| final class ServeCommand implements CommandInterface | ||
| { | ||
| /** | ||
| * @var list<string> List of loopback hosts | ||
| */ | ||
| private const array LOOPBACK_HOSTS = ['localhost', '127.0.0.1', '::1']; | ||
|
|
||
| /** | ||
| * @var list<string> List of wildcard hosts | ||
| */ | ||
| private const array WILDCARD_HOSTS = ['0.0.0.0', '::']; | ||
|
|
||
| /** | ||
| * Host to bind the server to | ||
| */ | ||
| private string $host = '127.0.0.1'; | ||
| private string $host = 'localhost'; | ||
|
|
||
| /** | ||
| * Port to bind the server to | ||
|
|
@@ -66,7 +76,7 @@ public function __invoke(?array $argv = null): never | |
| $this->climate->arguments->add([ | ||
| 'host' => [ | ||
| 'longPrefix' => 'host', | ||
| 'description' => 'Host to bind the server to', | ||
| 'description' => 'Host to bind the server to (if the value is omitted, the server will bind to all interfaces)', | ||
| 'defaultValue' => $this->host, | ||
| ], | ||
| 'port' => [ | ||
|
|
@@ -89,15 +99,18 @@ public function __invoke(?array $argv = null): never | |
| ], | ||
| ]); | ||
|
|
||
| $this->climate->arguments->parse(); | ||
| // Ignore parsing errors to handle passing `--host` without a value as a flag to bind to all interfaces | ||
| @$this->climate->arguments->parse(); | ||
|
|
||
| if ($this->climate->arguments->get('help')) { | ||
| $this->climate->usage($argv); | ||
| exit(0); | ||
| } | ||
|
|
||
| /** @var string */ | ||
| $host = $this->climate->arguments->get('host'); | ||
| $host = $this->climate->arguments->defined('host') | ||
| ? ($this->climate->arguments->get('host') ?: '0.0.0.0') // Bind to all interfaces if `--host` is passed without a value | ||
| : $this->host; | ||
|
|
||
| /** @var int */ | ||
| $port = $this->climate->arguments->get('port'); | ||
|
|
@@ -164,6 +177,14 @@ private function handleOutput(string $type, array $lines): void | |
| $this->climate->br(); | ||
| $this->climate->out(sprintf('➜ Listening on <cyan>http://%s:<bold>%s</bold>/</cyan>', $this->formatHost($this->host), $this->port)); | ||
| $this->climate->br(); | ||
|
|
||
| if (in_array($this->host, self::WILDCARD_HOSTS, true)) { | ||
| foreach ($this->getLocalNetworkIps() as $localNetworkIp) { | ||
| $this->climate->out(sprintf('➜ Remote address: <cyan>http://%s:<bold>%s</bold>/</cyan>', $this->formatHost($localNetworkIp), $this->port)); | ||
| $this->climate->br(); | ||
| } | ||
| } | ||
|
|
||
| $this->climate->out('<dark_gray>Press <bold>CTRL+C</bold> to stop</dark_gray>'); | ||
| $this->climate->br(); | ||
| break; | ||
|
|
@@ -336,4 +357,34 @@ private function outputRawLine(string $type, string $line): void | |
| throw new UnexpectedValueException(sprintf('Unexpected output type "%s"', $type)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get local network IP addresses, excluding loopback interfaces | ||
| * | ||
| * @return list<string> | ||
| */ | ||
| private function getLocalNetworkIps(): array | ||
| { | ||
| if (($interfaces = net_get_interfaces()) === false) { | ||
| return []; | ||
| } | ||
|
|
||
| $localNetworkIps = []; | ||
|
|
||
| foreach ($interfaces as $interface) { | ||
| if (!isset($interface['unicast'])) { | ||
| continue; | ||
| } | ||
| foreach ($interface['unicast'] as $data) { | ||
| if ( | ||
| $data['family'] === AF_INET // IPv4 addresses only | ||
| && !in_array($data['address'], self::LOOPBACK_HOSTS, true) // Exclude loopback address | ||
| ) { | ||
|
Comment on lines
+368
to
+382
|
||
| $localNetworkIps[] = $data['address']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $localNetworkIps; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@error suppression won’t preventLeague\CLImate\Arguments::parse()from throwingInvalidArgumentException(as other commands already expect/catch), so--hostpassed without a value will likely still crash parsing. Also,parse()is called without$argv, meaning__invoke($argv)won’t be honored consistently. Consider passing$argvtoparse()and handling the specific “missing value for --host” case viatry/catch(or by pre-processing$argvto rewrite bare--hostinto--host=0.0.0.0), while still surfacing other parse errors to the user.