diff --git a/README.md b/README.md index d8502e9..0a157f4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ http: trusted_proxies: ["10.20.0.0/24"] read_header_timeout: 5s ssl: - address: 0.0.0.0:8443 + address: :8443 cert: server.pem key: server-key.pem proxy_protocol: diff --git a/config/config.go b/config/config.go index a921a45..029d8fa 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config import ( + "net" "strings" "github.com/roadrunner-server/http/v6/servers/fcgi" @@ -107,6 +108,7 @@ func (c *Config) InitDefaults() error { } if c.SSLConfig != nil { + c.SSLConfig.Address = InheritHost(c.Address, c.SSLConfig.Address) err := c.SSLConfig.InitDefaults() if err != nil { return err @@ -125,6 +127,25 @@ func (c *Config) InitDefaults() error { return c.Valid() } +// InheritHost ensures an SSL listener without an explicit host binds to the same address as the parent. +func InheritHost(httpAddr, sslAddr string) string { + if sslAddr == "" { + sslAddr = ":443" + } + if !strings.HasPrefix(sslAddr, ":") { + return sslAddr + } + httpAddr = strings.TrimPrefix(httpAddr, "tcp://") + if strings.Contains(httpAddr, "://") { + return sslAddr + } + host, _, err := net.SplitHostPort(httpAddr) + if err != nil || host == "" { + return sslAddr + } + return net.JoinHostPort(host, sslAddr[1:]) +} + // Valid validates the configuration. func (c *Config) Valid() error { const op = errors.Op("validation") diff --git a/config/ssl_address_test.go b/config/ssl_address_test.go new file mode 100644 index 0000000..3ca6f99 --- /dev/null +++ b/config/ssl_address_test.go @@ -0,0 +1,22 @@ +package config + +import "testing" + +func TestInheritHost(t *testing.T) { + tests := []struct{ http, ssl, want string }{ + {"[::]:8080", ":8443", "[::]:8443"}, + {"tcp://[::1]:8080", ":8443", "[::1]:8443"}, + {"0.0.0.0:8080", ":8443", "0.0.0.0:8443"}, + {"localhost:8080", ":8443", "localhost:8443"}, + {"[::]:8080", "", "[::]:443"}, + {":8080", ":8443", ":8443"}, + {":8080", "", ":443"}, + {"[::]:8080", "127.0.0.1:8443", "127.0.0.1:8443"}, + {"unix:///tmp/rr.sock", ":8443", ":8443"}, + } + for _, tt := range tests { + if got := InheritHost(tt.http, tt.ssl); got != tt.want { + t.Errorf("InheritHost(%q, %q) = %q, want %q", tt.http, tt.ssl, got, tt.want) + } + } +} diff --git a/servers/https/config.go b/servers/https/config.go index 3f8be07..c6cbcc8 100644 --- a/servers/https/config.go +++ b/servers/https/config.go @@ -86,7 +86,7 @@ func (s *SSL) InitDefaults() error { } if s.Address == "" { - s.Address = "127.0.0.1:443" + s.Address = ":443" } if s.ProxyProtocol != nil { diff --git a/servers/https/config_test.go b/servers/https/config_test.go index 6ea4e00..2181568 100644 --- a/servers/https/config_test.go +++ b/servers/https/config_test.go @@ -189,7 +189,7 @@ func TestHTTP2_InitDefaults(t *testing.T) { func TestSSL_InitDefaultsAddress(t *testing.T) { empty := &SSL{} require.NoError(t, empty.InitDefaults()) - assert.Equal(t, "127.0.0.1:443", empty.Address) + assert.Equal(t, ":443", empty.Address) explicit := &SSL{Address: "0.0.0.0:8443"} require.NoError(t, explicit.InitDefaults())