http add server hook

addon-dailer
lqqyt2423 2 years ago
parent d95a8ccf21
commit 46f165105d

@ -17,6 +17,12 @@ type Addon interface {
// A client connection has been closed (either by us or the client). // A client connection has been closed (either by us or the client).
ClientDisconnected(*connection.Client) ClientDisconnected(*connection.Client)
// Mitmproxy has connected to a server.
ServerConnected(*flow.ConnContext)
// A server connection has been closed (either by us or the server).
ServerDisconnected(*flow.ConnContext)
// HTTP request headers were successfully read. At this point, the body is empty. // HTTP request headers were successfully read. At this point, the body is empty.
Requestheaders(*flow.Flow) Requestheaders(*flow.Flow)
@ -35,6 +41,8 @@ type Base struct{}
func (addon *Base) ClientConnected(*connection.Client) {} func (addon *Base) ClientConnected(*connection.Client) {}
func (addon *Base) ClientDisconnected(*connection.Client) {} func (addon *Base) ClientDisconnected(*connection.Client) {}
func (addon *Base) ServerConnected(*flow.ConnContext) {}
func (addon *Base) ServerDisconnected(*flow.ConnContext) {}
func (addon *Base) Requestheaders(*flow.Flow) {} func (addon *Base) Requestheaders(*flow.Flow) {}
func (addon *Base) Request(*flow.Flow) {} func (addon *Base) Request(*flow.Flow) {}
@ -54,6 +62,14 @@ func (addon *Log) ClientDisconnected(client *connection.Client) {
log.Infof("%v client disconnect\n", client.Conn.RemoteAddr()) log.Infof("%v client disconnect\n", client.Conn.RemoteAddr())
} }
func (addon *Log) ServerConnected(connCtx *flow.ConnContext) {
log.Infof("%v server connect %v\n", connCtx.Client.Conn.RemoteAddr(), connCtx.Server.Conn.RemoteAddr())
}
func (addon *Log) ServerDisconnected(connCtx *flow.ConnContext) {
log.Infof("%v server disconnect %v\n", connCtx.Client.Conn.RemoteAddr(), connCtx.Server.Conn.RemoteAddr())
}
func (addon *Log) Requestheaders(f *flow.Flow) { func (addon *Log) Requestheaders(f *flow.Flow) {
log := log.WithField("in", "Log") log := log.WithField("in", "Log")
start := time.Now() start := time.Now()

@ -23,16 +23,7 @@ func NewConnContext(c net.Conn) *ConnContext {
} }
} }
type serverConn struct { func (connCtx *ConnContext) InitHttpServer(sslInsecure bool, connWrap func(net.Conn) net.Conn, whenServerConnected func()) {
net.Conn
}
func (c *serverConn) Close() error {
log.Debugln("in http serverConn close")
return c.Conn.Close()
}
func (connCtx *ConnContext) InitHttpServer(SslInsecure bool) {
if connCtx.Server != nil { if connCtx.Server != nil {
return return
} }
@ -55,15 +46,16 @@ func (connCtx *ConnContext) InitHttpServer(SslInsecure bool) {
return nil, err return nil, err
} }
cw := &serverConn{c} cw := connWrap(c)
server.Conn = cw server.Conn = cw
defer whenServerConnected()
return cw, nil return cw, nil
}, },
ForceAttemptHTTP2: false, // disable http2 ForceAttemptHTTP2: false, // disable http2
DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true. DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true.
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
InsecureSkipVerify: SslInsecure, InsecureSkipVerify: sslInsecure,
KeyLogWriter: GetTlsKeyLogWriter(), KeyLogWriter: GetTlsKeyLogWriter(),
}, },
}, },
@ -75,7 +67,7 @@ func (connCtx *ConnContext) InitHttpServer(SslInsecure bool) {
connCtx.Server = server connCtx.Server = server
} }
func (connCtx *ConnContext) InitHttpsServer(SslInsecure bool) { func (connCtx *ConnContext) InitHttpsServer(sslInsecure bool) {
if connCtx.Server != nil { if connCtx.Server != nil {
return return
} }
@ -97,7 +89,7 @@ func (connCtx *ConnContext) InitHttpsServer(SslInsecure bool) {
DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true. DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true.
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
InsecureSkipVerify: SslInsecure, InsecureSkipVerify: sslInsecure,
KeyLogWriter: GetTlsKeyLogWriter(), KeyLogWriter: GetTlsKeyLogWriter(),
}, },
}, },

@ -6,7 +6,6 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"sync"
"github.com/lqqyt2423/go-mitmproxy/addon" "github.com/lqqyt2423/go-mitmproxy/addon"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
@ -52,19 +51,54 @@ type proxyConn struct {
net.Conn net.Conn
proxy *Proxy proxy *Proxy
connCtx *flow.ConnContext connCtx *flow.ConnContext
closeOnce sync.Once closed bool
closeErr error
} }
func (c *proxyConn) Close() error { func (c *proxyConn) Close() error {
log.Debugln("in proxyConn close") log.Debugln("in proxyConn close")
if c.closed {
return c.closeErr
}
c.closed = true
c.closeErr = c.Conn.Close()
c.closeOnce.Do(func() {
for _, addon := range c.proxy.Addons { for _, addon := range c.proxy.Addons {
addon.ClientDisconnected(c.connCtx.Client) addon.ClientDisconnected(c.connCtx.Client)
} }
})
return c.Conn.Close() if c.connCtx.Server != nil && c.connCtx.Server.Conn != nil {
c.connCtx.Server.Conn.Close()
}
return c.closeErr
}
type serverConn struct {
net.Conn
proxy *Proxy
connCtx *flow.ConnContext
closed bool
closeErr error
}
func (c *serverConn) Close() error {
log.Debugln("in http serverConn close")
if c.closed {
return c.closeErr
}
c.closed = true
c.closeErr = c.Conn.Close()
for _, addon := range c.proxy.Addons {
addon.ServerDisconnected(c.connCtx)
}
c.connCtx.Client.Conn.Close()
return c.closeErr
} }
func NewProxy(opts *Options) (*Proxy, error) { func NewProxy(opts *Options) (*Proxy, error) {
@ -246,7 +280,21 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
} }
f.ConnContext.InitHttpServer(proxy.Opts.SslInsecure) f.ConnContext.InitHttpServer(
proxy.Opts.SslInsecure,
func(c net.Conn) net.Conn {
return &serverConn{
Conn: c,
proxy: proxy,
connCtx: f.ConnContext,
}
},
func() {
for _, addon := range proxy.Addons {
addon.ServerConnected(f.ConnContext)
}
},
)
proxyRes, err := f.ConnContext.Server.Client.Do(proxyReq) proxyRes, err := f.ConnContext.Server.Client.Do(proxyReq)
if err != nil { if err != nil {

Loading…
Cancel
Save