martian

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MPL-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package martian provides an HTTP/1.1 proxy with an API for configurable request and response modifiers.

Index

Constants

This section is empty.

Variables

View Source
var ErrConnectFallback = errors.New("martian: connect fallback")

ErrConnectFallback is returned by a ConnectFunc to indicate that the CONNECT request should be handled by martian.

Functions

func ContextDuration added in v1.2.0

func ContextDuration(ctx context.Context) time.Duration

func ContextTraceID added in v1.2.0

func ContextTraceID(ctx context.Context) string

Types

type ConnectFunc added in v1.1.0

type ConnectFunc func(req *http.Request) (*http.Response, io.ReadWriteCloser, error)

ConnectFunc dials a network connection for a CONNECT request. If the returned net.Conn is not nil, the response must be not nil.

type ErrorStatus added in v1.2.0

type ErrorStatus struct {
	Err    error
	Status int
}

func (ErrorStatus) Error added in v1.2.0

func (e ErrorStatus) Error() string

func (ErrorStatus) Unwrap added in v1.2.0

func (e ErrorStatus) Unwrap() error

type Proxy

type Proxy struct {
	RequestModifier
	ResponseModifier
	Trace *ProxyTrace

	// RoundTripper specifies the round tripper to use for requests.
	RoundTripper http.RoundTripper

	// DialContext specifies the dial function for creating unencrypted TCP connections.
	// If not set and the RoundTripper is an *http.Transport, the Transport's DialContext is used.
	DialContext func(context.Context, string, string) (net.Conn, error)

	// ProxyURL specifies the upstream proxy to use for requests.
	// If not set and the RoundTripper is an *http.Transport, the Transport's ProxyURL is used.
	ProxyURL func(*http.Request) (*url.URL, error)

	// AllowHTTP disables automatic HTTP to HTTPS upgrades when the listener is TLS.
	AllowHTTP bool

	// RequestIDHeader specifies a special header name that the proxy will use to identify requests.
	// If the header is present in the request, the proxy will associate the value with the request in the logs.
	// If empty, no action is taken, and the proxy will generate a new request ID.
	RequestIDHeader string

	// ConnectRequestModifier modifies CONNECT requests to upstream proxy.
	// If ConnectPassthrough is enabled, this is ignored.
	ConnectRequestModifier func(*http.Request) error

	// ConnectFunc specifies a function to dial network connections for CONNECT requests.
	// Implementations can return ErrConnectFallback to indicate that the CONNECT request should be handled by martian.
	ConnectFunc ConnectFunc

	// ConnectTimeout specifies the maximum amount of time to connect to upstream before cancelling request.
	ConnectTimeout time.Duration

	// MITMConfig is config to use for MITMing of CONNECT requests.
	MITMConfig *mitm.Config

	// MITMFilter specifies a function to determine whether a CONNECT request should be MITMed.
	MITMFilter func(*http.Request) bool

	// MITMTLSHandshakeTimeout specifies the maximum amount of time to wait for a TLS handshake for a MITMed connection.
	// Zero means no timeout.
	MITMTLSHandshakeTimeout time.Duration

	// WithoutWarning disables the warning header added to requests and responses when modifier errors occur.
	WithoutWarning bool

	// ErrorResponse specifies a custom error HTTP response to send when a proxying error occurs.
	ErrorResponse func(req *http.Request, err error) *http.Response

	// IdleTimeout is the maximum amount of time to wait for the
	// next request. If IdleTimeout is zero, the value of ReadTimeout is used.
	// If both are zero, there is no timeout.
	IdleTimeout time.Duration

	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body. A zero or negative value means
	// there will be no timeout.
	//
	// Because ReadTimeout does not let Handlers make per-request
	// decisions on each request body's acceptable deadline or
	// upload rate, most users will prefer to use
	// ReadHeaderTimeout. It is valid to use them both.
	ReadTimeout time.Duration

	// ReadHeaderTimeout is the amount of time allowed to read
	// request headers. The connection's read deadline is reset
	// after reading the headers and the Handler can decide what
	// is considered too slow for the body. If ReadHeaderTimeout
	// is zero, the value of ReadTimeout is used. If both are
	// zero, there is no timeout.
	ReadHeaderTimeout time.Duration

	// WriteTimeout is the maximum duration before timing out
	// writes of the response. It is reset whenever a new
	// request's header is read. Like ReadTimeout, it does not
	// let Handlers make decisions on a per-request basis.
	// A zero or negative value means there will be no timeout.
	WriteTimeout time.Duration

	// BaseContex is the base context for all requests.
	BaseContex context.Context //nolint:containedctx // It's intended to be used as a base context.

	// TestingSkipRoundTrip skips the round trip for requests and returns a 200 OK response.
	TestingSkipRoundTrip bool
	// contains filtered or unexported fields
}

Proxy is an HTTP proxy with support for TLS MITM and customizable behavior.

func (*Proxy) Close

func (p *Proxy) Close()

Close sets the proxy to the closing state so it stops receiving new connections, finishes processing any inflight requests, and closes existing connections without reading anymore requests from them.

func (*Proxy) Handler

func (p *Proxy) Handler() http.Handler

Handler returns proxy as http.Handler, see [proxyHandler] for details.

func (*Proxy) Serve

func (p *Proxy) Serve(l net.Listener) error

Serve accepts connections from the listener and handles the requests.

type ProxyTrace added in v1.2.0

type ProxyTrace struct {
	// ReadRequest is called with the result of reading the request.
	// It is called after the request has been read.
	ReadRequest func(ReadRequestInfo)

	// WroteResponse is called with the result of writing the response.
	// It is called after the response has been written.
	WroteResponse func(WroteResponseInfo)
}

ProxyTrace is a set of hooks to run at various stages of a request. Any particular hook may be nil. Functions may be called concurrently from different goroutines and some may be called after the request has completed or failed.

type ReadRequestInfo added in v1.2.0

type ReadRequestInfo struct {
	// Req is the request that was read.
	Req *http.Request
	// Err is any error encountered while reading the Request.
	Err error
}

type RequestModifier

type RequestModifier interface {
	// ModifyRequest modifies the request.
	ModifyRequest(req *http.Request) error
}

RequestModifier is an interface that defines a request modifier that can be used by a proxy.

type RequestModifierFunc

type RequestModifierFunc func(req *http.Request) error

RequestModifierFunc is an adapter for using a function with the given signature as a RequestModifier.

func (RequestModifierFunc) ModifyRequest

func (f RequestModifierFunc) ModifyRequest(req *http.Request) error

ModifyRequest modifies the request using the given function.

type RequestResponseModifier

type RequestResponseModifier interface {
	RequestModifier
	ResponseModifier
}

RequestResponseModifier is an interface that is both a ResponseModifier and a RequestModifier.

type ResponseModifier

type ResponseModifier interface {
	// ModifyResponse modifies the response.
	ModifyResponse(res *http.Response) error
}

ResponseModifier is an interface that defines a response modifier that can be used by a proxy.

type ResponseModifierFunc

type ResponseModifierFunc func(res *http.Response) error

ResponseModifierFunc is an adapter for using a function with the given signature as a ResponseModifier.

func (ResponseModifierFunc) ModifyResponse

func (f ResponseModifierFunc) ModifyResponse(res *http.Response) error

ModifyResponse modifies the response using the given function.

type TraceIDPrependingLogger added in v1.2.0

type TraceIDPrependingLogger struct {
	log.Logger
}

func (TraceIDPrependingLogger) Debugf added in v1.2.0

func (l TraceIDPrependingLogger) Debugf(ctx context.Context, format string, args ...any)

func (TraceIDPrependingLogger) Errorf added in v1.2.0

func (l TraceIDPrependingLogger) Errorf(ctx context.Context, format string, args ...any)

func (TraceIDPrependingLogger) Infof added in v1.2.0

func (l TraceIDPrependingLogger) Infof(ctx context.Context, format string, args ...any)

type WroteResponseInfo added in v1.2.0

type WroteResponseInfo struct {
	// Res is the response that was written.
	Res *http.Response
	// Err is any error encountered while writing the Request.
	Err error
}

Directories

Path Synopsis
Package cybervillains provides the publically published Selenium project CyberVillains certificate and key.
Package cybervillains provides the publically published Selenium project CyberVillains certificate and key.
Package fifo provides Group, which is a list of modifiers that are executed consecutively.
Package fifo provides Group, which is a list of modifiers that are executed consecutively.
h2
Package h2 contains basic HTTP/2 handling for Martian.
Package h2 contains basic HTTP/2 handling for Martian.
grpc
Package grpc contains gRPC functionality for Martian proxy.
Package grpc contains gRPC functionality for Martian proxy.
testing
Package testing contains a test fixture for working with gRPC over HTTP/2.
Package testing contains a test fixture for working with gRPC over HTTP/2.
Package httpspec provides a modifier stack that has been preconfigured to provide spec-compliant HTTP proxy behavior.
Package httpspec provides a modifier stack that has been preconfigured to provide spec-compliant HTTP proxy behavior.
Package martiantest provides helper utilities for testing modifiers.
Package martiantest provides helper utilities for testing modifiers.
Package messageview provides no-op snapshots for HTTP requests and responses.
Package messageview provides no-op snapshots for HTTP requests and responses.
Package mitm provides tooling for MITMing TLS connections.
Package mitm provides tooling for MITMing TLS connections.
Package proxyutil provides functionality for building proxies.
Package proxyutil provides functionality for building proxies.

Jump to

Keyboard shortcuts

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