From 2032ec87b4f95ef2e754f002386702f9fc07fdf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 30 Sep 2023 21:36:00 +0800 Subject: [PATCH] Improve linux bind interface --- common/control/bind.go | 33 +++--------------------------- common/control/bind_darwin.go | 16 +++++++++++---- common/control/bind_linux.go | 37 +++++++++++++++++++++++++++++++++- common/control/bind_windows.go | 15 ++++++++++++-- 4 files changed, 64 insertions(+), 37 deletions(-) diff --git a/common/control/bind.go b/common/control/bind.go index 4a79185..b8451db 100644 --- a/common/control/bind.go +++ b/common/control/bind.go @@ -1,10 +1,9 @@ package control import ( - "os" - "runtime" "syscall" + E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" ) @@ -25,38 +24,12 @@ func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, addr } } -const useInterfaceName = runtime.GOOS == "linux" || runtime.GOOS == "android" - func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { if interfaceName == "" && interfaceIndex == -1 { - return nil + return E.New("interface not found: ", interfaceName) } if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) { return nil } - if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName { - return bindToInterface(conn, network, address, interfaceName, interfaceIndex) - } - if finder == nil { - return os.ErrInvalid - } - var err error - if useInterfaceName { - interfaceName, err = finder.InterfaceNameByIndex(interfaceIndex) - } else { - interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) - } - if err != nil { - return err - } - if useInterfaceName { - if interfaceName == "" { - return nil - } - } else { - if interfaceIndex == -1 { - return nil - } - } - return bindToInterface(conn, network, address, interfaceName, interfaceIndex) + return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex) } diff --git a/common/control/bind_darwin.go b/common/control/bind_darwin.go index 8262ac7..f5be42d 100644 --- a/common/control/bind_darwin.go +++ b/common/control/bind_darwin.go @@ -1,16 +1,24 @@ package control import ( + "os" "syscall" "golang.org/x/sys/unix" ) -func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { - if interfaceIndex == -1 { - return nil - } +func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error { return Raw(conn, func(fd uintptr) error { + var err error + if interfaceIndex == -1 { + if finder == nil { + return os.ErrInvalid + } + interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + if err != nil { + return err + } + } switch network { case "tcp6", "udp6": return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex) diff --git a/common/control/bind_linux.go b/common/control/bind_linux.go index 6ebca49..51529a0 100644 --- a/common/control/bind_linux.go +++ b/common/control/bind_linux.go @@ -1,13 +1,48 @@ package control import ( + "os" "syscall" + "github.com/sagernet/sing/common/atomic" + E "github.com/sagernet/sing/common/exceptions" + "golang.org/x/sys/unix" ) -func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { +var ifIndexDisabled atomic.Bool + +func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error { return Raw(conn, func(fd uintptr) error { + var err error + if !ifIndexDisabled.Load() { + if interfaceIndex == -1 { + if finder == nil { + return os.ErrInvalid + } + interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + if err != nil { + return err + } + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex) + if err == nil { + return nil + } else if E.IsMulti(err, unix.ENOPROTOOPT, unix.EINVAL) { + ifIndexDisabled.Store(true) + } else { + return err + } + } + if interfaceName == "" { + if finder == nil { + return os.ErrInvalid + } + interfaceName, err = finder.InterfaceNameByIndex(interfaceIndex) + if err != nil { + return err + } + } return unix.BindToDevice(int(fd), interfaceName) }) } diff --git a/common/control/bind_windows.go b/common/control/bind_windows.go index 5e23bf1..7029c80 100644 --- a/common/control/bind_windows.go +++ b/common/control/bind_windows.go @@ -2,17 +2,28 @@ package control import ( "encoding/binary" + "os" "syscall" "unsafe" M "github.com/sagernet/sing/common/metadata" ) -func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { +func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int) error { return Raw(conn, func(fd uintptr) error { + var err error + if interfaceIndex == -1 { + if finder == nil { + return os.ErrInvalid + } + interfaceIndex, err = finder.InterfaceIndexByName(interfaceName) + if err != nil { + return err + } + } handle := syscall.Handle(fd) if M.ParseSocksaddr(address).AddrString() == "" { - err := bind4(handle, interfaceIndex) + err = bind4(handle, interfaceIndex) if err != nil { return err }