From a045e545046bdf5627adcae05e69a372907f9f8a Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sun, 17 Dec 2017 14:46:24 +0800 Subject: [PATCH] http: optimized proxy client --- http.go | 16 +++++++++++----- main.go | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/http.go b/http.go index 7d4fff4..e182919 100644 --- a/http.go +++ b/http.go @@ -190,22 +190,28 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) { } 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 != "" { auth := s.user + ":" + s.password rc.Write([]byte("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n")) } + //header ended rc.Write([]byte("\r\n")) - var b [1024]byte - n, err := rc.Read(b[:]) - if bytes.Contains(b[:n], []byte("200")) { + respR := bufio.NewReader(rc) + respTP := textproto.NewReader(respR) + _, code, _, ok := parseFirstLine(respTP) + if ok && code == "200" { 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. diff --git a/main.go b/main.go index 81c6709..d985c33 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( ) // VERSION . -const VERSION = "0.4.1.1" +const VERSION = "0.4.2" func dialerFromConf() Dialer { // global forwarders in xx.conf