proxy

package
v0.1.49 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CACertPath

func CACertPath() string

CACertPath returns the path where CA cert should be stored.

func CAKeyPath

func CAKeyPath() string

CAKeyPath returns the path where CA key should be stored.

Types

type CaptureListener

type CaptureListener interface {
	OnCapture(capture *CapturedRequest)
}

CaptureListener receives real-time capture events.

type CaptureListenerFunc

type CaptureListenerFunc func(*CapturedRequest)

CaptureListenerFunc is a function adapter for CaptureListener.

func (CaptureListenerFunc) OnCapture

func (f CaptureListenerFunc) OnCapture(capture *CapturedRequest)

type CaptureStats

type CaptureStats struct {
	TotalCount        int
	TotalRequestSize  int64
	TotalResponseSize int64
	MethodCounts      map[string]int
	StatusCounts      map[int]int
	HostCounts        map[string]int
	AvgDuration       time.Duration
	OldestCapture     time.Time
	NewestCapture     time.Time
}

Stats returns statistics about the captured traffic.

type CaptureStore

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

CaptureStore stores captured requests in a ring buffer.

func NewCaptureStore

func NewCaptureStore(maxSize int) *CaptureStore

NewCaptureStore creates a new capture store with the given buffer size.

func (*CaptureStore) Add

func (s *CaptureStore) Add(capture *CapturedRequest)

Add adds a new capture to the store.

func (*CaptureStore) AddListener

func (s *CaptureStore) AddListener(listener CaptureListener)

AddListener adds a listener for real-time capture events.

func (*CaptureStore) All

func (s *CaptureStore) All() []*CapturedRequest

All returns all captures in reverse chronological order.

func (*CaptureStore) Clear

func (s *CaptureStore) Clear()

Clear removes all captures from the store.

func (*CaptureStore) Count

func (s *CaptureStore) Count() int

Count returns the current number of captures.

func (*CaptureStore) Get

func (s *CaptureStore) Get(id string) *CapturedRequest

Get returns a capture by ID.

func (*CaptureStore) List

func (s *CaptureStore) List(opts FilterOptions) []*CapturedRequest

List returns captures matching the filter options. Results are returned in reverse chronological order (newest first).

func (*CaptureStore) RemoveListener

func (s *CaptureStore) RemoveListener(listener CaptureListener)

RemoveListener removes a listener.

func (*CaptureStore) Stats

func (s *CaptureStore) Stats() CaptureStats

Stats returns statistics about the captured traffic.

type CapturedRequest

type CapturedRequest struct {
	ID        string    `json:"id"`
	Timestamp time.Time `json:"timestamp"`

	// Request data
	Method         string              `json:"method"`
	URL            string              `json:"url"`
	Host           string              `json:"host"`
	Path           string              `json:"path"`
	RequestHeaders map[string][]string `json:"request_headers,omitempty"`
	RequestBody    []byte              `json:"request_body,omitempty"`
	RequestSize    int64               `json:"request_size"`

	// Response data
	StatusCode      int                 `json:"status_code"`
	StatusText      string              `json:"status_text"`
	ResponseHeaders map[string][]string `json:"response_headers,omitempty"`
	ResponseBody    []byte              `json:"response_body,omitempty"`
	ResponseSize    int64               `json:"response_size"`

	// Timing
	Duration time.Duration `json:"duration"`

	// Connection info
	IsHTTPS        bool   `json:"is_https"`
	TLSVersion     string `json:"tls_version,omitempty"`
	TLSCipherSuite string `json:"tls_cipher_suite,omitempty"`
	SourceIP       string `json:"source_ip"`
	SourcePort     int    `json:"source_port"`

	// Error info (if request failed)
	Error string `json:"error,omitempty"`
}

CapturedRequest represents a single captured HTTP request/response pair.

func (*CapturedRequest) ContentType

func (c *CapturedRequest) ContentType() string

ContentType returns the response content-type header.

func (*CapturedRequest) IsClientError

func (c *CapturedRequest) IsClientError() bool

IsClientError returns true if the response status is 4xx.

func (*CapturedRequest) IsRedirect

func (c *CapturedRequest) IsRedirect() bool

IsRedirect returns true if the response status is 3xx.

func (*CapturedRequest) IsServerError

func (c *CapturedRequest) IsServerError() bool

IsServerError returns true if the response status is 5xx.

func (*CapturedRequest) IsSuccess

func (c *CapturedRequest) IsSuccess() bool

IsSuccess returns true if the response status is 2xx.

type Config

type Config struct {
	// ListenAddr is the address to listen on (e.g., ":8080", "127.0.0.1:8080")
	ListenAddr string

	// EnableHTTPS enables HTTPS interception via MITM
	EnableHTTPS bool

	// CACertPath is the path to the CA certificate for MITM
	CACertPath string

	// CAKeyPath is the path to the CA private key for MITM
	CAKeyPath string

	// AutoGenerateCA generates CA certificate if not found
	AutoGenerateCA bool

	// MaxBodySize is the maximum request/response body size to capture (bytes)
	// Bodies larger than this will be truncated
	MaxBodySize int64

	// BufferSize is the maximum number of captures to keep in memory
	BufferSize int

	// ExcludeHosts is a list of hosts to exclude from capture
	ExcludeHosts []string

	// IncludeHosts is a list of hosts to include (if empty, all hosts are included)
	IncludeHosts []string

	// ExcludeContentTypes is a list of content-types to exclude from body capture
	ExcludeContentTypes []string

	// Verbose enables verbose logging
	Verbose bool
}

Config holds the proxy server configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

func NewConfig

func NewConfig(opts ...ConfigOption) Config

NewConfig creates a new Config with the given options applied to defaults.

type ConfigOption

type ConfigOption func(*Config)

ConfigOption is a function that modifies the Config.

func WithAutoGenerateCA

func WithAutoGenerateCA(auto bool) ConfigOption

WithAutoGenerateCA enables or disables auto CA generation.

func WithBufferSize

func WithBufferSize(size int) ConfigOption

WithBufferSize sets the capture buffer size.

func WithCACert

func WithCACert(certPath, keyPath string) ConfigOption

WithCACert sets the CA certificate and key paths.

func WithExcludeHosts

func WithExcludeHosts(hosts ...string) ConfigOption

WithExcludeHosts sets hosts to exclude from capture.

func WithHTTPS

func WithHTTPS(enable bool) ConfigOption

WithHTTPS enables or disables HTTPS interception.

func WithIncludeHosts

func WithIncludeHosts(hosts ...string) ConfigOption

WithIncludeHosts sets hosts to include in capture (whitelist mode).

func WithListenAddr

func WithListenAddr(addr string) ConfigOption

WithListenAddr sets the listen address.

func WithMaxBodySize

func WithMaxBodySize(size int64) ConfigOption

WithMaxBodySize sets the maximum body size to capture.

func WithVerbose

func WithVerbose(verbose bool) ConfigOption

WithVerbose enables verbose logging.

type FilterOptions

type FilterOptions struct {
	// Method filter (GET, POST, etc.)
	Method string

	// Host filter (supports wildcards like *.example.com)
	Host string

	// Path filter (supports wildcards)
	Path string

	// Status code range
	StatusMin int
	StatusMax int

	// Content-type filter
	ContentType string

	// Full-text search (searches URL, headers, body)
	Search string

	// Size filters
	MinSize int64
	MaxSize int64

	// Time range
	After  time.Time
	Before time.Time

	// Protocol filter
	HTTPSOnly bool
	HTTPOnly  bool

	// Pagination
	Limit  int
	Offset int
}

FilterOptions specifies filters for querying captures.

type ProxyHandler

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

ProxyHandler handles HTTP and HTTPS proxy requests.

func NewProxyHandler

func NewProxyHandler(store *CaptureStore, tlsConfig *TLSConfig, config Config) *ProxyHandler

NewProxyHandler creates a new proxy handler.

func (*ProxyHandler) ServeHTTP

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

ServeHTTP handles incoming proxy requests.

type Server

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

Server is an HTTP/HTTPS proxy server that captures traffic.

func NewServer

func NewServer(opts ...ConfigOption) (*Server, error)

NewServer creates a new proxy server with the given configuration.

func (*Server) AddListener

func (s *Server) AddListener(listener CaptureListener)

AddListener adds a capture listener for real-time events.

func (*Server) CACertPEM

func (s *Server) CACertPEM() []byte

CACertPEM returns the CA certificate in PEM format.

func (*Server) ClearCaptures

func (s *Server) ClearCaptures()

ClearCaptures clears all captured requests.

func (*Server) Config

func (s *Server) Config() Config

Config returns the server configuration.

func (*Server) ExportCACert

func (s *Server) ExportCACert(path string) error

ExportCACert exports the CA certificate to a file.

func (*Server) GetCapture

func (s *Server) GetCapture(id string) *CapturedRequest

GetCapture returns a single capture by ID.

func (*Server) GetCaptures

func (s *Server) GetCaptures(opts FilterOptions) []*CapturedRequest

GetCaptures returns captured requests matching the filter.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the proxy server is running.

func (*Server) ListenAddr

func (s *Server) ListenAddr() string

ListenAddr returns the actual address the server is listening on. Useful when using port 0 to get an available port.

func (*Server) RemoveListener

func (s *Server) RemoveListener(listener CaptureListener)

RemoveListener removes a capture listener.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the proxy server.

func (*Server) Stats

func (s *Server) Stats() CaptureStats

Stats returns capture statistics.

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the proxy server.

func (*Server) Store

func (s *Server) Store() *CaptureStore

Store returns the capture store.

func (*Server) TLSConfig

func (s *Server) TLSConfig() *TLSConfig

TLSConfig returns the TLS configuration (for CA export).

type TLSConfig

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

TLSConfig manages CA certificate and dynamic host certificate generation.

func NewTLSConfig

func NewTLSConfig(certPath, keyPath string, autoGenerate bool) (*TLSConfig, error)

NewTLSConfig loads or generates CA certificate for MITM.

func (*TLSConfig) CACertPEM

func (tc *TLSConfig) CACertPEM() []byte

CACertPEM returns the CA certificate in PEM format.

func (*TLSConfig) ExportCACert

func (tc *TLSConfig) ExportCACert(destPath string) error

ExportCACert copies the CA certificate to the specified path.

func (*TLSConfig) GetCertForHost

func (tc *TLSConfig) GetCertForHost(host string) (*tls.Certificate, error)

GetCertForHost returns a TLS certificate for the given host. Certificates are cached for performance.

func (*TLSConfig) GetTLSConfig

func (tc *TLSConfig) GetTLSConfig() *tls.Config

GetTLSConfig returns a tls.Config that uses dynamic certificate generation.

Jump to

Keyboard shortcuts

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