mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 00:21:30 +00:00
Fix WireGuard client bind
This commit is contained in:
parent
59d437b9d2
commit
f61b272cbf
|
@ -36,6 +36,7 @@ func NewClientBind(ctx context.Context, errorHandler E.Handler, dialer N.Dialer,
|
||||||
errorHandler: errorHandler,
|
errorHandler: errorHandler,
|
||||||
dialer: dialer,
|
dialer: dialer,
|
||||||
reservedForEndpoint: make(map[netip.AddrPort][3]uint8),
|
reservedForEndpoint: make(map[netip.AddrPort][3]uint8),
|
||||||
|
done: make(chan struct{}),
|
||||||
isConnect: isConnect,
|
isConnect: isConnect,
|
||||||
connectAddr: connectAddr,
|
connectAddr: connectAddr,
|
||||||
reserved: reserved,
|
reserved: reserved,
|
||||||
|
@ -88,8 +89,7 @@ func (c *ClientBind) connect() (*wireConn, error) {
|
||||||
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
||||||
select {
|
select {
|
||||||
case <-c.done:
|
case <-c.done:
|
||||||
err = net.ErrClosed
|
c.done = make(chan struct{})
|
||||||
return
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
return []conn.ReceiveFunc{c.receive}, 0, nil
|
return []conn.ReceiveFunc{c.receive}, 0, nil
|
||||||
|
@ -129,16 +129,8 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClientBind) Reset() {
|
|
||||||
common.Close(common.PtrOrNil(c.conn))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ClientBind) Close() error {
|
func (c *ClientBind) Close() error {
|
||||||
common.Close(common.PtrOrNil(c.conn))
|
common.Close(common.PtrOrNil(c.conn))
|
||||||
if c.done == nil {
|
|
||||||
c.done = make(chan struct{})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
select {
|
select {
|
||||||
case <-c.done:
|
case <-c.done:
|
||||||
default:
|
default:
|
||||||
|
@ -165,7 +157,7 @@ func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint) error {
|
||||||
}
|
}
|
||||||
copy(b[1:4], reserved[:])
|
copy(b[1:4], reserved[:])
|
||||||
}
|
}
|
||||||
_, err = udpConn.WriteTo(b, M.SocksaddrFromNetIP(destination))
|
_, err = udpConn.WriteToUDPAddrPort(b, destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
udpConn.Close()
|
udpConn.Close()
|
||||||
return err
|
return err
|
||||||
|
@ -192,10 +184,18 @@ func (c *ClientBind) SetReservedForEndpoint(destination netip.AddrPort, reserved
|
||||||
|
|
||||||
type wireConn struct {
|
type wireConn struct {
|
||||||
net.PacketConn
|
net.PacketConn
|
||||||
|
conn net.Conn
|
||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *wireConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
|
||||||
|
if w.conn != nil {
|
||||||
|
return w.conn.Write(b)
|
||||||
|
}
|
||||||
|
return w.PacketConn.WriteTo(b, M.SocksaddrFromNetIP(addr).UDPAddr())
|
||||||
|
}
|
||||||
|
|
||||||
func (w *wireConn) Close() error {
|
func (w *wireConn) Close() error {
|
||||||
w.access.Lock()
|
w.access.Lock()
|
||||||
defer w.access.Unlock()
|
defer w.access.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue