代理 golang https

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

Proxy golang https

问题

我正在尝试使用带有身份验证的代理让一个程序访问https://www.google.com,我已经传递了头部参数

> Proxy-Authorization

但是代理服务器返回

> Proxy Authentication Required

代码:

package main

import (
	"crypto/tls"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
)

func main() {
	req, _ := http.NewRequest("GET", "https://www.google.com.br", nil)
	req.Header.Set("Host", "www.google.com.br")

	proxyURL := url.URL{
		Host: "IP-HERE:PORT-HEERE"}

	transport := &http.Transport{
		Proxy:           http.ProxyURL(&proxyURL),
		TLSClientConfig: &tls.Config{},
	}
	client := &http.Client{Transport: transport}
	req.RequestURI = ""

	auth := fmt.Sprintf("USER:PASSWORD")
	basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
	req.Header.Add("Proxy-Authorization", basic)

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("erro: %s", err)
	}
	fmt.Printf("code: %s", resp.StatusCode)
	htmlData, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(os.Stdout, string(htmlData))
}

我需要传递其他参数吗?

当我在http://www.google.com.br上执行一个不带https的GET请求时,代理身份验证成功。为什么呢?

英文:

i am trying to make one get in https://www.google.com using proxy with authentication, i already passing header parameter

> Proxy-Authorization

but proxy server return

> Proxy Authentication Required

code:

package main

import (
	"crypto/tls"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
)

func main() {
	req, _ := http.NewRequest("GET", "https://www.google.com.br", nil)
	req.Header.Set("Host", "www.google.com.br")

	proxyURL := url.URL{
		Host: "IP-HERE:PORT-HEERE"}

	transport := &http.Transport{
		Proxy:           http.ProxyURL(&proxyURL),
		TLSClientConfig: &tls.Config{},
	}
	client := &http.Client{Transport: transport}
	req.RequestURI = ""

	auth := fmt.Sprintf("USER:PASSWORD")
	basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
	req.Header.Add("Proxy-Authorization", basic)

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("erro: %s", err)
	}
	fmt.Printf("code: %s", resp.StatusCode)
	htmlData, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(os.Stdout, string(htmlData))
}

I have to pass another parameter?

When i perform one get in http://www.google.com.br, without https.. proxy authentication with success. why?

答案1

得分: 2

我找到了解决方案。Golang在CONNECT方法中不传递头参数,因此Proxy-Authorization参数不会发送到代理服务器。

为了解决这个问题,将ProxyConnectHeader字段添加到Transport结构体中。但是这个更改只有在发布之后才会生效,目前只在主分支中。

为了测试这个新字段,我在GitHub上创建了一个项目,并且成功运行。

golang路径链接

测试这个新字段的GitHub项目链接

英文:

I found the solution. Golang don't pass header parameters in method CONNECT, then parameter Proxy-Authorization aren't sent to proxy server.

to resolve this, field ProxyConnectHeader was added to struct Transport. but this change until released, this change are only in master.

to test this new field using golang from master, i made on project in github and worked.

link to golang path

link to project in github that test this new field

huangapple
  • 本文由 发表于 2016年11月18日 20:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/40676825.html
匿名

发表评论

匿名网友

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

确定