code refactoring

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

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

@ -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
}

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

@ -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
}

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

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

@ -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

@ -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,
})

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

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

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

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

Loading…
Cancel
Save