gfwproxyshadowsocksdocker-imagegogolanggvisornatnetworksocks4socks5tcpip-stacktortun-devicetun2sockstunneludpwireguard
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.0 KiB
45 lines
1.0 KiB
package dialer
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func setSocketOptions(network, address string, c syscall.RawConn, opts *Options) (err error) {
|
|
if opts == nil || !isTCPSocket(network) && !isUDPSocket(network) {
|
|
return
|
|
}
|
|
|
|
var innerErr error
|
|
err = c.Control(func(fd uintptr) {
|
|
host, _, _ := net.SplitHostPort(address)
|
|
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
|
return
|
|
}
|
|
|
|
if opts.InterfaceIndex == 0 && opts.InterfaceName != "" {
|
|
if iface, err := net.InterfaceByName(opts.InterfaceName); err == nil {
|
|
opts.InterfaceIndex = iface.Index
|
|
}
|
|
}
|
|
|
|
if opts.InterfaceIndex != 0 {
|
|
switch network {
|
|
case "tcp4", "udp4":
|
|
innerErr = unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, opts.InterfaceIndex)
|
|
case "tcp6", "udp6":
|
|
innerErr = unix.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, opts.InterfaceIndex)
|
|
}
|
|
if innerErr != nil {
|
|
return
|
|
}
|
|
}
|
|
})
|
|
|
|
if innerErr != nil {
|
|
err = innerErr
|
|
}
|
|
return
|
|
}
|
|
|