Documentation
¶
Overview ¶
Package inspector provides HTTP traffic inspection and recording capabilities.
The inspector package captures HTTP requests and responses passing through the tunnel, stores them in a ring buffer, and provides real-time updates via WebSocket to connected clients (the Inspector Web UI).
Features ¶
- Request/response capture with headers and body
- Ring buffer storage with configurable capacity
- Real-time WebSocket push to UI clients
- HTTP handler wrapping middleware
Usage ¶
insp := inspector.New(inspector.Config{
MaxRecords: 1000,
MaxBodySize: 1024 * 1024, // 1MB
EnableCapture: true,
})
// Wrap your handler to capture traffic
handler := insp.Wrap(yourHandler)
// Subscribe to real-time updates
ch := insp.Subscribe()
for record := range ch {
// Handle new record
}
Package inspector provides HTTP traffic inspection and recording capabilities.
Index ¶
- Constants
- type Config
- type Handler
- type Inspector
- func (i *Inspector) Capture(req *http.Request, reqBody []byte, resp *http.Response, respBody []byte, ...) *Record
- func (i *Inspector) Clear()
- func (i *Inspector) Close()
- func (i *Inspector) Count() int
- func (i *Inspector) Get(id string) *Record
- func (i *Inspector) IsEnabled() bool
- func (i *Inspector) RecordSummaries(limit, offset int) []*RecordSummary
- func (i *Inspector) Records(limit, offset int) []*Record
- func (i *Inspector) SetEnabled(enabled bool)
- func (i *Inspector) Subscribe() <-chan *Record
- func (i *Inspector) Unsubscribe(ch <-chan *Record)
- func (i *Inspector) Wrap(next http.Handler) http.Handler
- type Record
- type RecordSummary
- type ResponseData
- type Storage
- func (s *Storage) Add(r *Record)
- func (s *Storage) Capacity() int
- func (s *Storage) Clear()
- func (s *Storage) Count() int
- func (s *Storage) Get(id string) *Record
- func (s *Storage) List(limit, offset int) []*Record
- func (s *Storage) ListSummaries(limit, offset int) []*RecordSummary
- func (s *Storage) Update(r *Record)
- type WSClient
- type WSHub
- type WSMessage
Constants ¶
const ( MsgTypeRecord = "record" MsgTypeRecordList = "record_list" MsgTypeClear = "clear" MsgTypeStats = "stats" MsgTypePing = "ping" MsgTypePong = "pong" MsgTypeError = "error" MsgTypeSubscribe = "subscribe" MsgTypeUnsubscribe = "unsubscribe" )
WebSocket message types.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
MaxRecords int // Maximum number of records to keep.
MaxBodySize int64 // Maximum body size to capture (per request/response).
EnableCapture bool // Whether to capture traffic.
}
Config holds the inspector configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default inspector configuration.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP handlers for the inspector API.
func NewHandler ¶
NewHandler creates a new handler for the inspector.
func (*Handler) RegisterRoutes ¶
RegisterRoutes registers the inspector routes on a ServeMux.
type Inspector ¶
type Inspector struct {
// contains filtered or unexported fields
}
Inspector captures and stores HTTP traffic.
func (*Inspector) Capture ¶
func (i *Inspector) Capture(req *http.Request, reqBody []byte, resp *http.Response, respBody []byte, duration time.Duration, err error) *Record
Capture records a request/response pair.
func (*Inspector) Close ¶
func (i *Inspector) Close()
Close closes the inspector and all subscriber channels.
func (*Inspector) RecordSummaries ¶
func (i *Inspector) RecordSummaries(limit, offset int) []*RecordSummary
RecordSummaries returns paginated record summaries.
func (*Inspector) SetEnabled ¶
SetEnabled enables or disables traffic capture.
func (*Inspector) Unsubscribe ¶
Unsubscribe removes a subscriber channel.
type Record ¶
type Record struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
URL string `json:"url"`
Host string `json:"host"`
Path string `json:"path"`
Headers map[string]string `json:"headers"`
Body string `json:"body,omitempty"`
BodySize int64 `json:"bodySize"`
Status int `json:"status"`
Duration time.Duration `json:"duration"`
Response *ResponseData `json:"response,omitempty"`
Error string `json:"error,omitempty"`
}
Record represents a captured HTTP request/response pair.
func (*Record) SetRequestBody ¶
SetRequestBody sets the request body on the record.
func (*Record) SetResponse ¶
SetResponse sets the response data on the record.
func (*Record) Summary ¶
func (r *Record) Summary() *RecordSummary
Summary returns a lightweight summary of the record.
type RecordSummary ¶
type RecordSummary struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
URL string `json:"url"`
Host string `json:"host"`
Path string `json:"path"`
Status int `json:"status"`
Duration time.Duration `json:"duration"`
BodySize int64 `json:"bodySize"`
RespSize int64 `json:"respSize"`
Error string `json:"error,omitempty"`
}
RecordSummary is a lightweight version of Record for list views.
type ResponseData ¶
type ResponseData struct {
Status int `json:"status"`
StatusText string `json:"statusText"`
Headers map[string]string `json:"headers"`
Body string `json:"body,omitempty"`
BodySize int64 `json:"bodySize"`
}
ResponseData contains the response information.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage is a thread-safe ring buffer for storing records.
func NewStorage ¶
NewStorage creates a new storage with the given capacity.
func (*Storage) ListSummaries ¶
func (s *Storage) ListSummaries(limit, offset int) []*RecordSummary
ListSummaries returns record summaries with pagination (newest first).
type WSClient ¶
type WSClient struct {
// contains filtered or unexported fields
}
WSClient represents a connected WebSocket client.
type WSHub ¶
type WSHub struct {
// contains filtered or unexported fields
}
WSHub manages WebSocket connections.
func (*WSHub) ClientCount ¶
ClientCount returns the number of connected clients.
func (*WSHub) HandleWebSocket ¶
func (h *WSHub) HandleWebSocket(w http.ResponseWriter, r *http.Request)
HandleWebSocket handles WebSocket upgrade requests.