From 3778a367c8aa5c07162b4003c672945a6296c169 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:58:50 -0500 Subject: [PATCH] Fix and simplify SockOpt in UDP https://github.com/golang/go/issues/29277 --- transport/internet/system_dialer.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index 924f364c..3c9239e8 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -2,7 +2,6 @@ package internet import ( "context" - "runtime" "syscall" "time" @@ -67,17 +66,16 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne if err != nil { return nil, err } - if runtime.GOOS != "windows" && (sockopt != nil || len(d.controllers) > 0) { - file, err := packetConn.(*net.UDPConn).File() + if sockopt != nil { + sys, err := packetConn.(*net.UDPConn).SyscallConn() if err != nil { return nil, err } - fd := file.Fd() - if sockopt != nil { + sys.Control(func(fd uintptr) { if err := applyOutboundSocketOptions("udp", dest.NetAddr(), fd, sockopt); err != nil { newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx)) } - } + }) } return &PacketConnWrapper{ Conn: packetConn,