BindToInterfaceFunc: allow block()/AutoDetectInterfaceFunc() to return error

to avoid Tun traffic loopback, AutoDetectInterfaceFunc() should return error when no valid interface is found
This commit is contained in:
stT-e5gna2z5MBS 2023-09-19 23:04:30 -04:00
parent 1453c7c8c2
commit abcdbcba64

View file

@ -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 { 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) 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" const useInterfaceName = runtime.GOOS == "linux" || runtime.GOOS == "android"
func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error { 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 return nil
} }
if interfaceName == "" && interfaceIndex == -1 { if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
return nil return nil
} }
if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName { if interfaceName != "" && useInterfaceName || interfaceIndex != -1 && !useInterfaceName {