代码优化工作

addon-dailer
lqqyt2423 4 years ago
parent 5bdeed62b0
commit 3cc022c365

@ -8,7 +8,10 @@
- [x] http connect - [x] http connect
- [x] cert - [x] cert
- [x] https handler - [x] https handler
- [ ] http2
- [x] logger - [x] logger
- [x] 经内存转发 https 流量 - [x] 经内存转发 https 流量
- [x] 忽略某些错误例如broken pipe, reset by peer, timeout - [x] 忽略某些错误例如broken pipe, reset by peer, timeout
- [ ] websocket
- [ ] support get method with body
- [ ] http2
- [ ] 插件机制

@ -56,7 +56,7 @@ func (c *conn) Read(data []byte) (int, error) {
} }
resChan := make(chan *ioRes) resChan := make(chan *ioRes)
done := make(chan bool) done := make(chan struct{})
defer close(done) defer close(done)
go func() { go func() {

@ -21,6 +21,8 @@ var ignoreErr = func(log *_log.Entry, err error) bool {
"read: connection reset by peer", "read: connection reset by peer",
"write: broken pipe", "write: broken pipe",
"i/o timeout", "i/o timeout",
"net/http: TLS handshake timeout",
"io: read/write on closed pipe",
} }
for _, str := range strs { for _, str := range strs {
@ -44,16 +46,21 @@ type Proxy struct {
} }
func (proxy *Proxy) Start() error { 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() { go func() {
err := proxy.Mitm.Start() err := proxy.Mitm.Start()
if err != nil { errChan <- err
// TODO
log.Fatal(err)
}
}() }()
log.Infof("Proxy start listen at %v\n", proxy.Server.Addr) err := <-errChan
return proxy.Server.ListenAndServe() return err
} }
func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { 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 return
} }
ch := make(chan bool) done := make(chan struct{})
go func() { go func() {
_, err := io.Copy(conn, cconn) _, err := io.Copy(conn, cconn)
if err != nil && !ignoreErr(log, err) { if err != nil && !ignoreErr(log, err) {
log.Error(err) log.Error(err)
} }
ch <- true close(done)
}() }()
_, err = io.Copy(cconn, conn) _, err = io.Copy(cconn, conn)
@ -161,7 +168,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
log.Error(err) log.Error(err)
} }
<-ch <-done
} }
func NewProxy(opts *Options) (*Proxy, error) { func NewProxy(opts *Options) (*Proxy, error) {

Loading…
Cancel
Save