Google Go Lang - 获取 net/http 的套接字 id/fd 以与 syscall.Bind 一起使用
Google Go Lang - Getting socket id/fd of net/http to use with syscall.Bind
我正在尝试获取 net/http 请求的套接字 id/fd 以便我可以将它与 syscall.Bind() 一起使用以将套接字绑定到我的许多 public 传出 IPV4 地址。
我希望能够 select 哪个 IP 地址用于传出请求。这是为了 Windows。
非常感谢任何帮助。
下面是一些用于 linux 的代码,但我需要获取 http.Client 的套接字 fd 而不是 net.Conn。
func bindToIf(conn net.Conn, interfaceName string) {
ptrVal := reflect.ValueOf(conn)
val := reflect.Indirect(ptrVal)
//next line will get you the net.netFD
fdmember := val.FieldByName("fd")
val1 := reflect.Indirect(fdmember)
netFdPtr := val1.FieldByName("sysfd")
fd := int(netFdPtr.Int())
//fd now has the actual fd for the socket
err := syscall.SetsockoptString(fd, syscall.SOL_SOCKET,
syscall.SO_BINDTODEVICE, interfaceName)
if err != nil {
log.Fatal(err)
}
}
I'm trying to get the socket id/fd of a net/http request
http.Request
或 http.Client
都没有插座供您使用。
您可以通过修改 Transport 来自定义 http.Client
创建 TCP 连接的方式。请参阅 Dial
和 DialTLS
函数。
来自文档:
Dial specifies the dial function for creating unencrypted TCP connections. If Dial is nil, net.Dial is used.
您可能对this question感兴趣,它询问如何使用特定接口拨号。
您可以像这样设置默认传输:
http.DefaultTransport.(*http.Transport).Dial = func(network, addr string) (net.Conn, error) {
d := net.Dialer{LocalAddr: /* your addr here */}
return d.Dial(network, addr)
}
如果您使用的是 TLS,则需要为 http.DefaultTransport.DialTLS
执行类似的操作。
我正在尝试获取 net/http 请求的套接字 id/fd 以便我可以将它与 syscall.Bind() 一起使用以将套接字绑定到我的许多 public 传出 IPV4 地址。
我希望能够 select 哪个 IP 地址用于传出请求。这是为了 Windows。
非常感谢任何帮助。
下面是一些用于 linux 的代码,但我需要获取 http.Client 的套接字 fd 而不是 net.Conn。
func bindToIf(conn net.Conn, interfaceName string) {
ptrVal := reflect.ValueOf(conn)
val := reflect.Indirect(ptrVal)
//next line will get you the net.netFD
fdmember := val.FieldByName("fd")
val1 := reflect.Indirect(fdmember)
netFdPtr := val1.FieldByName("sysfd")
fd := int(netFdPtr.Int())
//fd now has the actual fd for the socket
err := syscall.SetsockoptString(fd, syscall.SOL_SOCKET,
syscall.SO_BINDTODEVICE, interfaceName)
if err != nil {
log.Fatal(err)
}
}
I'm trying to get the socket id/fd of a net/http request
http.Request
或 http.Client
都没有插座供您使用。
您可以通过修改 Transport 来自定义 http.Client
创建 TCP 连接的方式。请参阅 Dial
和 DialTLS
函数。
来自文档:
Dial specifies the dial function for creating unencrypted TCP connections. If Dial is nil, net.Dial is used.
您可能对this question感兴趣,它询问如何使用特定接口拨号。
您可以像这样设置默认传输:
http.DefaultTransport.(*http.Transport).Dial = func(network, addr string) (net.Conn, error) {
d := net.Dialer{LocalAddr: /* your addr here */}
return d.Dial(network, addr)
}
如果您使用的是 TLS,则需要为 http.DefaultTransport.DialTLS
执行类似的操作。