mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-05 16:56:54 +08:00
27 lines
610 B
Go
Executable File
27 lines
610 B
Go
Executable File
package dialer
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func bindToInterface(i *net.Interface) controlFunc {
|
|
return func(network, address string, c syscall.RawConn) error {
|
|
ipStr, _, _ := net.SplitHostPort(address)
|
|
if ip := net.ParseIP(ipStr); ip != nil && !ip.IsGlobalUnicast() {
|
|
return nil
|
|
}
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
switch network {
|
|
case "tcp4", "udp4":
|
|
unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, i.Index)
|
|
case "tcp6", "udp6":
|
|
unix.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, i.Index)
|
|
}
|
|
})
|
|
}
|
|
}
|