proxy

package
v0.0.0-...-4a59956 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FingerprintDir string

Functions

func GetFingerprintDir

func GetFingerprintDir() string

func ListFingerprints

func ListFingerprints() ([]string, error)

func NewUtlsConn

func NewUtlsConn(conn net.Conn, opts *Options, clientHello *tls.ClientHelloInfo) (*utls.UConn, error)

NewUtlsConn creates and configures a utls.UConn based on the proxy options and client hello info. It handles standard fingerprints, "client" mirroring, and saved profiles.

func SaveFingerprint

func SaveFingerprint(name string, fp *Fingerprint) error

func UtlsStateToTlsState

func UtlsStateToTlsState(state utls.ConnectionState) *tls.ConnectionState

Helper to convert utls state to standard tls state

Types

type Addon

type Addon interface {
	// A client has connected to mitmproxy. Note that a connection can correspond to multiple HTTP requests.
	ClientConnected(*ClientConn)

	// A client connection has been closed (either by us or the client).
	ClientDisconnected(*ClientConn)

	// Mitmproxy has connected to a server.
	ServerConnected(*ConnContext)

	// A server connection has been closed (either by us or the server).
	ServerDisconnected(*ConnContext)

	// The TLS handshake with the server has been completed successfully.
	TlsEstablishedServer(*ConnContext)

	// HTTP request headers were successfully read. At this point, the body is empty.
	Requestheaders(*Flow)

	// The full HTTP request has been read.
	Request(*Flow)

	// HTTP response headers were successfully read. At this point, the body is empty.
	Responseheaders(*Flow)

	// The full HTTP response has been read.
	Response(*Flow)

	// Stream request body modifier
	StreamRequestModifier(*Flow, io.Reader) io.Reader

	// Stream response body modifier
	StreamResponseModifier(*Flow, io.Reader) io.Reader

	// onAccessProxyServer
	AccessProxyServer(req *http.Request, res http.ResponseWriter)

	// WebSocket connection established (handshake complete)
	WebsocketHandshake(*Flow)

	// WebSocket message received from client
	WebsocketMessage(*Flow, *WebSocketMessage)
}

type BaseAddon

type BaseAddon struct{}

BaseAddon do nothing

func (*BaseAddon) AccessProxyServer

func (addon *BaseAddon) AccessProxyServer(req *http.Request, res http.ResponseWriter)

func (*BaseAddon) ClientConnected

func (addon *BaseAddon) ClientConnected(*ClientConn)

func (*BaseAddon) ClientDisconnected

func (addon *BaseAddon) ClientDisconnected(*ClientConn)

func (*BaseAddon) Request

func (addon *BaseAddon) Request(*Flow)

func (*BaseAddon) Requestheaders

func (addon *BaseAddon) Requestheaders(*Flow)

func (*BaseAddon) Response

func (addon *BaseAddon) Response(*Flow)

func (*BaseAddon) Responseheaders

func (addon *BaseAddon) Responseheaders(*Flow)

func (*BaseAddon) ServerConnected

func (addon *BaseAddon) ServerConnected(*ConnContext)

func (*BaseAddon) ServerDisconnected

func (addon *BaseAddon) ServerDisconnected(*ConnContext)

func (*BaseAddon) StreamRequestModifier

func (addon *BaseAddon) StreamRequestModifier(f *Flow, in io.Reader) io.Reader

func (*BaseAddon) StreamResponseModifier

func (addon *BaseAddon) StreamResponseModifier(f *Flow, in io.Reader) io.Reader

func (*BaseAddon) TlsEstablishedServer

func (addon *BaseAddon) TlsEstablishedServer(*ConnContext)

func (*BaseAddon) WebsocketHandshake

func (addon *BaseAddon) WebsocketHandshake(f *Flow)

func (*BaseAddon) WebsocketMessage

func (addon *BaseAddon) WebsocketMessage(f *Flow, msg *WebSocketMessage)

type ClientConn

type ClientConn struct {
	Id                 uuid.UUID
	Conn               net.Conn
	Tls                bool
	NegotiatedProtocol string
	UpstreamCert       bool // Connect to upstream server to look up certificate details. Default: True
	// contains filtered or unexported fields
}

client connection

func (*ClientConn) MarshalJSON

func (c *ClientConn) MarshalJSON() ([]byte, error)

type ConnContext

type ConnContext struct {
	ClientConn *ClientConn   `json:"clientConn"`
	ServerConn *ServerConn   `json:"serverConn"`
	Intercept  bool          `json:"intercept"` // Indicates whether to parse HTTPS
	FlowCount  atomic.Uint32 `json:"-"`         // Number of HTTP requests made on the same connection
	// contains filtered or unexported fields
}

connection context

func (*ConnContext) Id

func (connCtx *ConnContext) Id() uuid.UUID

type Fingerprint

type Fingerprint struct {
	Name              string   `json:"name"`
	CipherSuites      []uint16 `json:"cipher_suites"`
	SupportedVersions []uint16 `json:"supported_versions"`
	SupportedCurves   []uint16 `json:"supported_curves"`
	SupportedPoints   []uint8  `json:"supported_points"`
	SignatureSchemes  []uint16 `json:"signature_schemes"`
	ALPNProtocols     []string `json:"alpn_protocols"`
}

func LoadFingerprint

func LoadFingerprint(name string) (*Fingerprint, error)

func NewFingerprintFromClientHello

func NewFingerprintFromClientHello(name string, info *tls.ClientHelloInfo) *Fingerprint

Convert tls.ClientHelloInfo to Fingerprint struct

func (*Fingerprint) ToSpec

func (fp *Fingerprint) ToSpec() *utls.ClientHelloSpec

Convert Fingerprint to utls.ClientHelloSpec

type Flow

type Flow struct {
	Id          uuid.UUID
	ConnContext *ConnContext
	Request     *Request
	Response    *Response

	// https://docs.mitmproxy.org/stable/overview-features/#streaming
	// 如果为 true,则不缓冲 Request.Body 和 Response.Body,且不进入之后的 Addon.Request 和 Addon.Response
	Stream            bool
	UseSeparateClient bool // use separate http client to send http request

	// Metadata to pass data between addons. Not persisted by default unless handled by storage addon.
	Metadata map[string]interface{}
	// contains filtered or unexported fields
}

flow

func NewFlow

func NewFlow() *Flow

func (*Flow) Done

func (f *Flow) Done() <-chan struct{}

func (*Flow) Finish

func (f *Flow) Finish()

func (*Flow) MarshalJSON

func (f *Flow) MarshalJSON() ([]byte, error)

type InstanceLogAddon

type InstanceLogAddon struct {
	BaseAddon
	// contains filtered or unexported fields
}

InstanceLogAddon logs with instance identification

func NewInstanceLogAddonWithFile

func NewInstanceLogAddonWithFile(addr string, instanceName string, logFilePath string) *InstanceLogAddon

NewInstanceLogAddonWithFile creates a new instance-aware log addon with file output

func (*InstanceLogAddon) ClientConnected

func (addon *InstanceLogAddon) ClientConnected(client *ClientConn)

func (*InstanceLogAddon) ClientDisconnected

func (addon *InstanceLogAddon) ClientDisconnected(client *ClientConn)

func (*InstanceLogAddon) Request

func (addon *InstanceLogAddon) Request(f *Flow)

func (*InstanceLogAddon) Requestheaders

func (addon *InstanceLogAddon) Requestheaders(f *Flow)

func (*InstanceLogAddon) Response

func (addon *InstanceLogAddon) Response(f *Flow)

func (*InstanceLogAddon) ServerConnected

func (addon *InstanceLogAddon) ServerConnected(connCtx *ConnContext)

func (*InstanceLogAddon) ServerDisconnected

func (addon *InstanceLogAddon) ServerDisconnected(connCtx *ConnContext)

func (*InstanceLogAddon) SetLogger

func (addon *InstanceLogAddon) SetLogger(logger *InstanceLogger)

SetLogger allows setting a custom instance logger

func (*InstanceLogAddon) TlsEstablishedServer

func (addon *InstanceLogAddon) TlsEstablishedServer(connCtx *ConnContext)

type InstanceLogger

type InstanceLogger struct {
	InstanceID   string
	InstanceName string
	Port         string
	LogFilePath  string
	// contains filtered or unexported fields
}

func NewInstanceLogger

func NewInstanceLogger(addr string, instanceName string) *InstanceLogger

NewInstanceLogger creates a logger with instance identification

func NewInstanceLoggerWithFile

func NewInstanceLoggerWithFile(addr string, instanceName string, logFilePath string) *InstanceLogger

NewInstanceLoggerWithFile creates a logger with instance identification and optional file output

func (*InstanceLogger) Debug

func (il *InstanceLogger) Debug(args ...interface{})

Debug logs at debug level

func (*InstanceLogger) Debugf

func (il *InstanceLogger) Debugf(format string, args ...interface{})

Debugf logs formatted at debug level

func (*InstanceLogger) Error

func (il *InstanceLogger) Error(args ...interface{})

Error logs at error level

func (*InstanceLogger) Errorf

func (il *InstanceLogger) Errorf(format string, args ...interface{})

Errorf logs formatted at error level

func (*InstanceLogger) Fatal

func (il *InstanceLogger) Fatal(args ...interface{})

Fatal logs at fatal level

func (*InstanceLogger) Fatalf

func (il *InstanceLogger) Fatalf(format string, args ...interface{})

Fatalf logs formatted at fatal level

func (*InstanceLogger) GetEntry

func (il *InstanceLogger) GetEntry() *log.Entry

GetEntry returns the underlying logrus entry

func (*InstanceLogger) Info

func (il *InstanceLogger) Info(args ...interface{})

Info logs at info level

func (*InstanceLogger) Infof

func (il *InstanceLogger) Infof(format string, args ...interface{})

Infof logs formatted at info level

func (*InstanceLogger) Warn

func (il *InstanceLogger) Warn(args ...interface{})

Warn logs at warn level

func (*InstanceLogger) Warnf

func (il *InstanceLogger) Warnf(format string, args ...interface{})

Warnf logs formatted at warn level

func (*InstanceLogger) WithFields

func (il *InstanceLogger) WithFields(fields log.Fields) *log.Entry

WithFields adds additional fields to the logger

type LogAddon

type LogAddon struct {
	BaseAddon
}

LogAddon log connection and flow

func (*LogAddon) ClientConnected

func (addon *LogAddon) ClientConnected(client *ClientConn)

func (*LogAddon) ClientDisconnected

func (addon *LogAddon) ClientDisconnected(client *ClientConn)

func (*LogAddon) Requestheaders

func (addon *LogAddon) Requestheaders(f *Flow)

func (*LogAddon) ServerConnected

func (addon *LogAddon) ServerConnected(connCtx *ConnContext)

func (*LogAddon) ServerDisconnected

func (addon *LogAddon) ServerDisconnected(connCtx *ConnContext)

func (*LogAddon) WebsocketHandshake

func (addon *LogAddon) WebsocketHandshake(f *Flow)

func (*LogAddon) WebsocketMessage

func (addon *LogAddon) WebsocketMessage(f *Flow, msg *WebSocketMessage)

type Options

type Options struct {
	Debug             int
	Addr              string
	StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式
	SslInsecure       bool
	CaRootPath        string
	NewCaFunc         func() (cert.CA, error) //创建 Ca 的函数
	Upstream          string
	LogFilePath       string // Path to write logs to file
	TlsFingerprint    string // TLS fingerprint to emulate (chrome, firefox, ios, or random)
	FingerprintSave   string // Save decoding client hello to file
}

type Proxy

type Proxy struct {
	Opts    *Options
	Version string
	Addons  []Addon
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(opts *Options) (*Proxy, error)

func (*Proxy) AddAddon

func (proxy *Proxy) AddAddon(addon Addon)

func (*Proxy) Addr

func (proxy *Proxy) Addr() string

func (*Proxy) Close

func (proxy *Proxy) Close() error

func (*Proxy) GetCertificate

func (proxy *Proxy) GetCertificate() x509.Certificate

func (*Proxy) GetCertificateByCN

func (proxy *Proxy) GetCertificateByCN(commonName string) (*tls.Certificate, error)

func (*Proxy) SetAuthProxy

func (proxy *Proxy) SetAuthProxy(fn func(res http.ResponseWriter, req *http.Request) (bool, error))

func (*Proxy) SetShouldInterceptRule

func (proxy *Proxy) SetShouldInterceptRule(rule func(req *http.Request) bool)

func (*Proxy) SetUpstreamProxy

func (proxy *Proxy) SetUpstreamProxy(fn func(req *http.Request) (*url.URL, error))

func (*Proxy) Shutdown

func (proxy *Proxy) Shutdown(ctx context.Context) error

func (*Proxy) Start

func (proxy *Proxy) Start() error

type Request

type Request struct {
	Method string
	URL    *url.URL
	Proto  string
	Header http.Header
	Body   []byte
	// contains filtered or unexported fields
}

flow http request

func NewRequest

func NewRequest(req *http.Request) *Request

func (*Request) DecodedBody

func (req *Request) DecodedBody() ([]byte, error)

func (*Request) MarshalJSON

func (req *Request) MarshalJSON() ([]byte, error)

func (*Request) Raw

func (r *Request) Raw() *http.Request

func (*Request) SetRaw

func (r *Request) SetRaw(req *http.Request)

func (*Request) UnmarshalJSON

func (req *Request) UnmarshalJSON(data []byte) error

type Response

type Response struct {
	StatusCode int         `json:"statusCode"`
	Header     http.Header `json:"header"`
	Body       []byte      `json:"-"`
	BodyReader io.Reader
	// contains filtered or unexported fields
}

flow http response

func (*Response) DecodedBody

func (r *Response) DecodedBody() ([]byte, error)

func (*Response) IsTextContentType

func (r *Response) IsTextContentType() bool

func (*Response) ReplaceToDecodedBody

func (r *Response) ReplaceToDecodedBody()

type ServerConn

type ServerConn struct {
	Id      uuid.UUID
	Address string
	Conn    net.Conn
	// contains filtered or unexported fields
}

server connection

func (*ServerConn) MarshalJSON

func (c *ServerConn) MarshalJSON() ([]byte, error)

func (*ServerConn) TlsState

func (c *ServerConn) TlsState() *tls.ConnectionState

type UpstreamCertAddon

type UpstreamCertAddon struct {
	BaseAddon
	UpstreamCert bool // Connect to upstream server to look up certificate details.
}

func NewUpstreamCertAddon

func NewUpstreamCertAddon(upstreamCert bool) *UpstreamCertAddon

func (*UpstreamCertAddon) ClientConnected

func (addon *UpstreamCertAddon) ClientConnected(conn *ClientConn)

type WebSocketMessage

type WebSocketMessage struct {
	Type       int
	Data       []byte
	FromClient bool // true if message is from client, false if from server
}

WebSocketMessage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL