From ab9d83c5e303acb6cb3eb42bc1146ddd5dfb6004 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Mon, 13 Feb 2023 21:44:26 +0800 Subject: [PATCH] Add FindDialer addon --- go.mod | 3 ++- go.sum | 4 ++++ proxy/addon.go | 10 ++++++++++ proxy/connection.go | 20 ++++++++++++++++++-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1813e26..6781e4f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( ) require ( - golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sys v0.5.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/go.sum b/go.sum index db5ad53..3a723e4 100644 --- a/go.sum +++ b/go.sum @@ -19,8 +19,12 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0= golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/proxy/addon.go b/proxy/addon.go index 5fbb531..3726e03 100644 --- a/proxy/addon.go +++ b/proxy/addon.go @@ -5,6 +5,7 @@ import ( "time" log "github.com/sirupsen/logrus" + "golang.org/x/net/proxy" ) type Addon interface { @@ -14,6 +15,9 @@ type Addon interface { // A client connection has been closed (either by us or the client). ClientDisconnected(*ClientConn) + // A new connection will be established, at most one add on can provide a dialer. + FindDialer(connCtx *ConnContext, addr string) proxy.ContextDialer + // Mitmproxy has connected to a server. ServerConnected(*ConnContext) @@ -47,6 +51,7 @@ type BaseAddon struct{} func (addon *BaseAddon) ClientConnected(*ClientConn) {} func (addon *BaseAddon) ClientDisconnected(*ClientConn) {} +func (addon *BaseAddon) FindDialer(connContext *ConnContext, addr string) proxy.ContextDialer { return nil } func (addon *BaseAddon) ServerConnected(*ConnContext) {} func (addon *BaseAddon) ServerDisconnected(*ConnContext) {} @@ -76,6 +81,11 @@ func (addon *LogAddon) ClientDisconnected(client *ClientConn) { log.Infof("%v client disconnect\n", client.Conn.RemoteAddr()) } +func (addon *LogAddon) FindDialer(connCtx *ConnContext, addr string) proxy.ContextDialer { + log.Infof("asked for dialer for %v\n", connCtx.ServerConn) + return nil +} + func (addon *LogAddon) ServerConnected(connCtx *ConnContext) { log.Infof("%v server connect %v (%v->%v)\n", connCtx.ClientConn.Conn.RemoteAddr(), connCtx.ServerConn.Address, connCtx.ServerConn.Conn.LocalAddr(), connCtx.ServerConn.Conn.RemoteAddr()) } diff --git a/proxy/connection.go b/proxy/connection.go index e8e8ce1..8ad31d9 100644 --- a/proxy/connection.go +++ b/proxy/connection.go @@ -15,6 +15,7 @@ import ( uuid "github.com/satori/go.uuid" log "github.com/sirupsen/logrus" + "golang.org/x/net/proxy" ) // client connection @@ -100,8 +101,19 @@ func (connCtx *ConnContext) Id() uuid.UUID { return connCtx.ClientConn.Id } +func (connCtx *ConnContext) dialer(network string, addr string) proxy.ContextDialer { + for _, addon := range connCtx.proxy.Addons { + dialer := addon.FindDialer(connCtx, addr) + if dialer != nil { + return dialer + } + } + return &net.Dialer{} +} + func (connCtx *ConnContext) initHttpServerConn() { if connCtx.ServerConn != nil { + log.Infof("There was a server conn %v", connCtx.ServerConn) return } if connCtx.ClientConn.Tls { @@ -120,7 +132,8 @@ func (connCtx *ConnContext) initHttpServerConn() { Transport: &http.Transport{ Proxy: useProxy, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - c, err := (&net.Dialer{}).DialContext(ctx, network, addr) + dialer := connCtx.dialer(network, addr) + c, err := dialer.DialContext(ctx, network, addr) if err != nil { return nil, err } @@ -159,7 +172,10 @@ func (connCtx *ConnContext) initServerTcpConn(req *http.Request) error { connCtx.ServerConn = ServerConn ServerConn.Address = connCtx.pipeConn.host - plainConn, err := getConnFrom(req.Host, connCtx.proxy.Opts.Upstream) + dialer := connCtx.dialer("tcp", req.Host) + + plainConn, err := dialer.DialContext(context.Background(), "tcp", req.Host) + if err != nil { return err }