Documentation
¶
Overview ¶
Package reqlog is the live view of requests crossing a tunnel.
Both ends see the same request and can report it: the hub as it proxies (revproxy.WithRequestHook) and the peer as it serves (dial.Options.RequestHook). Neither stores anything. A hook is called once per request, after the response, and whatever it does with the event is the application's business: print it, count it, ship it somewhere.
The holt CLI uses it for the line `holt expose` and `holt hub` print as traffic goes through, which is the whole point: a tunnel you cannot watch is hard to trust.
Index ¶
Constants ¶
const ( SkippedDisabled = "disabled" SkippedBinary = "binary" )
The reasons a body was not captured.
const DefaultRecent = 100
DefaultRecent is how many events a new watcher is replayed when the broker is built with 0. Enough to fill a console panel, small enough to forget.
const Redacted = "<redacted>"
Redacted replaces the value of a header that carries a credential. The header is still reported — knowing a request was authenticated is worth seeing — but the secret itself never reaches a viewer.
Variables ¶
This section is empty.
Functions ¶
func BodyLimit ¶ added in v0.43.0
BodyLimit reads the body limit out of a set of options, which is how a caller holding options rather than a config asks how much it was told to capture.
func Headers ¶ added in v0.43.0
Headers flattens headers for reporting, joining repeats the way they travel on the wire and redacting the ones that carry credentials.
func Middleware ¶
Middleware wraps a handler so every request it serves is reported. It is what the peer side uses, and it works on any http.Handler.
By default it reports metadata only. WithHeaders and WithBodyLimit add the payload, bounded, for a caller that wants to see what crossed.
Types ¶
type Body ¶ added in v0.43.0
type Body struct {
// Content is the captured prefix, empty when Skipped says why not.
Content []byte
// Size is how many bytes crossed in total, which is more than
// len(Content) when Truncated.
Size int64
// Truncated reports that the body was longer than the limit.
Truncated bool
// Skipped names why nothing was captured: "disabled" (capture is
// off), "binary" (a content type not worth showing as text), or
// "" when Content is the answer.
Skipped string
}
Body is what was captured of one side's payload. Capture is bounded and best-effort: a body is a prefix, a reason it was not taken, or nothing at all when there was no body to take.
type BodyCapture ¶ added in v0.43.0
type BodyCapture struct {
// contains filtered or unexported fields
}
BodyCapture accumulates up to limit bytes while counting everything that passes. The zero value captures nothing.
func CaptureRequestBody ¶ added in v0.43.0
func CaptureRequestBody(r *http.Request, limit int) *BodyCapture
CaptureRequestBody replaces r.Body with one that records up to limit bytes as the handler reads it, and returns the capture to read afterwards. A limit of 0 records nothing but still counts the size, so a report can say a body was there.
func (*BodyCapture) Body ¶ added in v0.43.0
func (c *BodyCapture) Body(contentType string) Body
Body renders what was captured, given the content type it was declared with. A nil capture is a body nobody looked at.
type Broker ¶ added in v0.37.0
type Broker struct {
// contains filtered or unexported fields
}
Broker fans one Hook out to many watchers: the hub publishes each request once, and every console tab watching gets it.
Nothing is persisted. The broker keeps the last Recent events in memory so a tab that opens mid-traffic is not blank, and that buffer dies with the process. A watcher that falls behind loses events rather than slowing the request that produced them.
broker := reqlog.NewBroker(0)
proxy := revproxy.New(reg, revproxy.WithRequestHook(broker.Hook()))
for ev := range broker.Watch(ctx) { ... }
func NewBroker ¶ added in v0.37.0
NewBroker returns a broker replaying the last recent events to each new watcher. Pass 0 for DefaultRecent, or a negative number for no replay at all (a watcher then sees only what arrives after it).
func (*Broker) Hook ¶ added in v0.37.0
Hook returns the Hook that feeds the broker, for revproxy.WithRequestHook or dial.Options.RequestHook.
type Event ¶
type Event struct {
// At is when the response completed.
At time.Time
// Peer is the tunnel the request went through. The hub fills it;
// a peer reporting its own requests leaves it empty, since it
// knows only one.
Peer string
// Method and Path are the request's, as received.
Method string
Path string
// Query is the raw query string without "?", empty when there is
// none. It is kept apart from Path so a list stays readable and a
// reader can still see what was asked.
Query string
// Host is the authority the request carried, which is what
// subdomain routing reads.
Host string
// Proto is the client's protocol ("HTTP/1.1", "HTTP/2.0").
Proto string
// RemoteAddr is who the reporting end saw connecting: the client
// for a hub behind nothing, otherwise whatever proxy fronts it.
RemoteAddr string
// UserAgent is the request's, empty when it sent none.
UserAgent string
// RequestBytes is the request's declared Content-Length, -1 when
// it did not say (a streamed or chunked body).
RequestBytes int64
// ResponseBytes is what the handler actually wrote.
ResponseBytes int64
// RequestHeaders and ResponseHeaders are the headers as they
// crossed, with credential-carrying values redacted (see Headers).
// Nil when the reporting end does not collect them.
RequestHeaders map[string]string
ResponseHeaders map[string]string
// RequestBody and ResponseBody are bounded captures of the
// payloads, empty unless the reporting end was built with a body
// limit. See Body.
RequestBody Body
ResponseBody Body
// Status is the response code, 0 when the request never got one
// (the connection died first).
Status int
// Duration is how long the response took at the reporting end,
// so the hub's includes the tunnel hop and the peer's does not.
Duration time.Duration
}
Event is one request, seen from whichever end reports it. It is metadata only: no header values beyond the few named here, and never a body, so reporting a request costs what reading these fields costs and nothing that would have to be redacted later.
type Hook ¶
type Hook func(Event)
Hook receives one event per request. It runs on the request's goroutine after the response, so it should not block: print, count, or hand off to a channel.
type Option ¶ added in v0.43.0
type Option func(*config)
Option configures what a Middleware or Recorder collects.
func WithBodyLimit ¶ added in v0.43.0
WithBodyLimit captures up to limit bytes of each body, request and response. 0 (the default) captures none, which is the only setting that costs nothing and the only one that cannot leak a payload into whatever reads the events.
func WithHeaders ¶ added in v0.43.0
func WithHeaders() Option
WithHeaders reports the request and response headers, with credential-carrying values redacted (see Headers).
type Recorder ¶
type Recorder struct {
http.ResponseWriter
// contains filtered or unexported fields
}
Recorder captures the status code of a response while forwarding everything else, so a handler behind it keeps streaming, flushing and hijacking as it would without one.
func NewRecorder ¶
func NewRecorder(w http.ResponseWriter, opts ...Option) *Recorder
NewRecorder wraps w. Until the handler writes, the status reads as 200, which is what net/http sends for a handler that writes a body without calling WriteHeader. With WithBodyLimit it also keeps a bounded prefix of the response, readable afterwards with Body.
func (*Recorder) Body ¶ added in v0.43.0
Body is what was captured of the response, judged against the content type the handler declared.
func (*Recorder) Flush ¶
func (r *Recorder) Flush()
Flush keeps streaming responses streaming: without it the wrapper would hide the underlying Flusher and the proxy's immediate flush would buffer.
func (*Recorder) Unwrap ¶
func (r *Recorder) Unwrap() http.ResponseWriter
Unwrap lets http.ResponseController reach the real writer, which the standard library looks for (hijack, deadlines).