add flow.ConnContext

addon-dailer
lqqyt2423 2 years ago
parent 9d312a5739
commit d2659e454c

@ -67,6 +67,6 @@ func (addon *Log) Requestheaders(f *flow.Flow) {
if f.Response != nil && f.Response.Body != nil { if f.Response != nil && f.Response.Body != nil {
contentLen = len(f.Response.Body) contentLen = len(f.Response.Body)
} }
log.Infof("%v %v %v %v - %v ms\n", f.Request.Method, f.Request.URL.String(), StatusCode, contentLen, time.Since(start).Milliseconds()) log.Infof("%v %v %v %v %v - %v ms\n", f.ConnContext.Client.Conn.RemoteAddr(), f.Request.Method, f.Request.URL.String(), StatusCode, contentLen, time.Since(start).Milliseconds())
}() }()
} }

@ -0,0 +1,9 @@
package flow
import "github.com/lqqyt2423/go-mitmproxy/connection"
type ConnContext struct {
Client *connection.Client
}
var ConnContextKey = new(struct{})

@ -112,7 +112,8 @@ type Flow struct {
Stream bool Stream bool
done chan struct{} done chan struct{}
Id uuid.UUID Id uuid.UUID
ConnContext *ConnContext
} }
func (f *Flow) MarshalJSON() ([]byte, error) { func (f *Flow) MarshalJSON() ([]byte, error) {

@ -2,6 +2,7 @@ package proxy
import ( import (
"bufio" "bufio"
"context"
"crypto/tls" "crypto/tls"
"net" "net"
"net/http" "net/http"
@ -9,6 +10,7 @@ import (
"time" "time"
"github.com/lqqyt2423/go-mitmproxy/cert" "github.com/lqqyt2423/go-mitmproxy/cert"
"github.com/lqqyt2423/go-mitmproxy/flow"
) )
// 模拟了标准库中 server 运行,目的是仅通过当前进程内存转发 socket 数据,不需要经过 tcp 或 unix socket // 模拟了标准库中 server 运行,目的是仅通过当前进程内存转发 socket 数据,不需要经过 tcp 或 unix socket
@ -39,17 +41,19 @@ func newPipes(req *http.Request) (net.Conn, *connBuf) {
// add Peek method for conn // add Peek method for conn
type connBuf struct { type connBuf struct {
net.Conn net.Conn
r *bufio.Reader r *bufio.Reader
host string host string
remoteAddr string remoteAddr string
connContext *flow.ConnContext
} }
func newConnBuf(c net.Conn, req *http.Request) *connBuf { func newConnBuf(c net.Conn, req *http.Request) *connBuf {
return &connBuf{ return &connBuf{
Conn: c, Conn: c,
r: bufio.NewReader(c), r: bufio.NewReader(c),
host: req.Host, host: req.Host,
remoteAddr: req.RemoteAddr, remoteAddr: req.RemoteAddr,
connContext: req.Context().Value(flow.ConnContextKey).(*flow.ConnContext),
} }
} }
@ -85,8 +89,13 @@ func NewMiddle(proxy *Proxy, caPath string) (Interceptor, error) {
} }
server := &http.Server{ server := &http.Server{
Handler: m, Handler: m,
IdleTimeout: 5 * time.Second, IdleTimeout: 5 * time.Second,
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, flow.ConnContextKey, c.(*tls.Conn).NetConn().(*connBuf).connContext)
},
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // disable http2 TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // disable http2
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) { GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {

@ -2,6 +2,7 @@ package proxy
import ( import (
"bytes" "bytes"
"context"
"crypto/tls" "crypto/tls"
"io" "io"
"net" "net"
@ -31,11 +32,7 @@ type Proxy struct {
StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式
Addons []addon.Addon Addons []addon.Addon
activeConn map[net.Conn]*proxyContext activeConn map[net.Conn]*flow.ConnContext
}
type proxyContext struct {
client *connection.Client
} }
func NewProxy(opts *Options) (*Proxy, error) { func NewProxy(opts *Options) (*Proxy, error) {
@ -46,13 +43,19 @@ 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)
connCtx := &flow.ConnContext{
Client: client,
}
proxy.activeConn[c] = connCtx
return context.WithValue(ctx, flow.ConnContextKey, connCtx)
},
ConnState: func(c net.Conn, cs http.ConnState) { ConnState: func(c net.Conn, cs http.ConnState) {
if cs == http.StateNew { if cs == http.StateNew {
client := connection.NewClient(c) client := proxy.activeConn[c].Client
proxy.activeConn[c] = &proxyContext{
client,
}
for _, addon := range proxy.Addons { for _, addon := range proxy.Addons {
addon.ClientConnected(client) addon.ClientConnected(client)
} }
@ -101,7 +104,7 @@ func NewProxy(opts *Options) (*Proxy, error) {
proxy.Addons = make([]addon.Addon, 0) proxy.Addons = make([]addon.Addon, 0)
proxy.activeConn = make(map[net.Conn]*proxyContext) proxy.activeConn = make(map[net.Conn]*flow.ConnContext)
return proxy, nil return proxy, nil
} }
@ -183,6 +186,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
f := flow.NewFlow() f := flow.NewFlow()
f.Request = flow.NewRequest(req) f.Request = flow.NewRequest(req)
f.ConnContext = req.Context().Value(flow.ConnContextKey).(*flow.ConnContext)
defer f.Finish() defer f.Finish()
// trigger addon event Requestheaders // trigger addon event Requestheaders
@ -323,7 +327,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
} }
func (proxy *Proxy) whenClientConnClose(c net.Conn) { func (proxy *Proxy) whenClientConnClose(c net.Conn) {
client := proxy.activeConn[c].client client := proxy.activeConn[c].Client
for _, addon := range proxy.Addons { for _, addon := range proxy.Addons {
addon.ClientDisconnected(client) addon.ClientDisconnected(client)
} }

Loading…
Cancel
Save