dtx9931 2017-03-08 03:20
浏览 874

golang:如何使用代理执行https请求

I am able to do a http or https client request without setting up a proxy,

enter code here
tr := &http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},     
}

client := &http.Client{}
client.Transport = tr

request, err := http.NewRequest("HEAD", "http://www.???.com", nil)
request.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")

resp, err := client.Do(request)

if err != nil {
    log.Fatalln(err)
    return
}
defer resp.Body.Close()

httpcode = resp.Status

This script is working ok, I get a 200 ok when I request https url, but if I set up proxy , the script:

proxyString := "https://47.91.179.xxx:443"
proxyUrl, _ := url.Parse(proxyString)

tr := &http.Transport{
    Proxy: http.ProxyURL(proxyUrl),
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, 

}

client := &http.Client{}
client.Transport = tr

....................

I always get "Bad request", I read docs: https://golang.org/pkg/net/http/:

........... Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS. Programs that must disable HTTP/2 can do so by setting Transport.TLSNextProto (for clients) or Server.TLSNextProto (for servers) to a non-nil, empty map. Alternatively, the following GODEBUG environment variables are currently supported: ..............

So, I tried to stop http2:

tr := &http.Transport{
    Proxy: http.ProxyURL(proxyUrl),
    //Proxy: http.ProxyFromEnvironment,
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    TLSNextProto:    make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}

same problem, "Bad request". I tried os.Setenv("HTTPS_PROXY", "47.91.???.???:443"), It is the same result. This is logged in server:

{S;}220.255.95.68{S;}-{S;}07/Mar/2017:17:01:47 +0800{S;}CONNECT www.panpacific.com:443 HTTP/1.1{S;}400{S;}173{S;}340{S;}-{S;}-{S;}-{S;}-{S;}-{S;}www.panpacific.com

So, Does golang support https client request via proxy? How do I get correct results?

  • 写回答

1条回答

  • drap5081683 2019-09-04 20:46
    关注

    There doesn't appear to be anything wrong with your Client.Transport.Proxy configuration. Both attempted approaches (ProxyURL and ProxyFromEnvironment) should work fine to specify a forward proxy.

    Relevant question: How to program Go to use a proxy?

    Something to look out for, however unlikely, is that subsequent client-side calls to not somehow bypass the use of WriteProxy and instead call Write directly -- since this would defeat the proxy specification.


    Given the information provided here, "Bad Request" likely indicates that you are reaching the proxy, but that it is not working correctly as a generic HTTP/HTTPS forward proxy, which is what Client.Transport.Proxy would specify.

    Maybe some clarity would be added here: How to use Nginx as a HTTP/HTTPS proxy server?. In summary, NGINX is not normally used as a forward proxy, so maybe this isn't what you are trying to do.

    If you are trying to use NGINX as a reverse proxy, then configuration is a server-side concern and your clients do not need special proxy configuration -- just change your request URL to point to the proxy.


    Side note: If not otherwise specified, &http.Client{} will use DefaultTransport which automatically includes ProxyFromEnvironment, so the extra proxy configuration is only required if you are constructing a non-default Transport (as is done in the question in order to specify InsecureSkipVerify) or you want to have an application-specific proxy confuration. And, to repeat, client-side proxy configuration is only needed when using a forward proxy.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决