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) {