add ClientConnected hook

addon-dailer
lqqyt2423 2 years ago
parent 959568b822
commit dee64ddddf

@ -3,6 +3,7 @@ package addon
import ( import (
"time" "time"
"github.com/lqqyt2423/go-mitmproxy/connection"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
_log "github.com/sirupsen/logrus" _log "github.com/sirupsen/logrus"
) )
@ -10,6 +11,9 @@ import (
var log = _log.WithField("at", "addon") var log = _log.WithField("at", "addon")
type Addon interface { type Addon interface {
// A client has connected to mitmproxy. Note that a connection can correspond to multiple HTTP requests.
ClientConnected(*connection.Client)
// 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)
@ -26,6 +30,8 @@ type Addon interface {
// Base do nothing // Base do nothing
type Base struct{} type Base struct{}
func (addon *Base) ClientConnected(*connection.Client) {}
func (addon *Base) Requestheaders(*flow.Flow) {} func (addon *Base) Requestheaders(*flow.Flow) {}
func (addon *Base) Request(*flow.Flow) {} func (addon *Base) Request(*flow.Flow) {}
func (addon *Base) Responseheaders(*flow.Flow) {} func (addon *Base) Responseheaders(*flow.Flow) {}
@ -36,6 +42,10 @@ type Log struct {
Base Base
} }
func (addon *Log) ClientConnected(client *connection.Client) {
log.Infof("%v client connect\n", client.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()

@ -0,0 +1,19 @@
package connection
import (
"net"
uuid "github.com/satori/go.uuid"
)
type Client struct {
Id uuid.UUID
Conn net.Conn
}
func NewClient(c net.Conn) *Client {
return &Client{
Id: uuid.NewV4(),
Conn: c,
}
}

@ -2,6 +2,7 @@ package proxy
import ( import (
"bytes" "bytes"
"context"
"crypto/tls" "crypto/tls"
"io" "io"
"net" "net"
@ -9,6 +10,7 @@ import (
"time" "time"
"github.com/lqqyt2423/go-mitmproxy/addon" "github.com/lqqyt2423/go-mitmproxy/addon"
"github.com/lqqyt2423/go-mitmproxy/connection"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
_log "github.com/sirupsen/logrus" _log "github.com/sirupsen/logrus"
) )
@ -39,6 +41,13 @@ func NewProxy(opts *Options) (*Proxy, error) {
Addr: opts.Addr, Addr: opts.Addr,
Handler: proxy, Handler: proxy,
IdleTimeout: 5 * time.Second, IdleTimeout: 5 * time.Second,
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
client := connection.NewClient(c)
for _, addon := range proxy.Addons {
addon.ClientConnected(client)
}
return ctx
},
} }
proxy.Client = &http.Client{ proxy.Client = &http.Client{

Loading…
Cancel
Save