From fe7d9661e4d8a8403c46c76aa5ba09ba1b63693f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 25 Nov 2026 20:10:05 +0800 Subject: [PATCH] Fix WireGuard detour bind dropping packets from other peer addresses A single peer with a fixed address was dialed through the detour as a connected UDP socket, so the kernel discarded authenticated packets the peer sent from any other address, e.g. a server answering from another of its addresses. Listen instead, keeping the peer address as the destination for the detour. --- transport/wireguard/client_bind.go | 33 +++++++++--------------------- transport/wireguard/endpoint.go | 8 +++----- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/transport/wireguard/client_bind.go b/transport/wireguard/client_bind.go index 7afc4458..fa9d3825 100644 --- a/transport/wireguard/client_bind.go +++ b/transport/wireguard/client_bind.go @@ -30,20 +30,18 @@ type ClientBind struct { connAccess sync.Mutex conn atomic.Pointer[wireConn] done chan struct{} - isConnect bool - connectAddr netip.AddrPort + destination M.Socksaddr reserved [3]uint8 } -func NewClientBind(ctx context.Context, logger logger.Logger, dialer N.Dialer, isConnect bool, connectAddr netip.AddrPort, reserved [3]uint8) *ClientBind { +func NewClientBind(ctx context.Context, logger logger.Logger, dialer N.Dialer, destination M.Socksaddr, reserved [3]uint8) *ClientBind { return &ClientBind{ ctx: ctx, logger: logger, dialer: dialer, reservedForEndpoint: make(map[netip.AddrPort][3]uint8), done: make(chan struct{}), - isConnect: isConnect, - connectAddr: connectAddr, + destination: destination, reserved: reserved, } } @@ -72,24 +70,13 @@ func (c *ClientBind) connect() (*wireConn, error) { return serverConn, nil } } - if c.isConnect { - udpConn, err := c.dialer.DialContext(c.bindCtx, N.NetworkUDP, M.SocksaddrFromNetIP(c.connectAddr)) - if err != nil { - return nil, err - } - serverConn = &wireConn{ - PacketConn: bufio.NewUnbindPacketConn(udpConn), - done: make(chan struct{}), - } - } else { - udpConn, err := c.dialer.ListenPacket(c.bindCtx, M.Socksaddr{Addr: netip.IPv4Unspecified()}) - if err != nil { - return nil, err - } - serverConn = &wireConn{ - PacketConn: bufio.NewPacketConn(udpConn), - done: make(chan struct{}), - } + udpConn, err := c.dialer.ListenPacket(c.bindCtx, c.destination) + if err != nil { + return nil, err + } + serverConn = &wireConn{ + PacketConn: bufio.NewPacketConn(udpConn), + done: make(chan struct{}), } c.conn.Store(serverConn) return serverConn, nil diff --git a/transport/wireguard/endpoint.go b/transport/wireguard/endpoint.go index 407bf0f4..bc1ea648 100644 --- a/transport/wireguard/endpoint.go +++ b/transport/wireguard/endpoint.go @@ -173,18 +173,16 @@ func (e *Endpoint) Start(postStart bool) error { bind = standardBind } else { var ( - isConnect bool - connectAddr netip.AddrPort + destination = M.Socksaddr{Addr: netip.IPv4Unspecified()} reserved [3]uint8 ) if len(e.peers) == 1 { reserved = e.peers[0].reserved if e.peers[0].endpoint.IsValid() { - isConnect = true - connectAddr = e.peers[0].endpoint + destination = M.SocksaddrFromNetIP(e.peers[0].endpoint) } } - bind = NewClientBind(e.options.Context, e.options.Logger, e.options.Dialer, isConnect, connectAddr, reserved) + bind = NewClientBind(e.options.Context, e.options.Logger, e.options.Dialer, destination, reserved) } if isUDPListener || len(e.peers) > 1 { for _, peer := range e.peers {