2020-10-22 00:11:49 +08:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2020-10-25 20:31:01 +08:00
|
|
|
type controlFn = func(network, address string, c syscall.RawConn) error
|
|
|
|
|
|
|
|
func bindControl(ifaceName string) controlFn {
|
|
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
|
|
ipStr, _, err := net.SplitHostPort(address)
|
|
|
|
if err == nil {
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
if ip != nil && !ip.IsGlobalUnicast() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 00:11:49 +08:00
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
syscall.BindToDevice(int(fd), ifaceName)
|
|
|
|
})
|
|
|
|
}
|
2020-10-25 20:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func bindIfaceToDialer(dialer *net.Dialer, ifaceName string) error {
|
|
|
|
dialer.Control = bindControl(ifaceName)
|
2020-10-22 00:11:49 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindIfaceToListenConfig(lc *net.ListenConfig, ifaceName string) error {
|
2020-10-25 20:31:01 +08:00
|
|
|
lc.Control = bindControl(ifaceName)
|
2020-10-22 00:11:49 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|