mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
Fix crash on udpnat2 handler
This commit is contained in:
parent
ce1b4851a4
commit
23b0180a1b
2 changed files with 8 additions and 17 deletions
common/udpnat2
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue