Documentation
¶
Index ¶
- func Dump(w io.Writer) (RequestFunc, ResponseFunc)
- func Logger(log *slog.Logger) (RequestFunc, ResponseFunc)
- func Response(status int, contentType string, body io.ReadCloser) *http.Response
- type BlockResponse
- type ErrorFunc
- type RequestFunc
- func Blocker(patterns ...string) RequestFunc
- func BlockerFunc(respond BlockResponse, match func(*http.Request) bool) RequestFunc
- func BlockerWith(respond BlockResponse, patterns ...string) RequestFunc
- func DumpRequest(w io.Writer) RequestFunc
- func RemoveRequestHeader(key string) RequestFunc
- func SetRequestHeader(key, value string) RequestFunc
- type ResponseFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dump ¶
func Dump(w io.Writer) (RequestFunc, ResponseFunc)
Dump returns a pair of middlewares that write each request and response in HTTP/1.1 wire format to w. It is a shorthand for calling DumpRequest and DumpResponse with the same writer, and carries the same memory cost: every body passing through the proxy is buffered in full.
func Logger ¶
func Logger(log *slog.Logger) (RequestFunc, ResponseFunc)
Logger returns a pair of middlewares that log each proxied request and response using the provided slog.Logger.
The RequestFunc middleware records the method and URL at the start of the request. The ResponseFunc middleware records the status code, content type, and elapsed time.
Usage:
onReq, onResp := middleware.Logger(slog.Default()) handler.UseRequest(onReq) handler.UseResponse(onResp)
Types ¶
type BlockResponse ¶
BlockResponse constructs the HTTP response returned for a blocked request.
func RespondWith403 ¶
func RespondWith403() BlockResponse
RespondWith403 returns a BlockResponse that sends 403 Forbidden.
func RespondWithAuto ¶
func RespondWithAuto() BlockResponse
RespondWithAuto returns a BlockResponse that infers the appropriate empty response from the request URL's file extension.
- .gif .png .jpg .jpeg .webp .ico .svg → 1×1 transparent GIF
- .js .mjs → empty JS (//)
- .css → empty CSS
- .html .htm → empty HTML
- (other) → 200 empty body
func RespondWithEmptyCSS ¶
func RespondWithEmptyCSS() BlockResponse
RespondWithEmptyCSS returns a BlockResponse that sends an empty CSS response.
func RespondWithEmptyHTML ¶
func RespondWithEmptyHTML() BlockResponse
RespondWithEmptyHTML returns a BlockResponse that sends a minimal HTML response.
func RespondWithEmptyJS ¶
func RespondWithEmptyJS() BlockResponse
RespondWithEmptyJS returns a BlockResponse that sends an empty JavaScript response.
func RespondWithPixel ¶
func RespondWithPixel() BlockResponse
RespondWithPixel returns a BlockResponse that sends a 1×1 transparent GIF. Useful for silently replacing blocked image requests without causing browser errors.
type ErrorFunc ¶ added in v0.2.0
ErrorFunc is called when the upstream round trip fails, so no response middleware will run for the request.
type RequestFunc ¶
type RequestFunc = mitm.RequestFunc
RequestFunc is called before a request is forwarded to the upstream server.
func Blocker ¶
func Blocker(patterns ...string) RequestFunc
Blocker returns a RequestFunc that responds with 403 Forbidden when the request's host matches any of the provided patterns.
Patterns support a single leading wildcard:
- "example.com" matches only example.com
- "*.example.com" matches any subdomain of example.com, but not example.com itself
func BlockerFunc ¶
func BlockerFunc(respond BlockResponse, match func(*http.Request) bool) RequestFunc
BlockerFunc returns a RequestFunc that calls respond when match returns true. Use this when host-pattern matching is not expressive enough, e.g. for URL path filtering.
func BlockerWith ¶
func BlockerWith(respond BlockResponse, patterns ...string) RequestFunc
BlockerWith is like Blocker but uses respond instead of 403 Forbidden.
func DumpRequest ¶
func DumpRequest(w io.Writer) RequestFunc
DumpRequest returns a RequestFunc that writes each request in HTTP/1.1 wire format (including body) to w. Intended for debugging.
The whole body is read into memory to be written out and then replayed, so a large upload passing through the proxy is held in memory in full. Scope this to the traffic being investigated rather than leaving it on a busy proxy.
func RemoveRequestHeader ¶
func RemoveRequestHeader(key string) RequestFunc
RemoveRequestHeader returns a RequestFunc that removes key from every request.
func SetRequestHeader ¶
func SetRequestHeader(key, value string) RequestFunc
SetRequestHeader returns a RequestFunc that sets key to value on every request. An existing value for key is replaced.
type ResponseFunc ¶
type ResponseFunc = mitm.ResponseFunc
ResponseFunc is called after the upstream response is received and before it is written back to the client. The original request is available via resp.Request.
func DumpResponse ¶
func DumpResponse(w io.Writer) ResponseFunc
DumpResponse returns a ResponseFunc that writes each response in HTTP/1.1 wire format (including body) to w. Intended for debugging.
The whole body is read into memory, so a large download is held in memory in full. Scope this to the traffic being investigated.
func RemoveResponseHeader ¶
func RemoveResponseHeader(key string) ResponseFunc
RemoveResponseHeader returns a ResponseFunc that removes key from every response.
func SetResponseHeader ¶
func SetResponseHeader(key, value string) ResponseFunc
SetResponseHeader returns a ResponseFunc that sets key to value on every response. An existing value for key is replaced.