猿问

如何在转到 IP 地址中绑定 http.Client

我有一台带有多个 NIC 的客户端机器,如何将 Go 中的 http.Client 绑定到某个 NIC 或某个 SRC IP 地址?


假设您有一些非常基本的 http 客户端代码,如下所示:


package main


import (

    "net/http"

)


func main() {

    webclient := &http.Client{}

    req, _ := http.NewRequest("GET", "http://www.google.com", nil)

    httpResponse, _ := webclient.Do(req)

    defer httpResponse.Body.Close()

}

有没有办法绑定到某个网卡或IP?


幕布斯7119047
浏览 181回答 2
2回答

POPMUISE

与此问题类似,您需要设置http.Client.Transport字段。将其设置为实例net.Transport允许您指定net.Dialer要使用的实例。net.Dialer然后允许您指定要建立连接的本地地址。例子:localAddr, err := net.ResolveIPAddr("ip", "<my local address>")if err != nil {&nbsp; panic(err)}// You also need to do this to make it work and not give you a&nbsp;// "mismatched local address type ip"// This will make the ResolveIPAddr a TCPAddr without needing to&nbsp;// say what SRC port number to use.localTCPAddr := net.TCPAddr{&nbsp; &nbsp; IP: localAddr.IP,}webclient := &http.Client{&nbsp; &nbsp; Transport: &http.Transport{&nbsp; &nbsp; &nbsp; &nbsp; Proxy: http.ProxyFromEnvironment,&nbsp; &nbsp; &nbsp; &nbsp; DialContext: (&net.Dialer{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalAddr: &localTCPAddr,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timeout:&nbsp; &nbsp;30 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeepAlive: 30 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DualStack: true,&nbsp; &nbsp; &nbsp; &nbsp; }).DialContext,&nbsp; &nbsp; &nbsp; &nbsp; MaxIdleConns:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100,&nbsp; &nbsp; &nbsp; &nbsp; IdleConnTimeout:&nbsp; &nbsp; &nbsp; &nbsp;90 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; TLSHandshakeTimeout:&nbsp; &nbsp;10 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; ExpectContinueTimeout: 1 * time.Second,&nbsp; &nbsp; },}

www说

这是一个完整的示例,其中包含了 Tim 的答案。我还打破了所有嵌套的部分,以便于阅读和学习。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "net"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; localAddr, err := net.ResolveIPAddr("ip", "10.128.64.219")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; localTCPAddr := net.TCPAddr{&nbsp; &nbsp; &nbsp; &nbsp; IP: localAddr.IP,&nbsp; &nbsp; }&nbsp; &nbsp; d := net.Dialer{&nbsp; &nbsp; &nbsp; &nbsp; LocalAddr: &localTCPAddr,&nbsp; &nbsp; &nbsp; &nbsp; Timeout:&nbsp; &nbsp;30 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; KeepAlive: 30 * time.Second,&nbsp; &nbsp; }&nbsp; &nbsp; tr := &http.Transport{&nbsp; &nbsp; &nbsp; &nbsp; Proxy:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http.ProxyFromEnvironment,&nbsp; &nbsp; &nbsp; &nbsp; Dial:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.Dial,&nbsp; &nbsp; &nbsp; &nbsp; TLSHandshakeTimeout: 10 * time.Second,&nbsp; &nbsp; }&nbsp; &nbsp; webclient := &http.Client{Transport: tr}&nbsp; &nbsp; // Use NewRequest so we can change the UserAgent string in the header&nbsp; &nbsp; req, err := http.NewRequest("GET", "http://www.google.com:80", nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; res, err := webclient.Do(req)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println("DEBUG", res)&nbsp; &nbsp; defer res.Body.Close()&nbsp; &nbsp; content, err := ioutil.ReadAll(res.Body)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%s", string(content))}
随时随地看视频慕课网APP

相关分类

Go
我要回答