From 8284a0ef8f1ee3774982cb6e53f53848401fa3a5 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 31 Mar 2025 10:09:33 +0000 Subject: [PATCH] Dialer: Do not use ListenSystemPacket() when dialing UDP https://github.com/XTLS/Xray-core/pull/4530#issuecomment-2765744961 --- transport/internet/system_dialer.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index 0b3c2f10..63365099 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -59,7 +59,17 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne Port: 0, } } - packetConn, err := ListenSystemPacket(ctx, srcAddr, sockopt) + var lc net.ListenConfig + lc.Control = func(network, address string, c syscall.RawConn) error { + return c.Control(func(fd uintptr) { + if sockopt != nil { + if err := applyOutboundSocketOptions(network, "", fd, sockopt); err != nil { + errors.LogInfo(ctx, err, "failed to apply socket options") + } + } + }) + } + packetConn, err := lc.ListenPacket(ctx, srcAddr.Network(), srcAddr.String()) if err != nil { return nil, err } @@ -67,23 +77,6 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne if err != nil { return nil, err } - if sockopt != nil { - sys, err := packetConn.(*net.UDPConn).SyscallConn() - if err != nil { - return nil, err - } - sys.Control(func(fd uintptr) { - var network string - if destAddr.IP.To4() != nil { - network = "udp4" - } else { - network = "udp6" - } - if err := applyOutboundSocketOptions(network, dest.NetAddr(), fd, sockopt); err != nil { - errors.LogInfo(ctx, err, "failed to apply socket options") - } - }) - } return &PacketConnWrapper{ Conn: packetConn, Dest: destAddr,