From 9d8c3b0a3be6f9a7d9ea89b9c4d8840755ccafad Mon Sep 17 00:00:00 2001 From: PuerNya Date: Mon, 18 Dec 2023 23:44:19 +0800 Subject: [PATCH] fix: udp nat handle --- tunnel/tunnel.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 64ae91bf..9891d96f 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -342,29 +342,9 @@ func handleUDPConn(packet C.PacketAdapter) { return false } - if handle() { - packet.Drop() - return - } - - cond, loaded := natTable.GetOrCreateLock(key) - go func() { defer packet.Drop() - if loaded { - cond.L.Lock() - cond.Wait() - handle() - cond.L.Unlock() - return - } - - defer func() { - natTable.DeleteLock(key) - cond.Broadcast() - }() - proxy, rule, err := resolveMetadata(metadata) if err != nil { log.Warnln("[UDP] Parse metadata failed: %s", err.Error()) @@ -381,6 +361,25 @@ func handleUDPConn(packet C.PacketAdapter) { metadata.DstIP = ip } + if handle() { + return + } + + cond, loaded := natTable.GetOrCreateLock(key) + + if loaded { + cond.L.Lock() + cond.Wait() + handle() + cond.L.Unlock() + return + } + + defer func() { + natTable.DeleteLock(key) + cond.Broadcast() + }() + ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout) defer cancel() rawPc, err := retry(ctx, func(ctx context.Context) (C.PacketConn, error) {