fix: mux's udp should add write lock

This commit is contained in:
wwqgtxx
2023-05-11 15:34:28 +08:00
parent 75cd72385a
commit e404695a0d
4 changed files with 29 additions and 19 deletions

View File

@ -2,6 +2,7 @@ package net
import (
"net"
"sync"
"github.com/Dreamacro/clash/common/pool"
)
@ -66,3 +67,22 @@ func waitReadFrom(pc net.PacketConn) (data []byte, put func(), addr net.Addr, er
}
return
}
type threadSafePacketConn struct {
net.PacketConn
access sync.Mutex
}
func (c *threadSafePacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
c.access.Lock()
defer c.access.Unlock()
return c.PacketConn.WriteTo(b, addr)
}
func (c *threadSafePacketConn) Upstream() any {
return c.PacketConn
}
func NewThreadSafePacketConn(pc net.PacketConn) net.PacketConn {
return &threadSafePacketConn{PacketConn: pc}
}