inspector

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
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

func NewHandler(inspector *Inspector) *Handler

NewHandler creates a new handler for the inspector.

func (*Handler) Close

func (h *Handler) Close()

Close closes the handler and its WebSocket hub.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers the inspector routes on a ServeMux.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type Inspector

type Inspector struct {
	// contains filtered or unexported fields
}

Inspector captures and stores HTTP traffic.

func New

func New(config Config) *Inspector

New creates a new inspector with the given configuration.

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) Clear

func (i *Inspector) Clear()

Clear removes all records.

func (*Inspector) Close

func (i *Inspector) Close()

Close closes the inspector and all subscriber channels.

func (*Inspector) Count

func (i *Inspector) Count() int

Count returns the number of stored records.

func (*Inspector) Get

func (i *Inspector) Get(id string) *Record

Get retrieves a single record by ID.

func (*Inspector) IsEnabled

func (i *Inspector) IsEnabled() bool

IsEnabled returns whether traffic capture is enabled.

func (*Inspector) RecordSummaries

func (i *Inspector) RecordSummaries(limit, offset int) []*RecordSummary

RecordSummaries returns paginated record summaries.

func (*Inspector) Records

func (i *Inspector) Records(limit, offset int) []*Record

Records returns paginated records.

func (*Inspector) SetEnabled

func (i *Inspector) SetEnabled(enabled bool)

SetEnabled enables or disables traffic capture.

func (*Inspector) Subscribe

func (i *Inspector) Subscribe() <-chan *Record

Subscribe returns a channel that receives new records.

func (*Inspector) Unsubscribe

func (i *Inspector) Unsubscribe(ch <-chan *Record)

Unsubscribe removes a subscriber channel.

func (*Inspector) Wrap

func (i *Inspector) Wrap(next http.Handler) http.Handler

Wrap wraps an HTTP handler to capture traffic.

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 NewRecord

func NewRecord(id string, req *http.Request) *Record

NewRecord creates a new record from an HTTP request.

func (*Record) Complete

func (r *Record) Complete(duration time.Duration)

Complete marks the record as complete with the given duration.

func (*Record) SetError

func (r *Record) SetError(err error)

SetError sets an error on the record.

func (*Record) SetRequestBody

func (r *Record) SetRequestBody(body []byte, maxSize int64)

SetRequestBody sets the request body on the record.

func (*Record) SetResponse

func (r *Record) SetResponse(resp *http.Response, body []byte, maxSize int64)

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

func NewStorage(capacity int) *Storage

NewStorage creates a new storage with the given capacity.

func (*Storage) Add

func (s *Storage) Add(r *Record)

Add adds a record to the storage.

func (*Storage) Capacity

func (s *Storage) Capacity() int

Capacity returns the storage capacity.

func (*Storage) Clear

func (s *Storage) Clear()

Clear removes all records from the storage.

func (*Storage) Count

func (s *Storage) Count() int

Count returns the number of records in storage.

func (*Storage) Get

func (s *Storage) Get(id string) *Record

Get retrieves a record by ID.

func (*Storage) List

func (s *Storage) List(limit, offset int) []*Record

List returns records with pagination (newest first).

func (*Storage) ListSummaries

func (s *Storage) ListSummaries(limit, offset int) []*RecordSummary

ListSummaries returns record summaries with pagination (newest first).

func (*Storage) Update

func (s *Storage) Update(r *Record)

Update updates a record in the storage.

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 NewWSHub

func NewWSHub(inspector *Inspector) *WSHub

NewWSHub creates a new WebSocket hub.

func (*WSHub) ClientCount

func (h *WSHub) ClientCount() int

ClientCount returns the number of connected clients.

func (*WSHub) Close

func (h *WSHub) Close()

Close closes the hub and all connections.

func (*WSHub) HandleWebSocket

func (h *WSHub) HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket handles WebSocket upgrade requests.

type WSMessage

type WSMessage struct {
	Type    string      `json:"type"`
	Payload interface{} `json:"payload,omitempty"`
}

WSMessage represents a WebSocket message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL