Documentation
¶
Index ¶
- Constants
- func GenerateCA(certOut, keyOut string) error
- func Response(status int, contentType string, body io.ReadCloser) *http.Response
- type CertManager
- type ErrorFunc
- type Handler
- func (h *Handler) CertManager() *CertManager
- func (h *Handler) ListenAndServe(ctx context.Context, addr string) error
- func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (h *Handler) SetCertManager(certMgr *CertManager)
- func (h *Handler) SetTunnelIdleTimeout(d time.Duration)
- func (h *Handler) SetUpstreamTransport(rt http.RoundTripper)
- func (h *Handler) TunnelIdleTimeout() time.Duration
- func (h *Handler) UpstreamTransport() http.RoundTripper
- func (h *Handler) UseError(fn ErrorFunc)
- func (h *Handler) UseHandshakeError(fn HandshakeErrorFunc)
- func (h *Handler) UseRequest(fn RequestFunc)
- func (h *Handler) UseResponse(fn ResponseFunc)
- type HandshakeErrorFunc
- type RequestFunc
- type ResponseFunc
Constants ¶
const DefaultTunnelIdleTimeout = 90 * time.Second
DefaultTunnelIdleTimeout bounds how long an intercepted tunnel may sit with no request in flight before the proxy closes it.
Variables ¶
This section is empty.
Functions ¶
func GenerateCA ¶
Types ¶
type CertManager ¶
type CertManager struct {
// contains filtered or unexported fields
}
func NewCertManager ¶
func NewCertManager(certFile, keyFile string) (*CertManager, error)
func (*CertManager) TLSConfig ¶
func (m *CertManager) TLSConfig() *tls.Config
TLSConfig returns the tls.Config used for every intercepted client connection. The same value is returned on every call, deliberately: a tls.Config owns the session ticket keys, so handing out a fresh config per connection leaves every client unable to decrypt the ticket it was issued and forces a full handshake every time. Callers must treat it as read-only.
type ErrorFunc ¶ added in v0.2.0
ErrorFunc is called when the upstream round trip fails, so no response middleware will run for req. It is the terminal callback for a request that passed the request middlewares but never produced a response.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is an http.Handler that acts as a forward proxy and performs TLS interception (MITM) on CONNECT tunnels when a CertManager is provided.
func New ¶
func New(certMgr *CertManager) *Handler
New creates a Handler. Providing a non-nil certMgr enables TLS interception; passing nil falls back to a transparent TCP relay for CONNECT tunnels.
The caller should set http.Server.BaseContext to a context that is canceled on shutdown so that long-lived CONNECT tunnels are torn down promptly when the server stops.
func (*Handler) CertManager ¶ added in v0.2.0
func (h *Handler) CertManager() *CertManager
CertManager returns the current CertManager in a thread-safe manner.
func (*Handler) ListenAndServe ¶
ListenAndServe starts an HTTP proxy server on the given network address. It uses ctx as the BaseContext for all incoming connections and shuts down gracefully when ctx is canceled.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements http.Handler. CONNECT requests initiate a tunnel; all other methods run request middlewares then are proxied via the reverse proxy.
func (*Handler) SetCertManager ¶ added in v0.2.0
func (h *Handler) SetCertManager(certMgr *CertManager)
SetCertManager replaces the active CertManager in a thread-safe manner.
func (*Handler) SetTunnelIdleTimeout ¶ added in v0.2.0
SetTunnelIdleTimeout bounds how long an intercepted tunnel may sit with no request in flight before the proxy closes it. It bounds only the wait for the next request; a request already arriving, however slowly, is not idle.
Zero disables the bound, which means a client that opens a tunnel and goes quiet holds its goroutines and buffers until it disconnects. Only tunnels opened after the call are affected.
func (*Handler) SetUpstreamTransport ¶ added in v0.2.0
func (h *Handler) SetUpstreamTransport(rt http.RoundTripper)
SetUpstreamTransport replaces the round tripper used for every proxied request, in a thread-safe manner.
The default is tuned for a proxy: one pooled transport for every host, with an idle timeout and a ceiling so a long-lived process does not accumulate connections it will never use again. Replace it to reach origins through another proxy, to trust a private certificate authority, or to observe upstream traffic in a test. Middlewares still run around whatever is set here; this replaces only the round trip itself.
It applies to plain HTTP requests as well as to intercepted tunnels, and takes effect on the next round trip rather than only on new tunnels.
func (*Handler) TunnelIdleTimeout ¶ added in v0.2.0
TunnelIdleTimeout returns how long an intercepted tunnel may sit idle, in a thread-safe manner.
func (*Handler) UpstreamTransport ¶ added in v0.2.0
func (h *Handler) UpstreamTransport() http.RoundTripper
UpstreamTransport returns the round tripper used for every proxied request, in a thread-safe manner.
func (*Handler) UseError ¶ added in v0.2.0
UseError registers fn as an upstream error middleware. Middlewares are called in registration order whenever an upstream round trip fails. Every request that reaches the upstream is followed by exactly one of the response middlewares or the error middlewares, so state keyed on the request can always be released.
func (*Handler) UseHandshakeError ¶ added in v0.2.0
func (h *Handler) UseHandshakeError(fn HandshakeErrorFunc)
UseHandshakeError registers fn as a handshake error middleware callback.
func (*Handler) UseRequest ¶
func (h *Handler) UseRequest(fn RequestFunc)
UseRequest registers fn as a request middleware. Middlewares are called in registration order before each upstream request.
func (*Handler) UseResponse ¶
func (h *Handler) UseResponse(fn ResponseFunc)
UseResponse registers fn as a response middleware. Middlewares are called in registration order after each upstream response.
type HandshakeErrorFunc ¶ added in v0.2.0
HandshakeErrorFunc is called when a client TLS handshake fails during CONNECT tunnel interception.
type RequestFunc ¶
RequestFunc is called before a request is forwarded to the upstream server.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
blocker
command
Command blocker demonstrates the Blocker interceptor with various response helpers.
|
Command blocker demonstrates the Blocker interceptor with various response helpers. |
|
dump
command
Command dump demonstrates the Dump interceptor, which prints every proxied request and response in HTTP/1.1 wire format to stderr.
|
Command dump demonstrates the Dump interceptor, which prints every proxied request and response in HTTP/1.1 wire format to stderr. |
|
genca
command
Command genca generates a self-signed ECDSA CA certificate and private key for use with the MITM proxy.
|
Command genca generates a self-signed ECDSA CA certificate and private key for use with the MITM proxy. |
|
header
command
Command header demonstrates the Header interceptor helpers: injecting headers into upstream requests and stripping or adding headers on responses.
|
Command header demonstrates the Header interceptor helpers: injecting headers into upstream requests and stripping or adding headers on responses. |
|
logger
command
Command logger demonstrates how to attach the Logger interceptor to a MITM proxy to log every proxied request and response.
|
Command logger demonstrates how to attach the Logger interceptor to a MITM proxy to log every proxied request and response. |
|
proxy
command
Command proxy is a minimal MITM HTTP/HTTPS proxy built on github.com/aomori446/mitm.
|
Command proxy is a minimal MITM HTTP/HTTPS proxy built on github.com/aomori446/mitm. |