mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-05 16:56:54 +08:00
28 lines
497 B
Go
28 lines
497 B
Go
package dialer
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func bindToInterface(i *net.Interface) controlFunc {
|
|
return func(network, address string, c syscall.RawConn) (err error) {
|
|
host, _, _ := net.SplitHostPort(address)
|
|
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
|
return nil
|
|
}
|
|
|
|
var innerErr error
|
|
err = c.Control(func(fd uintptr) {
|
|
innerErr = unix.BindToDevice(int(fd), i.Name)
|
|
})
|
|
|
|
if innerErr != nil {
|
|
err = innerErr
|
|
}
|
|
return
|
|
}
|
|
}
|