From ce559298839f170767f7f16088a9f4ccdc9652b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 25 Aug 2025 21:28:11 +0800 Subject: [PATCH] ping: Fix missing bind for unix socket --- ping/socket_unix.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ping/socket_unix.go b/ping/socket_unix.go index af072ae..3d3c59b 100644 --- a/ping/socket_unix.go +++ b/ping/socket_unix.go @@ -12,7 +12,6 @@ import ( "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" - "golang.org/x/sys/unix" ) @@ -74,6 +73,17 @@ func connect(privileged bool, controlFunc control.Func, destination netip.Addr) } } + var bindAddress netip.Addr + if !destination.Is6() { + bindAddress = netip.AddrFrom4([4]byte{0, 0, 0, 0}) + } else { + bindAddress = netip.AddrFrom16([16]byte{}) + } + err = unix.Bind(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(bindAddress, 0))) + if err != nil { + return nil, err + } + err = unix.Connect(fd, M.AddrPortToSockaddr(netip.AddrPortFrom(destination, 0))) if err != nil { return nil, E.Cause(err, "connect()")