From 3cc022c36589aeef0bca1a3d95c7a60559faac2f Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Wed, 9 Dec 2020 22:24:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- proxy/mitm_memory.go | 2 +- proxy/proxy.go | 25 ++++++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c863d2f..a2370c7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,10 @@ - [x] http connect - [x] cert - [x] https handler -- [ ] http2 - [x] logger - [x] 经内存转发 https 流量 - [x] 忽略某些错误例如:broken pipe, reset by peer, timeout +- [ ] websocket +- [ ] support get method with body +- [ ] http2 +- [ ] 插件机制 diff --git a/proxy/mitm_memory.go b/proxy/mitm_memory.go index 5c3c0cc..7979c91 100644 --- a/proxy/mitm_memory.go +++ b/proxy/mitm_memory.go @@ -56,7 +56,7 @@ func (c *conn) Read(data []byte) (int, error) { } resChan := make(chan *ioRes) - done := make(chan bool) + done := make(chan struct{}) defer close(done) go func() { diff --git a/proxy/proxy.go b/proxy/proxy.go index 3b90cc7..7b9c2c2 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -21,6 +21,8 @@ var ignoreErr = func(log *_log.Entry, err error) bool { "read: connection reset by peer", "write: broken pipe", "i/o timeout", + "net/http: TLS handshake timeout", + "io: read/write on closed pipe", } for _, str := range strs { @@ -44,16 +46,21 @@ type Proxy struct { } func (proxy *Proxy) Start() error { + errChan := make(chan error) + + go func() { + log.Infof("Proxy start listen at %v\n", proxy.Server.Addr) + err := proxy.Server.ListenAndServe() + errChan <- err + }() + go func() { err := proxy.Mitm.Start() - if err != nil { - // TODO - log.Fatal(err) - } + errChan <- err }() - log.Infof("Proxy start listen at %v\n", proxy.Server.Addr) - return proxy.Server.ListenAndServe() + err := <-errChan + return err } func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { @@ -147,13 +154,13 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { return } - ch := make(chan bool) + done := make(chan struct{}) go func() { _, err := io.Copy(conn, cconn) if err != nil && !ignoreErr(log, err) { log.Error(err) } - ch <- true + close(done) }() _, err = io.Copy(cconn, conn) @@ -161,7 +168,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { log.Error(err) } - <-ch + <-done } func NewProxy(opts *Options) (*Proxy, error) {