Documentation
¶
Overview ¶
Package martian provides an HTTP/1.1 proxy with an API for configurable request and response modifiers.
Index ¶
- Variables
- func ContextDuration(ctx context.Context) time.Duration
- func ContextTraceID(ctx context.Context) string
- func OnProxyConnectResponse(_ context.Context, _ *url.URL, req *http.Request, connectRes *http.Response) error
- type ConnectFunc
- type ErrorStatus
- type Proxy
- func (p *Proxy) Close() error
- func (p *Proxy) Connect(ctx context.Context, req *http.Request, terminateTLS bool) (res *http.Response, crw io.ReadWriteCloser, cerr error)
- func (p *Proxy) Handler() http.Handler
- func (p *Proxy) Serve(l net.Listener) error
- func (p *Proxy) Shutdown(ctx context.Context) error
- type ProxyTrace
- type ReadRequestInfo
- type RequestModifier
- type RequestModifierFunc
- type RequestResponseModifier
- type ResponseModifier
- type ResponseModifierFunc
- type TraceIDAppendingLogger
- func (l TraceIDAppendingLogger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l TraceIDAppendingLogger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l TraceIDAppendingLogger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l TraceIDAppendingLogger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l TraceIDAppendingLogger) With(args ...any) log.StructuredLogger
- type WroteResponseInfo
Constants ¶
This section is empty.
Variables ¶
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 ContextTraceID ¶ added in v1.2.0
Types ¶
type ConnectFunc ¶ added in v1.1.0
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
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
// 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
// TLSHandshakeTimeout is the maximum amount of time to wait for a TLS handshake.
// The proxy will try to cast accepted connections to tls.Conn and perform a handshake.
// If TLSHandshakeTimeout is zero, no timeout is set.
TLSHandshakeTimeout 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
// BaseContext is the base context for all requests.
BaseContext 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.
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 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 ¶
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 ¶
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 TraceIDAppendingLogger ¶ added in v1.5.0
type TraceIDAppendingLogger struct {
// contains filtered or unexported fields
}
func NewTraceIDAppendingLogger ¶ added in v1.5.0
func NewTraceIDAppendingLogger(sl log.StructuredLogger) TraceIDAppendingLogger
func (TraceIDAppendingLogger) DebugContext ¶ added in v1.5.0
func (l TraceIDAppendingLogger) DebugContext(ctx context.Context, msg string, args ...any)
func (TraceIDAppendingLogger) ErrorContext ¶ added in v1.5.0
func (l TraceIDAppendingLogger) ErrorContext(ctx context.Context, msg string, args ...any)
func (TraceIDAppendingLogger) InfoContext ¶ added in v1.5.0
func (l TraceIDAppendingLogger) InfoContext(ctx context.Context, msg string, args ...any)
func (TraceIDAppendingLogger) WarnContext ¶ added in v1.5.0
func (l TraceIDAppendingLogger) WarnContext(ctx context.Context, msg string, args ...any)
func (TraceIDAppendingLogger) With ¶ added in v1.5.0
func (l TraceIDAppendingLogger) With(args ...any) log.StructuredLogger
type WroteResponseInfo ¶ added in v1.2.0
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
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. |
|
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. |