Fix crash on udpnat2 handler

This commit is contained in:
世界 2025-03-24 18:06:52 +08:00
parent ce1b4851a4
commit 23b0180a1b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 8 additions and 17 deletions
common/udpnat2

View file

@ -28,6 +28,7 @@ type natConn struct {
cache freelru.Cache[netip.AddrPort, *natConn]
writer N.PacketWriter
localAddr M.Socksaddr
handlerAccess sync.RWMutex
handler N.UDPHandlerEx
packetChan chan *N.PacketBuffer
closeOnce sync.Once
@ -75,23 +76,10 @@ func (c *natConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr,
}
func (c *natConn) SetHandler(handler N.UDPHandlerEx) {
select {
case <-c.doneChan:
default:
}
c.handlerAccess.Lock()
defer c.handlerAccess.Unlock()
c.handler = handler
c.readWaitOptions = N.NewReadWaitOptions(c.writer, handler)
fetch:
for {
select {
case packet := <-c.packetChan:
c.handler.NewPacketEx(packet.Buffer, packet.Destination)
N.PutPacketBuffer(packet)
continue fetch
default:
break fetch
}
}
}
func (c *natConn) Timeout() time.Duration {

View file

@ -74,8 +74,11 @@ func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destinati
for _, bufferSlice := range bufferSlices {
buffer.Write(bufferSlice)
}
if conn.handler != nil {
conn.handler.NewPacketEx(buffer, destination)
conn.handlerAccess.RLock()
handler := conn.handler
conn.handlerAccess.RUnlock()
if handler != nil {
handler.NewPacketEx(buffer, destination)
return
}
packet := N.NewPacketBuffer()