8000 Merge pull request #525 from tucksaun/feat/listen-all-ip · symfony-cli/symfony-cli@12013a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12013a9

Browse files
authored
Merge pull request #525 from tucksaun/feat/listen-all-ip
Allow to define the IP on which `server:start` should listen to
2 parents 799c80f + 298dbb2 commit 12013a9

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ COPY symfony /usr/local/bin/
66

77
FROM scratch
88

9+
ENV SYMFONY_ALLOW_ALL_IP=true
10+
911
ENTRYPOINT ["/usr/local/bin/symfony"]
1012

1113
COPY --from=build . .

commands/local_server_start.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ var localServerStartCmd = &console.Command{
6565
&console.StringFlag{Name: "document-root", Usage: "Project document root (auto-configured by default)"},
6666
&console.StringFlag{Name: "passthru", Usage: "Project passthru index (auto-configured by default)"},
6767
&console.IntFlag{Name: "port", DefaultValue: 8000, Usage: "Preferred HTTP port"},
68+
&console.StringFlag{Name: "listen-ip", DefaultValue: "127.0.0.1", Usage: "The IP on which the CLI should listen"},
69+
&console.BoolFlag{Name: "allow-all-ip", Usage: "Listen on all the available interfaces"},
6870
&console.BoolFlag{Name: "daemon", Aliases: []string{"d"}, Usage: "Run the server in the background"},
6971
&console.BoolFlag{Name: "no-humanize", Usage: "Do not format JSON logs"},
7072
&console.StringFlag{Name: "p12", Usage: "Name of the file containing the TLS certificate to use in p12 format"},

local/http/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Server struct {
4949
Callback ServerCallback
5050
Port int
5151
PreferredPort int
52+
ListenIp string
5253
PKCS12 string
5354
AllowHTTP bool
5455
Logger zerolog.Logger
@@ -79,7 +80,7 @@ var gzipContentTypes = []string{
7980

8081
// Start starts the server
8182
func (s *Server) Start(errChan chan error) (int, error) {
82-
ln, port, err := process.CreateListener(s.Port, s.PreferredPort)
83+
ln, port, err := process.CreateListener(s.ListenIp, s.Port, s.PreferredPort)
8384
if err != nil {
8485
return port, errors.WithStack(err)
8586
}

local/process/listener.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
package process
2121

2222
import (
23+
"fmt"
2324
"net"
24-
"strconv"
2525

2626
"github.com/pkg/errors"
2727
)
2828

2929
// CreateListener creates a listener on a port
3030
// Pass a preferred port (will increment by 1 if port is not available)
3131
// or pass 0 to auto-find any available port
32-
func CreateListener(port, preferredPort int) (net.Listener, int, error) {
32+
func CreateListener(listenIp string, port, preferredPort int) (net.Listener, int, error) {
3333
var ln net.Listener
3434
var err error
3535
tryPort := preferredPort
@@ -39,9 +39,15 @@ func CreateListener(port, preferredPort int) (net.Listener, int, error) {
3939
max = 1
4040
}
4141
for {
42-
ln, err = net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(tryPort))
42+
// we really want to test availability on 127.0.0.1
43+
ln, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", tryPort))
4344
if err == nil {
44-
break
45+
ln.Close()
46+
// but then, we want to listen to as many local IP's as possible
47+
ln, err = net.Listen("tcp", fmt.Sprintf("%s:%d", listenIp, tryPort))
10000 48+
if err == nil {
49+
break
50+
}
4551
}
4652
if port > 0 {
4753
return nil, 0, errors.Wrapf(err, "unable to listen on port %d", port)

local/project/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const DockerComposeWorkerKey = "docker_compose"
3535
type Config struct {
3636
HomeDir string
3737
ProjectDir string
38+
ListenIp string
3839
DocumentRoot string `yaml:"document_root"`
3940
Passthru string `yaml:"passthru"`
4041
Port int `yaml:"port"`
@@ -83,6 +84,11 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
8384
}
8485
config.AppVersion = c.App.Version
8586
config.ProjectDir = projectDir
87+
if c.IsSet("allow-all-ip") {
88+
config.ListenIp = ""
89+
} else {
90+
config.ListenIp = c.String("listen-ip")
91+
}
8692
if c.IsSet("document-root") {
8793
config.DocumentRoot = c.String("document-root")
8894
}

local/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func New(c *Config) (*Project, error) {
5656
DocumentRoot: documentRoot,
5757
Port: c.Port,
5858
PreferredPort: c.PreferredPort,
59+
ListenIp: c.ListenIp,
5960
Logger: c.Logger,
6061
PKCS12: c.PKCS12,
6162
AllowHTTP: c.AllowHTTP,

0 commit comments

Comments
 (0)
0