mirror of
https://github.com/caddyserver/caddy.git
synced 2025-03-09 07:29:03 -04:00
caddytls: Fix sni_regexp matcher to obtain layer4 contexts (#6804)
* caddytls: Fix sni_regexp matcher * caddytls: Refactor sni_regexp matcher
This commit is contained in:
parent
30743c361a
commit
7b8f3505e3
@ -15,6 +15,7 @@
|
|||||||
package caddytls
|
package caddytls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@ -224,15 +225,28 @@ func (MatchServerNameRE) CaddyModule() caddy.ModuleInfo {
|
|||||||
|
|
||||||
// Match matches hello based on SNI using a regular expression.
|
// Match matches hello based on SNI using a regular expression.
|
||||||
func (m MatchServerNameRE) Match(hello *tls.ClientHelloInfo) bool {
|
func (m MatchServerNameRE) Match(hello *tls.ClientHelloInfo) bool {
|
||||||
repl := caddy.NewReplacer()
|
// Note: caddytls.TestServerNameMatcher calls this function without any context
|
||||||
// caddytls.TestServerNameMatcher calls this function without any context
|
ctx := hello.Context()
|
||||||
if ctx := hello.Context(); ctx != nil {
|
if ctx == nil {
|
||||||
|
// layer4.Connection implements GetContext() to pass its context here,
|
||||||
|
// since hello.Context() returns nil
|
||||||
|
if mayHaveContext, ok := hello.Conn.(interface{ GetContext() context.Context }); ok {
|
||||||
|
ctx = mayHaveContext.GetContext()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var repl *caddy.Replacer
|
||||||
|
if ctx != nil {
|
||||||
// In some situations the existing context may have no replacer
|
// In some situations the existing context may have no replacer
|
||||||
if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
|
if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
|
||||||
repl = replAny.(*caddy.Replacer)
|
repl = replAny.(*caddy.Replacer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if repl == nil {
|
||||||
|
repl = caddy.NewReplacer()
|
||||||
|
}
|
||||||
|
|
||||||
return m.MatchRegexp.Match(hello.ServerName, repl)
|
return m.MatchRegexp.Match(hello.ServerName, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user