google / martian

Martian is a library for building custom HTTP/S proxies
Apache License 2.0
2.01k stars 248 forks source link

got 407 when fetching https with downstream proxy #269

Open skiloop opened 6 years ago

skiloop commented 6 years ago

My remote proxy which is password protected and supports https, when applies to martian as downstream proxy, 407 is got.

martian -addr=:8000 -api martian -organization Martian -downstream-proxy-url http://xxxx:password@someproxy:60000
curl -x http://localhost:8000 https://ip.cn  -v

then comes the following result:

skiloop commented 6 years ago

after monitor network traffic, I found not Proxy-Authorization in headers fields of the CONNECT method to downstream proxy. I changed a few lines of function Proxy.connect to:


        pbw := bufio.NewWriter(conn)
        pbr := bufio.NewReader(conn)
        if p.proxyURL.User != nil {
            req.Header.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(p.proxyURL.User.String())))
        }
        req.Write(pbw)
        pbw.Flush()

and this results stucking at tls handshakes

skiloop commented 6 years ago

The problem may be the usage of http.ReadResponse for CONNECT

res, err := http.ReadResponse(pbr, req)

When it write response back, "Connection: close" is added automatically.