如何强制Golang https GET请求使用特定的IP地址.我想跳过DNS解析,自己提供IP.curl中的类似功能是--Resolve,如下所示.
curl https://domain.com/dir/filename——解析"domain.com:443:10.10.10"
因为这是SSL,所以我希望避免在IP中替换域,如下例所示.
curl https://10.10.10.10/dir/filename——标题"Host:domain.com"
如何强制Golang https GET请求使用特定的IP地址.我想跳过DNS解析,自己提供IP.curl中的类似功能是--Resolve,如下所示.
curl https://domain.com/dir/filename——解析"domain.com:443:10.10.10"
因为这是SSL,所以我希望避免在IP中替换域,如下例所示.
curl https://10.10.10.10/dir/filename——标题"Host:domain.com"
您可以提供一个自定义的Transport.DialContext
函数.
func main() {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
// DualStack: true, // this is deprecated as of go 1.16
}
// or create your own transport, there's an example on godoc.
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if addr == "google.com:443" {
addr = "216.58.198.206:443"
}
return dialer.DialContext(ctx, network, addr)
}
resp, err := http.Get("https://google.com")
log.Println(resp.Header, err)
}