types

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addon

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

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

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

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

	// The TLS handshake with the server has been completed successfully.
	TLSEstablishedServer(*conn.Context)

	// 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)
}

Addon defines the interface for proxy addons.

type AddonNotifier

type AddonNotifier interface {
	NotifyClientDisconnected(client *conn.ClientConn)
	NotifyServerDisconnected(connCtx *conn.Context)
}

AddonNotifier defines the interface for notifying addons about connection events. This is used by the internal conn package to notify about disconnections.

type AddonRegistry

type AddonRegistry interface {
	Get() []Addon
}

AddonRegistry manages a collection of addons.

type BaseAddon

type BaseAddon struct{}

BaseAddon provides default no-op implementations of all Addon methods.

func (*BaseAddon) AccessProxyServer

func (*BaseAddon) AccessProxyServer(_ *http.Request, _ http.ResponseWriter)

func (*BaseAddon) ClientConnected

func (*BaseAddon) ClientConnected(*conn.ClientConn)

func (*BaseAddon) ClientDisconnected

func (*BaseAddon) ClientDisconnected(*conn.ClientConn)

func (*BaseAddon) Request

func (*BaseAddon) Request(*Flow)

func (*BaseAddon) Requestheaders

func (*BaseAddon) Requestheaders(*Flow)

func (*BaseAddon) Response

func (*BaseAddon) Response(*Flow)

func (*BaseAddon) Responseheaders

func (*BaseAddon) Responseheaders(*Flow)

func (*BaseAddon) ServerConnected

func (*BaseAddon) ServerConnected(*conn.Context)

func (*BaseAddon) ServerDisconnected

func (*BaseAddon) ServerDisconnected(*conn.Context)

func (*BaseAddon) StreamRequestModifier

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

func (*BaseAddon) StreamResponseModifier

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

func (*BaseAddon) TLSEstablishedServer

func (*BaseAddon) TLSEstablishedServer(*conn.Context)

type ClientFactory

type ClientFactory interface {
	// CreateMainClient creates the main fallback/separate client.
	// Used when the request has been modified (different host/scheme) or when
	// UseSeparateClient is set. This client goes through the upstream proxy and supports
	// HTTP/2. It creates new connections rather than reusing existing ones.
	CreateMainClient(upstreamManager UpstreamManager, insecureSkipVerify bool) *http.Client

	// CreateHTTP2Client creates an HTTP/2 server connection client.
	// Created specifically for HTTP/2 connections when the negotiated protocol
	// is "h2". Uses http2.Transport and reuses the existing TLS connection
	// rather than creating new connections.
	CreateHTTP2Client(tlsConn *tls.Conn) *http.Client

	// CreatePlainHTTPClient creates a plain HTTP connection client.
	// Created for plain HTTP (non-TLS) connections. Explicitly disables HTTP/2
	// and reuses the existing plain connection via custom DialContext function.
	// This avoids creating new connections for each request on the same HTTP connection.
	CreatePlainHTTPClient(conn net.Conn) *http.Client

	// CreateHTTPSClient creates an HTTPS/TLS connection client.
	// Created for HTTPS connections after TLS handshake. Reuses the established
	// TLS connection via custom DialTLSContext function and allows HTTP/2
	// negotiation. This maintains persistent connections to upstream servers.
	CreateHTTPSClient(tlsConn *tls.Conn) *http.Client
}

ClientFactory is responsible for creating HTTP clients for different scenarios. This allows external customization of client creation logic for patching and testing.

type DefaultClientFactory

type DefaultClientFactory struct{}

DefaultClientFactory is the default implementation of ClientFactory. It creates clients with the standard configuration used by the proxy.

func NewDefaultClientFactory

func NewDefaultClientFactory() *DefaultClientFactory

NewDefaultClientFactory creates a new DefaultClientFactory.

func (*DefaultClientFactory) CreateHTTP2Client

func (*DefaultClientFactory) CreateHTTP2Client(tlsConn *tls.Conn) *http.Client

CreateHTTP2Client implements ClientFactory.

func (*DefaultClientFactory) CreateHTTPSClient

func (*DefaultClientFactory) CreateHTTPSClient(tlsConn *tls.Conn) *http.Client

CreateHTTPSClient implements ClientFactory.

func (*DefaultClientFactory) CreateMainClient

func (*DefaultClientFactory) CreateMainClient(upstreamManager UpstreamManager, insecureSkipVerify bool) *http.Client

CreateMainClient implements ClientFactory.

func (*DefaultClientFactory) CreatePlainHTTPClient

func (*DefaultClientFactory) CreatePlainHTTPClient(conn net.Conn) *http.Client

CreatePlainHTTPClient implements ClientFactory.

type Flow

type Flow struct {
	ID          uuid.UUID
	ConnContext *conn.Context
	Request     *Request
	Response    *Response

	// https://docs.mitmproxy.org/stable/overview-features/#streaming
	// If true, Request.Body and Response.Body are not buffered, and will not enter subsequent Addon.Request and Addon.Response
	Stream            bool
	UseSeparateClient bool // use separate http client to send http request
	// contains filtered or unexported fields
}

Flow represents a complete HTTP request/response flow.

func NewFlow

func NewFlow() *Flow

NewFlow creates a new Flow instance.

func (*Flow) Done

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

Done returns a channel that is closed when the flow is finished.

func (*Flow) Finish

func (f *Flow) Finish()

Finish marks the flow as complete.

func (*Flow) MarshalJSON

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

type Request

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

Request represents an HTTP request in the proxy flow.

func NewRequest

func NewRequest(req *http.Request) *Request

NewRequest creates a new Request from an http.Request.

func (*Request) DecodedBody

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

DecodedBody returns the decoded request body.

func (*Request) MarshalJSON

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

func (*Request) Raw

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

Raw returns the underlying http.Request.

func (*Request) UnmarshalJSON

func (r *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

	Close bool // connection close
}

Response represents an HTTP response in the proxy flow.

func (*Response) DecodedBody

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

DecodedBody returns the decoded response body.

func (*Response) IsTextContentType

func (r *Response) IsTextContentType() bool

IsTextContentType checks if the response has a text content type.

func (*Response) ReplaceToDecodedBody

func (r *Response) ReplaceToDecodedBody()

ReplaceToDecodedBody replaces the body with the decoded version.

type UpstreamManager

type UpstreamManager interface {
	// RealUpstreamProxy returns a function that resolves upstream proxy for HTTP client transport.
	// This is used by the HTTP client to determine the proxy for each request.
	// The returned function extracts the original request from the context and uses it
	// to determine the appropriate proxy.
	RealUpstreamProxy() func(*http.Request) (*url.URL, error)
}

UpstreamManager defines the interface for managing upstream proxy connections. This interface allows external implementations to control how the proxy connects to upstream servers.

Jump to

Keyboard shortcuts

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