diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-03-20 15:51:37 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-03-20 15:51:37 -0600 |
commit | 3c1def243020a3897121d4c5badf07ed45d2397d (patch) | |
tree | 01be16286024a88043a4575962e03924bded36b6 /modules/caddytls/matchers.go | |
parent | b583007c49975c5c368630af30bbe3b116935df0 (diff) |
caddytls: Support wildcard matching in ServerName conn policy matcher
Diffstat (limited to 'modules/caddytls/matchers.go')
-rw-r--r-- | modules/caddytls/matchers.go | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/modules/caddytls/matchers.go b/modules/caddytls/matchers.go index 9e2dfc5..1f5f9b6 100644 --- a/modules/caddytls/matchers.go +++ b/modules/caddytls/matchers.go @@ -16,6 +16,7 @@ package caddytls import ( "crypto/tls" + "strings" "github.com/caddyserver/caddy/v2" ) @@ -24,7 +25,9 @@ func init() { caddy.RegisterModule(MatchServerName{}) } -// MatchServerName matches based on SNI. +// MatchServerName matches based on SNI. Names in +// this list may use left-most-label wildcards, +// similar to wildcard certificates. type MatchServerName []string // CaddyModule returns the Caddy module information. @@ -38,10 +41,23 @@ func (MatchServerName) CaddyModule() caddy.ModuleInfo { // Match matches hello based on SNI. func (m MatchServerName) Match(hello *tls.ClientHelloInfo) bool { for _, name := range m { - // TODO: support wildcards (and regex?) if hello.ServerName == name { return true } + + // check for wildcard match on this name, but only + // bother if there is even a wildcard character + if !strings.Contains(name, "*") { + continue + } + labels := strings.Split(hello.ServerName, ".") + for i := range labels { + labels[i] = "*" + candidate := strings.Join(labels, ".") + if candidate == name { + return true + } + } } return false } |