From c49cd118d7969300867ab47b6b89f360626e77e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 20 Mar 2024 10:46:54 +0800 Subject: [PATCH] Fix WireGuard client bind --- transport/wireguard/client_bind.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/transport/wireguard/client_bind.go b/transport/wireguard/client_bind.go index d39d1bece0..b23d851ad5 100644 --- a/transport/wireguard/client_bind.go +++ b/transport/wireguard/client_bind.go @@ -88,8 +88,7 @@ func (c *ClientBind) connect() (*wireConn, error) { func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) { select { case <-c.done: - err = net.ErrClosed - return + c.done = make(chan struct{}) default: } return []conn.ReceiveFunc{c.receive}, 0, nil @@ -165,7 +164,7 @@ func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint) error { } copy(b[1:4], reserved[:]) } - _, err = udpConn.WriteTo(b, M.SocksaddrFromNetIP(destination)) + _, err = udpConn.WriteToUDPAddrPort(b, destination) if err != nil { udpConn.Close() return err @@ -192,10 +191,18 @@ func (c *ClientBind) SetReservedForEndpoint(destination netip.AddrPort, reserved type wireConn struct { net.PacketConn + conn net.Conn access sync.Mutex 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 { w.access.Lock() defer w.access.Unlock()