@marcel91200 / caddy Mirror · read-only / commits / 7b8f3505
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 03:15 UTC

caddytls: Fix sni_regexp matcher to obtain layer4 contexts (#6804)

* caddytls: Fix sni_regexp matcher * caddytls: Refactor sni_regexp matcher

vnxme committed Jan 25, 2025 at 17:45 UTC 7b8f3505e33139de0d542566478e98b361bb84bf
1 file changed +17 -3
modules/caddytls/matchers.go
+17 -3
@@ -15,6 +15,7 @@
15 package caddytls
16
17 import (
18 + "context"
19 "crypto/tls"
20 "fmt"
21 "net"
@@ -224,15 +225,28 @@ func (MatchServerNameRE) CaddyModule() caddy.ModuleInfo {
225
226 // Match matches hello based on SNI using a regular expression.
227 func (m MatchServerNameRE) Match(hello *tls.ClientHelloInfo) bool {
227 - repl := caddy.NewReplacer()
228 - // caddytls.TestServerNameMatcher calls this function without any context
229 - if ctx := hello.Context(); ctx != nil {
228 + // Note: caddytls.TestServerNameMatcher calls this function without any context
229 + ctx := hello.Context()
230 + if ctx == nil {
231 + // layer4.Connection implements GetContext() to pass its context here,
232 + // since hello.Context() returns nil
233 + if mayHaveContext, ok := hello.Conn.(interface{ GetContext() context.Context }); ok {
234 + ctx = mayHaveContext.GetContext()
235 + }
236 + }
237 +
238 + var repl *caddy.Replacer
239 + if ctx != nil {
240 // In some situations the existing context may have no replacer
241 if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
242 repl = replAny.(*caddy.Replacer)
243 }
244 }
245
246 + if repl == nil {
247 + repl = caddy.NewReplacer()
248 + }
249 +
250 return m.MatchRegexp.Match(hello.ServerName, repl)
251 }
252