From f748812771b9e7da75a9ecb5d871ea674a7a9898 Mon Sep 17 00:00:00 2001
From: vnxme <46669194+vnxme@users.noreply.github.com>
Date: Fri, 25 Oct 2024 22:03:19 +0300
Subject: [PATCH] Fix tls alpn matcher

The context may have no replacer
---
 modules/l4tls/alpn_matcher.go | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/modules/l4tls/alpn_matcher.go b/modules/l4tls/alpn_matcher.go
index e5ea95f..71f1ef0 100644
--- a/modules/l4tls/alpn_matcher.go
+++ b/modules/l4tls/alpn_matcher.go
@@ -37,7 +37,14 @@ func (*MatchALPN) CaddyModule() caddy.ModuleInfo {
 }
 
 func (m *MatchALPN) Match(hello *tls.ClientHelloInfo) bool {
-	repl := hello.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
+	repl := caddy.NewReplacer()
+	if ctx := hello.Context(); ctx != nil {
+		// In some situations the existing context may have no replacer
+		if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
+			repl = replAny.(*caddy.Replacer)
+		}
+	}
+
 	clientProtocols := hello.SupportedProtos
 	for _, alpn := range *m {
 		alpn = repl.ReplaceAll(alpn, "")