diff --git a/pkg/muxer/http/mux_test.go b/pkg/muxer/http/mux_test.go index be8e2de30..0f6649dc7 100644 --- a/pkg/muxer/http/mux_test.go +++ b/pkg/muxer/http/mux_test.go @@ -63,6 +63,20 @@ func Test_addRoute(t *testing.T) { "http://localhost/foo": http.StatusOK, }, }, + { + desc: "Host IPv4", + rule: "Host(`127.0.0.1`)", + expected: map[string]int{ + "http://127.0.0.1/foo": http.StatusOK, + }, + }, + { + desc: "Host IPv6", + rule: "Host(`10::10`)", + expected: map[string]int{ + "http://10::10/foo": http.StatusOK, + }, + }, { desc: "Non-ASCII Host", rule: "Host(`locàlhost`)", diff --git a/pkg/muxer/tcp/mux.go b/pkg/muxer/tcp/mux.go index e21dfc75d..093c182d0 100644 --- a/pkg/muxer/tcp/mux.go +++ b/pkg/muxer/tcp/mux.go @@ -315,7 +315,7 @@ func alpn(tree *matchersTree, protos ...string) error { return nil } -var almostFQDN = regexp.MustCompile(`^[[:alnum:]\.-]+$`) +var hostOrIP = regexp.MustCompile(`^[[:alnum:]\.\-\:]+$`) // hostSNI checks if the SNI Host of the connection match the matcher host. func hostSNI(tree *matchersTree, hosts ...string) error { @@ -329,8 +329,8 @@ func hostSNI(tree *matchersTree, hosts ...string) error { continue } - if !almostFQDN.MatchString(host) { - return fmt.Errorf("invalid value for \"HostSNI\" matcher, %q is not a valid hostname", host) + if !hostOrIP.MatchString(host) { + return fmt.Errorf("invalid value for \"HostSNI\" matcher, %q is not a valid hostname or IP", host) } hosts[i] = strings.ToLower(host) diff --git a/pkg/muxer/tcp/mux_test.go b/pkg/muxer/tcp/mux_test.go index 50b8938cf..f33398ef5 100644 --- a/pkg/muxer/tcp/mux_test.go +++ b/pkg/muxer/tcp/mux_test.go @@ -740,7 +740,7 @@ func Test_HostSNI(t *testing.T) { }, { desc: "Matching hosts", - ruleHosts: []string{"foobar"}, + ruleHosts: []string{"foobar", "foo-bar.baz"}, serverName: "foobar", }, { @@ -748,6 +748,16 @@ func Test_HostSNI(t *testing.T) { ruleHosts: []string{"foo.bar"}, serverName: "foo.bar", }, + { + desc: "Matching IPv4", + ruleHosts: []string{"127.0.0.1"}, + serverName: "127.0.0.1", + }, + { + desc: "Matching IPv6", + ruleHosts: []string{"10::10"}, + serverName: "10::10", + }, } for _, test := range testCases {