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) {