From f6f94d8de84ca80048657af39bcf795683c78a30 Mon Sep 17 00:00:00 2001 From: hunshcn Date: Tue, 2 Jan 2024 13:37:01 +0800 Subject: [PATCH] add peek mutex --- listener/http/proxy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/listener/http/proxy.go b/listener/http/proxy.go index 807d1748..4822eabc 100644 --- a/listener/http/proxy.go +++ b/listener/http/proxy.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "strings" + "sync" _ "unsafe" "github.com/metacubex/mihomo/adapter/inbound" @@ -28,6 +29,7 @@ func HandleConn(c net.Conn, tunnel C.Tunnel, cache *lru.LruCache[string, bool], defer client.CloseIdleConnections() ctx, cancel := context.WithCancel(context.Background()) defer cancel() + peekMutex := sync.Mutex{} conn := N.NewBufferedConn(c) @@ -35,7 +37,9 @@ func HandleConn(c net.Conn, tunnel C.Tunnel, cache *lru.LruCache[string, bool], trusted := cache == nil // disable authenticate if lru is nil for keepAlive { + peekMutex.Lock() request, err := ReadRequest(conn.Reader()) + peekMutex.Unlock() if err != nil { break } @@ -87,6 +91,8 @@ func HandleConn(c net.Conn, tunnel C.Tunnel, cache *lru.LruCache[string, bool], startBackgroundRead := func() { go func() { + peekMutex.Lock() + defer peekMutex.Unlock() _, err := conn.Peek(1) if err != nil { cancel()