From 818cd2dd4bfa277107fda15a2b20f497e4dd7a53 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_linux.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/control/bind_linux.go b/common/control/bind_linux.go index 6ebca49d..2979dad4 100644 --- a/common/control/bind_linux.go +++ b/common/control/bind_linux.go @@ -1,13 +1,28 @@ package control import ( + "errors" "syscall" + "github.com/sagernet/sing/common/atomic" + "golang.org/x/sys/unix" ) +var ifIndexDisabled atomic.Bool + func bindToInterface(conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { return Raw(conn, func(fd uintptr) error { + if !ifIndexDisabled.Load() { + err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX, interfaceIndex) + if err == nil { + return nil + } else if errors.Is(err, unix.ENOPROTOOPT) { + ifIndexDisabled.Store(true) + } else { + return err + } + } return unix.BindToDevice(int(fd), interfaceName) }) }