Skip to content
Closed
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"net"
"strings"

"github.com/roadrunner-server/http/v6/servers/fcgi"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions config/ssl_address_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
2 changes: 1 addition & 1 deletion servers/https/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion servers/https/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down