http: optimized proxy client

This commit is contained in:
nadoo 2017-12-17 14:46:24 +08:00
parent 3b73599b7b
commit a045e54504
2 changed files with 12 additions and 6 deletions

16
http.go
View File

@ -190,22 +190,28 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
} }
rc.Write([]byte("CONNECT " + addr + " HTTP/1.0\r\n")) rc.Write([]byte("CONNECT " + addr + " HTTP/1.0\r\n"))
// c.Write([]byte("Proxy-Connection: Keep-Alive\r\n")) rc.Write([]byte("Proxy-Connection: close\r\n"))
if s.user != "" && s.password != "" { if s.user != "" && s.password != "" {
auth := s.user + ":" + s.password auth := s.user + ":" + s.password
rc.Write([]byte("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n")) rc.Write([]byte("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n"))
} }
//header ended
rc.Write([]byte("\r\n")) rc.Write([]byte("\r\n"))
var b [1024]byte respR := bufio.NewReader(rc)
n, err := rc.Read(b[:]) respTP := textproto.NewReader(respR)
if bytes.Contains(b[:n], []byte("200")) { _, code, _, ok := parseFirstLine(respTP)
if ok && code == "200" {
return rc, err return rc, err
} else if code == "407" {
logf("proxy-http: authencation needed by proxy %s", s.addr)
} else if code == "405" {
logf("proxy-http: 'CONNECT' method not allowed by proxy %s", s.addr)
} }
return nil, errors.New("cound not connect remote address:" + addr) return nil, errors.New("cound not connect remote address: " + addr + ". error code: " + code)
} }
// parseFirstLine parses "GET /foo HTTP/1.1" OR "HTTP/1.1 200 OK" into its three parts. // parseFirstLine parses "GET /foo HTTP/1.1" OR "HTTP/1.1 200 OK" into its three parts.

View File

@ -9,7 +9,7 @@ import (
) )
// VERSION . // VERSION .
const VERSION = "0.4.1.1" const VERSION = "0.4.2"
func dialerFromConf() Dialer { func dialerFromConf() Dialer {
// global forwarders in xx.conf // global forwarders in xx.conf