github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/control/bind.go (about) 1 package control 2 3 import ( 4 "syscall" 5 6 E "github.com/sagernet/sing/common/exceptions" 7 M "github.com/sagernet/sing/common/metadata" 8 N "github.com/sagernet/sing/common/network" 9 ) 10 11 func BindToInterface(finder InterfaceFinder, interfaceName string, interfaceIndex int) Func { 12 return func(network, address string, conn syscall.RawConn) error { 13 return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex, false) 14 } 15 } 16 17 func BindToInterfaceFunc(finder InterfaceFinder, block func(network string, address string) (interfaceName string, interfaceIndex int, err error)) Func { 18 return func(network, address string, conn syscall.RawConn) error { 19 interfaceName, interfaceIndex, err := block(network, address) 20 if err != nil { 21 return err 22 } 23 return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex, false) 24 } 25 } 26 27 func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int, preferInterfaceName bool) error { 28 if interfaceName == "" && interfaceIndex == -1 { 29 return E.New("interface not found: ", interfaceName) 30 } 31 if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) { 32 return nil 33 } 34 return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex, preferInterfaceName) 35 }