如何在 Go 中复制
curl --proxy-insecure
行为
insecureproxy.net
有自签名证书,somesecureurl.net
有正确且可信的证书吗?https_proxy="https://insecureproxy.net" curl https://somesecureurl.net/ --proxy-insecure
我想接受代理使用不安全的 TLS 证书这一事实,但我仍然想建立到我的目的地的安全 TLS 会话。
net/http
,但我只能使用 InsecureSkipVerify
禁用对 代理和目的地的 TLS 验证。
c.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true
},
Proxy: http.ProxyURL(insecureproxy),
},
}
您可以将客户端传输设置为同时使用 Transport.TLSClientConfig 和 Transport.DialTLSContext。在这种情况下,TLSClientConfig 应用于连接到目标 HTTPS 服务器,而 DialTLSContext 用于连接到您的 HTTPS 代理。
在你的情况下,它可能看起来像这样:
c.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
// configs for destination server or nil to use defaults
},
Proxy: http.ProxyURL(insecureproxy),
DialTLSContext: func(_ context.Context, network, addr string) (net.Conn, error) {
return tls.Dial(network, addr, &tls.Config{InsecureSkipVerify: true})
},
},
}