From c3dc20feb5e4047b3ee8aa90b8a3b5086e0828ae Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Fri, 17 Jun 2022 10:38:18 +0800 Subject: [PATCH] code refactoring --- addon/dumper.go | 2 -- cert/cert.go | 10 ++++------ proxy/addon.go | 2 ++ proxy/connection.go | 5 +++-- proxy/flowencoding.go | 1 + proxy/helper.go | 6 +++--- proxy/middle.go | 3 ++- proxy/proxy.go | 10 ++++------ proxy/websocket.go | 2 ++ web/conn.go | 1 + web/message.go | 1 + web/web.go | 5 +---- 12 files changed, 24 insertions(+), 24 deletions(-) diff --git a/addon/dumper.go b/addon/dumper.go index 1b68ecb..a7cff78 100644 --- a/addon/dumper.go +++ b/addon/dumper.go @@ -45,8 +45,6 @@ func (d *Dumper) Requestheaders(f *proxy.Flow) { func (d *Dumper) dump(f *proxy.Flow) { // 参考 httputil.DumpRequest - log := log.WithField("in", "Dumper") - buf := bytes.NewBuffer(make([]byte, 0)) fmt.Fprintf(buf, "%s %s %s\r\n", f.Request.Method, f.Request.URL.RequestURI(), f.Request.Proto) fmt.Fprintf(buf, "Host: %s\r\n", f.Request.URL.Host) diff --git a/cert/cert.go b/cert/cert.go index ef522ad..9929abf 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -21,16 +21,14 @@ import ( "github.com/golang/groupcache/lru" "github.com/golang/groupcache/singleflight" - _log "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) -var log = _log.WithField("at", "cert") - // reference // https://docs.mitmproxy.org/stable/concepts-certificates/ // https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/certs.py -var caErrNotFound = errors.New("ca not found") +var errCaNotFound = errors.New("ca not found") type CA struct { rsa.PrivateKey @@ -56,7 +54,7 @@ func NewCA(path string) (*CA, error) { } if err := ca.load(); err != nil { - if err != caErrNotFound { + if err != errCaNotFound { return nil, err } } else { @@ -126,7 +124,7 @@ func (ca *CA) load() error { stat, err := os.Stat(caFile) if err != nil { if os.IsNotExist(err) { - return caErrNotFound + return errCaNotFound } return err } diff --git a/proxy/addon.go b/proxy/addon.go index 56a18f7..ddde001 100644 --- a/proxy/addon.go +++ b/proxy/addon.go @@ -2,6 +2,8 @@ package proxy import ( "time" + + log "github.com/sirupsen/logrus" ) type Addon interface { diff --git a/proxy/connection.go b/proxy/connection.go index 0cd606e..ae9962c 100644 --- a/proxy/connection.go +++ b/proxy/connection.go @@ -7,6 +7,7 @@ import ( "net/http" uuid "github.com/satori/go.uuid" + log "github.com/sirupsen/logrus" ) // client connection @@ -58,7 +59,7 @@ func newConnContext(c net.Conn, proxy *Proxy) *ConnContext { } } -func (connCtx *ConnContext) InitHttpServerConn() { +func (connCtx *ConnContext) initHttpServerConn() { if connCtx.ServerConn != nil { return } @@ -104,7 +105,7 @@ func (connCtx *ConnContext) InitHttpServerConn() { connCtx.ServerConn = serverConn } -func (connCtx *ConnContext) InitHttpsServerConn() { +func (connCtx *ConnContext) initHttpsServerConn() { if connCtx.ServerConn != nil { return } diff --git a/proxy/flowencoding.go b/proxy/flowencoding.go index 8cf074f..2676d0c 100644 --- a/proxy/flowencoding.go +++ b/proxy/flowencoding.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/andybalholm/brotli" + log "github.com/sirupsen/logrus" ) var errEncodingNotSupport = errors.New("content-encoding not support") diff --git a/proxy/helper.go b/proxy/helper.go index 5ef6600..793a1fb 100644 --- a/proxy/helper.go +++ b/proxy/helper.go @@ -7,7 +7,7 @@ import ( "strings" "sync" - _log "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) var normalErrMsgs []string = []string{ @@ -22,7 +22,7 @@ var normalErrMsgs []string = []string{ } // 仅打印预料之外的错误信息 -func logErr(log *_log.Entry, err error) (loged bool) { +func logErr(log *log.Entry, err error) (loged bool) { msg := err.Error() for _, str := range normalErrMsgs { @@ -40,7 +40,7 @@ func logErr(log *_log.Entry, err error) (loged bool) { // 转发流量 // Read a => Write b // Read b => Write a -func transfer(log *_log.Entry, a, b io.ReadWriteCloser) { +func transfer(log *log.Entry, a, b io.ReadWriteCloser) { done := make(chan struct{}) defer close(done) diff --git a/proxy/middle.go b/proxy/middle.go index 1f9ac4a..5dbc641 100644 --- a/proxy/middle.go +++ b/proxy/middle.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/lqqyt2423/go-mitmproxy/cert" + log "github.com/sirupsen/logrus" ) // 模拟了标准库中 server 运行,目的是仅通过当前进程内存转发 socket 数据,不需要经过 tcp 或 unix socket @@ -150,7 +151,7 @@ func (m *middle) intercept(pipeServerConn *pipeConn) { if buf[0] == 0x16 && buf[1] == 0x03 && buf[2] <= 0x03 { // tls pipeServerConn.connContext.ClientConn.Tls = true - pipeServerConn.connContext.InitHttpsServerConn() + pipeServerConn.connContext.initHttpsServerConn() m.listener.connChan <- pipeServerConn } else { // ws diff --git a/proxy/proxy.go b/proxy/proxy.go index b1fa4d5..796e68e 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -7,11 +7,9 @@ import ( "net" "net/http" - _log "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) -var log = _log.WithField("at", "proxy") - type Options struct { Debug int Addr string @@ -103,7 +101,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { return } - log := log.WithFields(_log.Fields{ + log := log.WithFields(log.Fields{ "in": "Proxy.ServeHTTP", "url": req.URL, "method": req.Method, @@ -206,7 +204,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { } } - f.ConnContext.InitHttpServerConn() + f.ConnContext.initHttpServerConn() proxyRes, err := f.ConnContext.ServerConn.client.Do(proxyReq) if err != nil { logErr(log, err) @@ -256,7 +254,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) { } func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) { - log := log.WithFields(_log.Fields{ + log := log.WithFields(log.Fields{ "in": "Proxy.handleConnect", "host": req.Host, }) diff --git a/proxy/websocket.go b/proxy/websocket.go index de1b07d..aff8e00 100644 --- a/proxy/websocket.go +++ b/proxy/websocket.go @@ -6,6 +6,8 @@ import ( "net/http" "net/http/httputil" "strings" + + log "github.com/sirupsen/logrus" ) // 当前仅做了转发 websocket 流量 diff --git a/web/conn.go b/web/conn.go index 528b9ff..8f1522d 100644 --- a/web/conn.go +++ b/web/conn.go @@ -6,6 +6,7 @@ import ( "github.com/gorilla/websocket" "github.com/lqqyt2423/go-mitmproxy/proxy" + log "github.com/sirupsen/logrus" ) type breakPointRule struct { diff --git a/web/message.go b/web/message.go index 68fd572..fb2de13 100644 --- a/web/message.go +++ b/web/message.go @@ -8,6 +8,7 @@ import ( "github.com/lqqyt2423/go-mitmproxy/proxy" uuid "github.com/satori/go.uuid" + log "github.com/sirupsen/logrus" ) // message: diff --git a/web/web.go b/web/web.go index 1f5c711..cb2b7f7 100644 --- a/web/web.go +++ b/web/web.go @@ -8,11 +8,9 @@ import ( "github.com/gorilla/websocket" "github.com/lqqyt2423/go-mitmproxy/proxy" - _log "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) -var log = _log.WithField("at", "web addon") - //go:embed client/build var assets embed.FS @@ -42,7 +40,6 @@ func NewWebAddon(addr string) *WebAddon { serverMux.Handle("/", http.FileServer(http.FS(fsys))) server := &http.Server{Addr: addr, Handler: serverMux} - log = log.WithField("in", "WebAddon") web.conns = make([]*concurrentConn, 0) go func() {