From 2abeda9c4222ad7b2a300cdd8d46c2b256896178 Mon Sep 17 00:00:00 2001 From: xqzr <34030394+xqzr@users.noreply.github.com> Date: Mon, 31 Mar 2025 20:13:57 +0800 Subject: [PATCH] Sockopt: Allow listen `v6only` work for Windows & Darwin (#4571) Completes https://github.com/XTLS/Xray-core/pull/1677 --------- Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com> --- transport/internet/sockopt_darwin.go | 6 ++++++ transport/internet/sockopt_windows.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/transport/internet/sockopt_darwin.go b/transport/internet/sockopt_darwin.go index 86af492a..f684de98 100644 --- a/transport/internet/sockopt_darwin.go +++ b/transport/internet/sockopt_darwin.go @@ -200,6 +200,12 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } } + if config.V6Only { + if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_V6ONLY, 1); err != nil { + return errors.New("failed to set IPV6_V6ONLY").Base(err) + } + } + return nil } diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index e1c1d25e..cbd3b41e 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -15,6 +15,7 @@ const ( IPV6_UNICAST_IF = 31 IP_MULTICAST_IF = 9 IPV6_MULTICAST_IF = 9 + IPV6_V6ONLY = 27 ) func setTFO(fd syscall.Handle, tfo int) error { @@ -90,6 +91,12 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } } + if config.V6Only { + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_IPV6, IPV6_V6ONLY, 1); err != nil { + return errors.New("failed to set IPV6_V6ONLY").Base(err) + } + } + return nil }