code refactoring

addon-dailer
lqqyt2423 3 years ago
parent 01f0cc7a4b
commit c3dc20feb5

@ -45,8 +45,6 @@ func (d *Dumper) Requestheaders(f *proxy.Flow) {
func (d *Dumper) dump(f *proxy.Flow) { func (d *Dumper) dump(f *proxy.Flow) {
// 参考 httputil.DumpRequest // 参考 httputil.DumpRequest
log := log.WithField("in", "Dumper")
buf := bytes.NewBuffer(make([]byte, 0)) 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, "%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) fmt.Fprintf(buf, "Host: %s\r\n", f.Request.URL.Host)

@ -21,16 +21,14 @@ import (
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
"github.com/golang/groupcache/singleflight" "github.com/golang/groupcache/singleflight"
_log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var log = _log.WithField("at", "cert")
// reference // reference
// https://docs.mitmproxy.org/stable/concepts-certificates/ // https://docs.mitmproxy.org/stable/concepts-certificates/
// https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/certs.py // 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 { type CA struct {
rsa.PrivateKey rsa.PrivateKey
@ -56,7 +54,7 @@ func NewCA(path string) (*CA, error) {
} }
if err := ca.load(); err != nil { if err := ca.load(); err != nil {
if err != caErrNotFound { if err != errCaNotFound {
return nil, err return nil, err
} }
} else { } else {
@ -126,7 +124,7 @@ func (ca *CA) load() error {
stat, err := os.Stat(caFile) stat, err := os.Stat(caFile)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return caErrNotFound return errCaNotFound
} }
return err return err
} }

@ -2,6 +2,8 @@ package proxy
import ( import (
"time" "time"
log "github.com/sirupsen/logrus"
) )
type Addon interface { type Addon interface {

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
) )
// client connection // 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 { if connCtx.ServerConn != nil {
return return
} }
@ -104,7 +105,7 @@ func (connCtx *ConnContext) InitHttpServerConn() {
connCtx.ServerConn = serverConn connCtx.ServerConn = serverConn
} }
func (connCtx *ConnContext) InitHttpsServerConn() { func (connCtx *ConnContext) initHttpsServerConn() {
if connCtx.ServerConn != nil { if connCtx.ServerConn != nil {
return return
} }

@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/andybalholm/brotli" "github.com/andybalholm/brotli"
log "github.com/sirupsen/logrus"
) )
var errEncodingNotSupport = errors.New("content-encoding not support") var errEncodingNotSupport = errors.New("content-encoding not support")

@ -7,7 +7,7 @@ import (
"strings" "strings"
"sync" "sync"
_log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var normalErrMsgs []string = []string{ 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() msg := err.Error()
for _, str := range normalErrMsgs { for _, str := range normalErrMsgs {
@ -40,7 +40,7 @@ func logErr(log *_log.Entry, err error) (loged bool) {
// 转发流量 // 转发流量
// Read a => Write b // Read a => Write b
// Read b => Write a // 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{}) done := make(chan struct{})
defer close(done) defer close(done)

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/lqqyt2423/go-mitmproxy/cert" "github.com/lqqyt2423/go-mitmproxy/cert"
log "github.com/sirupsen/logrus"
) )
// 模拟了标准库中 server 运行,目的是仅通过当前进程内存转发 socket 数据,不需要经过 tcp 或 unix socket // 模拟了标准库中 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 { if buf[0] == 0x16 && buf[1] == 0x03 && buf[2] <= 0x03 {
// tls // tls
pipeServerConn.connContext.ClientConn.Tls = true pipeServerConn.connContext.ClientConn.Tls = true
pipeServerConn.connContext.InitHttpsServerConn() pipeServerConn.connContext.initHttpsServerConn()
m.listener.connChan <- pipeServerConn m.listener.connChan <- pipeServerConn
} else { } else {
// ws // ws

@ -7,11 +7,9 @@ import (
"net" "net"
"net/http" "net/http"
_log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var log = _log.WithField("at", "proxy")
type Options struct { type Options struct {
Debug int Debug int
Addr string Addr string
@ -103,7 +101,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
return return
} }
log := log.WithFields(_log.Fields{ log := log.WithFields(log.Fields{
"in": "Proxy.ServeHTTP", "in": "Proxy.ServeHTTP",
"url": req.URL, "url": req.URL,
"method": req.Method, "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) proxyRes, err := f.ConnContext.ServerConn.client.Do(proxyReq)
if err != nil { if err != nil {
logErr(log, err) 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) { func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
log := log.WithFields(_log.Fields{ log := log.WithFields(log.Fields{
"in": "Proxy.handleConnect", "in": "Proxy.handleConnect",
"host": req.Host, "host": req.Host,
}) })

@ -6,6 +6,8 @@ import (
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"strings" "strings"
log "github.com/sirupsen/logrus"
) )
// 当前仅做了转发 websocket 流量 // 当前仅做了转发 websocket 流量

@ -6,6 +6,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/lqqyt2423/go-mitmproxy/proxy" "github.com/lqqyt2423/go-mitmproxy/proxy"
log "github.com/sirupsen/logrus"
) )
type breakPointRule struct { type breakPointRule struct {

@ -8,6 +8,7 @@ import (
"github.com/lqqyt2423/go-mitmproxy/proxy" "github.com/lqqyt2423/go-mitmproxy/proxy"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
) )
// message: // message:

@ -8,11 +8,9 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/lqqyt2423/go-mitmproxy/proxy" "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 //go:embed client/build
var assets embed.FS var assets embed.FS
@ -42,7 +40,6 @@ func NewWebAddon(addr string) *WebAddon {
serverMux.Handle("/", http.FileServer(http.FS(fsys))) serverMux.Handle("/", http.FileServer(http.FS(fsys)))
server := &http.Server{Addr: addr, Handler: serverMux} server := &http.Server{Addr: addr, Handler: serverMux}
log = log.WithField("in", "WebAddon")
web.conns = make([]*concurrentConn, 0) web.conns = make([]*concurrentConn, 0)
go func() { go func() {

Loading…
Cancel
Save