mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package dialer
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
)
|
|
|
|
func bindIfaceToDialer(dialer *net.Dialer, ifaceName string) error {
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dialer.Control = func(network, address string, c syscall.RawConn) error {
|
|
return c.Control(func(fd uintptr) {
|
|
switch network {
|
|
case "tcp4", "udp4":
|
|
syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, iface.Index)
|
|
case "tcp6", "udp6":
|
|
syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, iface.Index)
|
|
}
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func bindIfaceToListenConfig(lc *net.ListenConfig, ifaceName string) error {
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
lc.Control = func(network, address string, c syscall.RawConn) error {
|
|
return c.Control(func(fd uintptr) {
|
|
switch network {
|
|
case "tcp4", "udp4":
|
|
syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, iface.Index)
|
|
case "tcp6", "udp6":
|
|
syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, iface.Index)
|
|
}
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|