From 3c4a2b06a988c24d9e782cdb087bd24f1a2ba401 Mon Sep 17 00:00:00 2001 From: stT-e5gna2z5MBS <143945532+stT-e5gna2z5MBS@users.noreply.github.com> Date: Wed, 20 Sep 2023 02:05:54 -0400 Subject: [PATCH] BindToInterfaceFunc: allow block()/AutoDetectInterfaceFunc() to return error to avoid Tun traffic loopback, AutoDetectInterfaceFunc() should return error when no valid interface is found --- common/control/bind.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/common/control/bind.go b/common/control/bind.go index 94c621c..4a79185 100644 --- a/common/control/bind.go +++ b/common/control/bind.go @@ -15,9 +15,12 @@ func BindToInterface(finder InterfaceFinder, interfaceName string, interfaceInde } } -func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, address string) (interfaceName string, interfaceIndex int)) Func { +func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, address string) (interfaceName string, interfaceIndex int, err error)) Func { return func(network, address string, conn syscall.RawConn) error { - interfaceName, interfaceIndex := block(network, address) + interfaceName, interfaceIndex, err := block(network, address) + if err != nil { + return err + } return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex) } } @@ -25,10 +28,10 @@ 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 addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) { + if interfaceName == "" && interfaceIndex == -1 { return nil } - if interfaceName == "" && interfaceIndex == -1 { + if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) { return nil } if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName {