From 600ee0ed1aecc8140fae5ecc8d30234a81d9c2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Sat, 6 Jul 2024 06:12:12 +0000 Subject: [PATCH] Add prefix match --- proxy/vless/inbound/inbound.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/proxy/vless/inbound/inbound.go b/proxy/vless/inbound/inbound.go index e8372463..e8affe09 100644 --- a/proxy/vless/inbound/inbound.go +++ b/proxy/vless/inbound/inbound.go @@ -3,7 +3,6 @@ package inbound //go:generate go run github.com/xtls/xray-core/common/errors/errorgen import ( - "bufio" "bytes" "context" gotls "crypto/tls" @@ -313,10 +312,11 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s h2path := extractPathFromH2Request(connection) if h2path != "" { path = h2path + errors.LogInfo(ctx, "realPath = "+path) } } } - fb := pfb[path] + fb := prefixMatch(pfb, path) if fb == nil { return errors.New(`failed to find the default "path" config`).AtWarning() } @@ -595,8 +595,7 @@ func (h *Handler) Process(ctx context.Context, network net.Network, connection s // Get path form http2 func extractPathFromH2Request(conn stat.Connection) string { - reader := bufio.NewReader(conn) - framer := http2.NewFramer(conn, reader) + framer := http2.NewFramer(io.Discard, conn) for { frame, err := framer.ReadFrame() @@ -626,3 +625,21 @@ func extractPathFromH2Request(conn stat.Connection) string { } } } + +func prefixMatch(pfb map[string]*Fallback, path string) *Fallback { + for { + if val, exists := pfb[path]; exists { + return val + } + if path == "/" { + break + } + path = strings.TrimSuffix(path, "/") + if idx := strings.LastIndex(path, "/"); idx != -1 { + path = path[:idx] + } else { + break + } + } + return nil +}