mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 12:27:37 +03:00
35 lines
1.3 KiB
Go
35 lines
1.3 KiB
Go
package control
|
|
|
|
import (
|
|
"syscall"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
func BindToInterface(finder InterfaceFinder, interfaceName string, interfaceIndex int) Func {
|
|
return func(network, address string, conn syscall.RawConn) error {
|
|
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex)
|
|
}
|
|
}
|
|
|
|
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, err := block(network, address)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return BindToInterface0(finder, conn, network, address, interfaceName, interfaceIndex)
|
|
}
|
|
}
|
|
|
|
func BindToInterface0(finder InterfaceFinder, conn syscall.RawConn, network string, address string, interfaceName string, interfaceIndex int) error {
|
|
if interfaceName == "" && interfaceIndex == -1 {
|
|
return E.New("interface not found: ", interfaceName)
|
|
}
|
|
if addr := M.ParseSocksaddr(address).Addr; addr.IsValid() && N.IsVirtual(addr) {
|
|
return nil
|
|
}
|
|
return bindToInterface(conn, network, address, finder, interfaceName, interfaceIndex)
|
|
}
|