Skip to content

Allow to define the IP on which server:start should listen to #525

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

Merged
merged 1 commit into from
Oct 11, 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
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ COPY symfony /usr/local/bin/

FROM scratch

ENV SYMFONY_ALLOW_ALL_IP=true

ENTRYPOINT ["/usr/local/bin/symfony"]

COPY --from=build . .
2 changes: 2 additions & 0 deletions commands/local_server_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ var localServerStartCmd = &console.Command{
&console.StringFlag{Name: "document-root", Usage: "Project document root (auto-configured by default)"},
&console.StringFlag{Name: "passthru", Usage: "Project passthru index (auto-configured by default)"},
&console.IntFlag{Name: "port", DefaultValue: 8000, Usage: "Preferred HTTP port"},
&console.StringFlag{Name: "listen-ip", DefaultValue: "127.0.0.1", Usage: "The IP on which the CLI should listen"},
&console.BoolFlag{Name: "allow-all-ip", Usage: "Listen on all the available interfaces"},
&console.BoolFlag{Name: "daemon", Aliases: []string{"d"}, Usage: "Run the server in the background"},
&console.BoolFlag{Name: "no-humanize", Usage: "Do not format JSON logs"},
&console.StringFlag{Name: "p12", Usage: "Name of the file containing the TLS certificate to use in p12 format"},
Expand Down
3 changes: 2 additions & 1 deletion local/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Server struct {
Callback ServerCallback
Port int
PreferredPort int
ListenIp string
PKCS12 string
AllowHTTP bool
Logger zerolog.Logger
Expand Down Expand Up @@ -79,7 +80,7 @@ var gzipContentTypes = []string{

// Start starts the server
func (s *Server) Start(errChan chan error) (int, error) {
ln, port, err := process.CreateListener(s.Port, s.PreferredPort)
ln, port, err := process.CreateListener(s.ListenIp, s.Port, s.PreferredPort)
if err != nil {
return port, errors.WithStack(err)
}
Expand Down
14 changes: 10 additions & 4 deletions local/process/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
package process

import (
"fmt"
"net"
"strconv"

"github.com/pkg/errors"
)

// CreateListener creates a listener on a port
// Pass a preferred port (will increment by 1 if port is not available)
// or pass 0 to auto-find any available port
func CreateListener(port, preferredPort int) (net.Listener, int, error) {
func CreateListener(listenIp string, port, preferredPort int) (net.Listener, int, error) {
var ln net.Listener
var err error
tryPort := preferredPort
Expand All @@ -39,9 +39,15 @@ func CreateListener(port, preferredPort int) (net.Listener, int, error) {
max = 1
}
for {
ln, err = net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(tryPort))
// we really want to test availability on 127.0.0.1
ln, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", tryPort))
if err == nil {
break
ln.Close()
// but then, we want to listen to as many local IP's as possible
ln, err = net.Listen("tcp", fmt.Sprintf("%s:%d", listenIp, tryPort))
if err == nil {
break
}
}
if port > 0 {
return nil, 0, errors.Wrapf(err, "unable to listen on port %d", port)
Expand Down
6 changes: 6 additions & 0 deletions local/project/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const DockerComposeWorkerKey = "docker_compose"
type Config struct {
HomeDir string
ProjectDir string
ListenIp string
DocumentRoot string `yaml:"document_root"`
Passthru string `yaml:"passthru"`
Port int `yaml:"port"`
Expand Down Expand Up @@ -83,6 +84,11 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
}
config.AppVersion = c.App.Version
config.ProjectDir = projectDir
if c.IsSet("allow-all-ip") {
config.ListenIp = ""
} else {
config.ListenIp = c.String("listen-ip")
}
if c.IsSet("document-root") {
config.DocumentRoot = c.String("document-root")
}
Expand Down
1 change: 1 addition & 0 deletions local/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func New(c *Config) (*Project, error) {
DocumentRoot: documentRoot,
Port: c.Port,
PreferredPort: c.PreferredPort,
ListenIp: c.ListenIp,
Logger: c.Logger,
PKCS12: c.PKCS12,
AllowHTTP: c.AllowHTTP,
Expand Down