mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user