golang 强制对特定 ip 的 http 请求(类似于 curl --resolve)
导读:如何强制 golang https get 请求使用特定的 IP 地址。我想跳过 DNS 解析并自己提供 IP。 curl 中的等价物是 --resolve,curl https://domain.com/dir/filename --re...
如何强制 golang https get 请求使用特定的 IP 地址。我想跳过 DNS 解析并自己提供 IP。 curl 中的等价物是 --resolve,
curl https://domain.com/dir/filename --resolve "domain.com:443:10.10.10.10"
由于这是 ssl,因此我想避免用 IP 代替域,如下例所示。
curl https://10.10.10.10/dir/filename --header "主机: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)
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: golang 强制对特定 ip 的 http 请求(类似于 curl --resolve)
本文地址: https://pptw.com/jishu/573453.html
