通过基本身份验证使用HTTP代理访问HTTPS

huangapple go评论94阅读模式
英文:

Access HTTPS via HTTP proxy with basic authentication

问题

我正在尝试使用带有用户名/密码授权的代理对HTTPS URL进行GET请求(代理需要授权,而不是网站)。
以下是我的操作:

package main

import (
	"crypto/tls"
	"encoding/base64"
	"fmt"
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	ua := "Mozilla/5.0 (Windows NT 6.1"
	basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:mypass"))
	req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
	proxyUrl, _ := url.Parse("http://myproxy.com:9999")
	fmt.Println(basic) // Basic dXNlcm5hbWU6bXlwYXNz
	req.Header.Add("Proxy-Authorization", basic)
	req.Header.Add("User-Agent", ua)
	bb, _ := httputil.DumpRequest(req, false)
	fmt.Println(string(bb))
	/*
	Get / HTTP/1.1
	Host: api.ipify.org
	Proxy-Authorization: Basic dXNlcm5hbWU6bXlwYXNz
	User-Agent: Mozilla/5.0 (Windows NT 6.1
	*/
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			Proxy:           http.ProxyURL(proxyUrl),
		},
	}
	resp, err := client.Do(req)
	fmt.Println(err)  // Proxy Authentication Required
	fmt.Println(resp) // <nil>
}

问题在于,当我尝试对HTTP(而不是HTTPS)站点进行请求时,一切正常,但如果我进行HTTPS请求,则失败(请参见上面的消息)。

我使用浏览器(FireFox)测试了代理,一切都正常,我通过Firebug查找了头信息,并添加了所有与请求相关的内容(如上所示)。我已经仔细检查了basic值和其他所有内容,但没有任何运气。

所以,有人知道为什么会发生这种情况,或者至少我该如何研究这个问题吗?

最后要补充的是,我可以在这种情况下使用一个不需要任何授权的公共HTTP代理(错误消息也暗示了这一点),问题似乎是在授权进入这个过程时出现的。

P.S. 不幸的是,我不能分享代理的IP、端口和用户名,因为这违反了他们的政策。

英文:

I am trying to make a GET request against an HTTPS URL using proxy with username/password authorization (auth is required by the proxy not the website).
Here's what I do:

package main

import (
	&quot;crypto/tls&quot;
	&quot;encoding/base64&quot;
	&quot;fmt&quot;
	&quot;net/http&quot;
	&quot;net/http/httputil&quot;
	&quot;net/url&quot;
)

func main() {
	ua := &quot;Mozilla/5.0 (Windows NT 6.1&quot;
	basic := &quot;Basic &quot; + base64.StdEncoding.EncodeToString([]byte(&quot;username:mypass&quot;))
	req, err := http.NewRequest(&quot;GET&quot;, &quot;https://api.ipify.org/&quot;, nil)
	proxyUrl, _ := url.Parse(&quot;http://myproxy.com:9999&quot;)
	fmt.Println(basic) // Basic dXNlcm5hbWU6bXlwYXNz
	req.Header.Add(&quot;Proxy-Authorization&quot;, basic)
	req.Header.Add(&quot;User-Agent&quot;, ua)
	bb, _ := httputil.DumpRequest(req, false)
	fmt.Println(string(bb))
	/*
	Get / HTTP/1.1
	Host: api.ipify.org
	Proxy-Authorization: Basic dXNlcm5hbWU6bXlwYXNz
	User-Agent: Mozilla/5.0 (Windows NT 6.1
	*/
	client := &amp;http.Client{
		Transport: &amp;http.Transport{
			TLSClientConfig: &amp;tls.Config{InsecureSkipVerify: true},
			Proxy:           http.ProxyURL(proxyUrl),
		},
	}
	resp, err := client.Do(req)
	fmt.Println(err)  // Proxy Authentication Required
	fmt.Println(resp) // &lt;nil&gt;
}

The catch is that when I try to do a request to an HTTP (not HTTPS) site it goes fine, but if I make HTTPS request it fails (see above the message).

I tested the proxy with my browser (FireFox) and everything goes well, I looked for the headers through firebug and added everything relevant to the request (above). I've double-triple-checked the basic value and everything else but without any luck.

So does some one have any idea why this happens or at least how do I research the problem?

The last thing to add is that I can use a public HTTP proxy (that doesn't require any auth) in this case and problems seem to start when auth enters in this process (the error also suggests that).

P.S. Unfortunately I cannot share the proxy IP, port and username cause it is against their policy.

答案1

得分: 3

package main

import (
	"crypto/tls"
	"fmt"
	"net/url"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
	proxyUrl, _ := url.Parse("http://username:password@127.0.0.1:9999")
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			Proxy:           http.ProxyURL(proxyUrl),
		},
	}
	_, err = client.Do(req)
	fmt.Println(err)
}
package main

import (
	"crypto/tls"
	"fmt"
	"net/url"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.ipify.org/", nil)
	proxyUrl, _ := url.Parse("http://username:password@127.0.0.1:9999")
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			Proxy:           http.ProxyURL(proxyUrl),
		},
	}
	_, err = client.Do(req)
	fmt.Println(err)
}
英文:
package main

import (
	&quot;crypto/tls&quot;
	&quot;fmt&quot;
	&quot;net/url&quot;
	&quot;net/http&quot;
)

func main() {
	req, err := http.NewRequest(&quot;GET&quot;, &quot;https://api.ipify.org/&quot;, nil)
	proxyUrl, _ := url.Parse(&quot;http://username:password@127.0.0.1:9999&quot;)
	client := &amp;http.Client{
		Transport: &amp;http.Transport{
			TLSClientConfig: &amp;tls.Config{InsecureSkipVerify: true},
			Proxy:           http.ProxyURL(proxyUrl),
		},
	}
	_, err = client.Do(req)
	fmt.Println(err)
}

huangapple
  • 本文由 发表于 2016年11月26日 19:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/40817784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定