capture

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package capture provides traffic capture functionality for the proxy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Capturer

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

Capturer captures HTTP transactions.

func NewCapturer

func NewCapturer(cfg *Config) *Capturer

NewCapturer creates a new traffic capturer.

func (*Capturer) AddHandler

func (c *Capturer) AddHandler(h Handler)

AddHandler adds a handler to be called for each captured record.

func (*Capturer) Clear

func (c *Capturer) Clear()

Clear clears all captured records.

func (*Capturer) FinishCapture

func (c *Capturer) FinishCapture(rec *Record, resp *http.Response) error

FinishCapture completes capturing a response.

func (*Capturer) FinishCaptureWithStatus

func (c *Capturer) FinishCaptureWithStatus(rec *Record, statusCode int, bytesWritten int64) error

FinishCaptureWithStatus completes capturing with just status code and size (for reverse proxy).

func (*Capturer) Records

func (c *Capturer) Records() []Record

Records returns all captured records.

func (*Capturer) StartCapture

func (c *Capturer) StartCapture(req *http.Request) *Record

StartCapture begins capturing a request.

type Config

type Config struct {
	// Output is where to write captured traffic (default: stdout)
	Output io.Writer
	// Format is the output format (default: ndjson)
	Format Format
	// IncludeHeaders controls whether to include headers
	IncludeHeaders bool
	// FilterHeaders is a list of headers to exclude (case-insensitive)
	FilterHeaders []string
	// IncludeBody controls whether to include request/response bodies
	IncludeBody bool
	// MaxBodySize is the maximum body size to capture (default: 1MB)
	MaxBodySize int64
	// SkipBinary skips capturing binary content (images, videos, etc.)
	SkipBinary bool
	// Filter is the request/response filter (optional)
	Filter *Filter
}

Config holds capturer configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default capturer configuration.

type Filter

type Filter struct {
	// IncludeHosts is a list of hosts to include (supports wildcards)
	IncludeHosts []string
	// ExcludeHosts is a list of hosts to exclude (supports wildcards)
	ExcludeHosts []string
	// IncludePaths is a list of path patterns to include (supports wildcards)
	IncludePaths []string
	// ExcludePaths is a list of path patterns to exclude (supports wildcards)
	ExcludePaths []string
	// IncludeMethods is a list of HTTP methods to include
	IncludeMethods []string
	// ExcludeMethods is a list of HTTP methods to exclude
	ExcludeMethods []string
	// IncludeStatusCodes is a list of status codes to include
	IncludeStatusCodes []int
	// ExcludeStatusCodes is a list of status codes to exclude
	ExcludeStatusCodes []int
	// MinStatusCode is the minimum status code to include
	MinStatusCode int
	// MaxStatusCode is the maximum status code to include
	MaxStatusCode int
	// contains filtered or unexported fields
}

Filter defines criteria for including/excluding requests.

func NewFilter

func NewFilter() *Filter

NewFilter creates a new filter with default settings.

func (*Filter) Compile

func (f *Filter) Compile() error

Compile compiles the filter patterns for efficient matching.

func (*Filter) Match

func (f *Filter) Match(rec *Record) bool

Match checks if a complete record matches the filter criteria.

func (*Filter) MatchRequest

func (f *Filter) MatchRequest(host, path, method string) bool

MatchRequest checks if a request matches the filter criteria.

func (*Filter) MatchResponse

func (f *Filter) MatchResponse(statusCode int) bool

MatchResponse checks if a response matches the filter criteria.

type Format

type Format string

Format specifies the output format for captured traffic.

const (
	// FormatJSON outputs records as JSON objects
	FormatJSON Format = "json"
	// FormatNDJSON outputs records as newline-delimited JSON
	FormatNDJSON Format = "ndjson"
	// FormatHAR outputs records in HAR format
	FormatHAR Format = "har"
	// FormatIR outputs records in APISpecRift IR format
	FormatIR Format = "ir"
)

type HAR

type HAR struct {
	Log HARLog `json:"log"`
}

HAR represents an HTTP Archive file.

type HARCache

type HARCache struct{}

HARCache represents cache information.

type HARContent

type HARContent struct {
	Size     int64  `json:"size"`
	MimeType string `json:"mimeType"`
	Text     string `json:"text,omitempty"`
	Encoding string `json:"encoding,omitempty"`
}

HARContent represents response content.

type HARCookie

type HARCookie struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Path     string `json:"path,omitempty"`
	Domain   string `json:"domain,omitempty"`
	Expires  string `json:"expires,omitempty"`
	HTTPOnly bool   `json:"httpOnly,omitempty"`
	Secure   bool   `json:"secure,omitempty"`
}

HARCookie represents a cookie.

type HARCreator

type HARCreator struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

HARCreator identifies the tool that created the HAR.

type HAREntry

type HAREntry struct {
	StartedDateTime string      `json:"startedDateTime"`
	Time            float64     `json:"time"`
	Request         HARRequest  `json:"request"`
	Response        HARResponse `json:"response"`
	Cache           HARCache    `json:"cache"`
	Timings         HARTimings  `json:"timings"`
}

HAREntry represents a single HTTP transaction.

type HARHeader

type HARHeader struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

HARHeader represents an HTTP header.

type HARLog

type HARLog struct {
	Version string     `json:"version"`
	Creator HARCreator `json:"creator"`
	Entries []HAREntry `json:"entries"`
}

HARLog is the root of the HAR format.

type HARPostData

type HARPostData struct {
	MimeType string         `json:"mimeType"`
	Text     string         `json:"text,omitempty"`
	Params   []HARPostParam `json:"params,omitempty"`
}

HARPostData represents POST data.

type HARPostParam

type HARPostParam struct {
	Name        string `json:"name"`
	Value       string `json:"value,omitempty"`
	FileName    string `json:"fileName,omitempty"`
	ContentType string `json:"contentType,omitempty"`
}

HARPostParam represents a POST parameter.

type HARQueryPair

type HARQueryPair struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

HARQueryPair represents a query string parameter.

type HARRequest

type HARRequest struct {
	Method      string         `json:"method"`
	URL         string         `json:"url"`
	HTTPVersion string         `json:"httpVersion"`
	Cookies     []HARCookie    `json:"cookies"`
	Headers     []HARHeader    `json:"headers"`
	QueryString []HARQueryPair `json:"queryString"`
	PostData    *HARPostData   `json:"postData,omitempty"`
	HeadersSize int            `json:"headersSize"`
	BodySize    int            `json:"bodySize"`
}

HARRequest represents an HTTP request.

type HARResponse

type HARResponse struct {
	Status      int         `json:"status"`
	StatusText  string      `json:"statusText"`
	HTTPVersion string      `json:"httpVersion"`
	Cookies     []HARCookie `json:"cookies"`
	Headers     []HARHeader `json:"headers"`
	Content     HARContent  `json:"content"`
	RedirectURL string      `json:"redirectURL"`
	HeadersSize int         `json:"headersSize"`
	BodySize    int         `json:"bodySize"`
}

HARResponse represents an HTTP response.

type HARTimings

type HARTimings struct {
	Blocked float64 `json:"blocked"`
	DNS     float64 `json:"dns"`
	Connect float64 `json:"connect"`
	Send    float64 `json:"send"`
	Wait    float64 `json:"wait"`
	Receive float64 `json:"receive"`
	SSL     float64 `json:"ssl"`
}

HARTimings represents timing information.

type HARWriter

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

HARWriter writes records in HAR format.

func NewHARWriter

func NewHARWriter(output io.Writer) *HARWriter

NewHARWriter creates a new HAR writer.

func (*HARWriter) AddRecord

func (w *HARWriter) AddRecord(rec *Record)

AddRecord converts a capture Record to HAR format and adds it.

func (*HARWriter) Write

func (w *HARWriter) Write() error

Write outputs the complete HAR file.

type Handler

type Handler func(*Record)

Handler is called for each captured record.

type Record

type Record struct {
	// Request information
	Request RequestRecord `json:"request"`
	// Response information
	Response ResponseRecord `json:"response,omitempty"`
	// Timing information
	StartTime  time.Time `json:"startTime"`
	EndTime    time.Time `json:"endTime,omitempty"`
	DurationMs float64   `json:"durationMs,omitempty"`
}

Record represents a captured HTTP transaction.

type RequestRecord

type RequestRecord struct {
	Method      string            `json:"method"`
	URL         string            `json:"url"`
	Host        string            `json:"host"`
	Path        string            `json:"path"`
	Scheme      string            `json:"scheme"`
	Headers     map[string]string `json:"headers,omitempty"`
	Query       map[string]string `json:"query,omitempty"`
	Body        interface{}       `json:"body,omitempty"`
	BodySize    int64             `json:"bodySize,omitempty"`
	IsBinary    bool              `json:"isBinary,omitempty"`
	ContentType string            `json:"contentType,omitempty"`
}

RequestRecord represents a captured HTTP request.

type ResponseRecord

type ResponseRecord struct {
	Status      int               `json:"status"`
	StatusText  string            `json:"statusText,omitempty"`
	Headers     map[string]string `json:"headers,omitempty"`
	Body        interface{}       `json:"body,omitempty"`
	IsBinary    bool              `json:"isBinary,omitempty"`
	ContentType string            `json:"contentType,omitempty"`
	Size        int64             `json:"size,omitempty"`
}

ResponseRecord represents a captured HTTP response.

Jump to

Keyboard shortcuts

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