proxymitm

package module
v0.0.0-...-904f417 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: MIT Imports: 19 Imported by: 0

README

CodeQL

sample proxy uing mitm

Usage

  1. create certificate
  2. server run
  3. do
    curl https://target -x localhost:18080 --cacert your_ca_path
    

Documentation

Index

Constants

View Source
const (
	DefaultReadTimeout  = 30 * time.Second
	DefaultWriteTimeout = 30 * time.Second
	DefaultIdleTimeout  = 90 * time.Second
)

Default timeout values

Variables

This section is empty.

Functions

func IsErrorType

func IsErrorType(err error, typ ErrorType) bool

IsErrorType determines if the specified error is a ProxyError with a specific ErrorType

func New

func New(certPath, keyPath string, logger *slog.Logger) (*http.Server, error)

Types

type ContentModifierInterceptor

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

ContentModifierInterceptor is an interceptor that modifies the contents of requests and responses

func NewContentModifierInterceptor

func NewContentModifierInterceptor(logger *slog.Logger) *ContentModifierInterceptor

NewContentModifierInterceptor creates a new ContentModifierInterceptor

func (*ContentModifierInterceptor) AddBodyReplacement

func (cmi *ContentModifierInterceptor) AddBodyReplacement(search, replace string)

AddBodyReplacement adds a response body replacement

func (*ContentModifierInterceptor) AddRequestHeaderModification

func (cmi *ContentModifierInterceptor) AddRequestHeaderModification(header, value string)

AddRequestHeaderModification adds a request header modification

func (*ContentModifierInterceptor) AddResponseHeaderModification

func (cmi *ContentModifierInterceptor) AddResponseHeaderModification(header, value string)

AddResponseHeaderModification adds a response header modification

func (*ContentModifierInterceptor) ProcessRequest

func (cmi *ContentModifierInterceptor) ProcessRequest(req *http.Request) (*http.Request, bool, error)

ProcessRequest modifies the contents of a request

func (*ContentModifierInterceptor) ProcessResponse

func (cmi *ContentModifierInterceptor) ProcessResponse(resp *http.Response, req *http.Request) (*http.Response, error)

ProcessResponse modifies the contents of a response

type ErrorType

type ErrorType string

ErrorType represents the type of proxy error

const (
	// ErrHijack is an error that occurs when HTTP connection hijacking fails
	ErrHijack ErrorType = "hijack"
	// ErrTLSHandshake is an error that occurs when TLS handshake fails
	ErrTLSHandshake ErrorType = "tls_handshake"
	// ErrCreateRequest is an error that occurs when request creation fails
	ErrCreateRequest ErrorType = "create_request"
	// ErrSendRequest is an error that occurs when request sending fails
	ErrSendRequest ErrorType = "send_request"
	// ErrCertificate is an error that occurs when certificate-related processing fails
	ErrCertificate ErrorType = "certificate"
	// ErrGateway is an error that occurs when the proxy cannot reach the upstream server (502 Bad Gateway)
	ErrGateway ErrorType = "gateway"
	// ErrTimeout is an error that occurs when the upstream server doesn't respond in time (504 Gateway Timeout)
	ErrTimeout ErrorType = "timeout"
)

type FilteringInterceptor

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

FilteringInterceptor is an interceptor that filters requests

func NewFilteringInterceptor

func NewFilteringInterceptor(logger *slog.Logger) *FilteringInterceptor

NewFilteringInterceptor creates a new FilteringInterceptor

func (*FilteringInterceptor) AddBlockedHost

func (fi *FilteringInterceptor) AddBlockedHost(host string)

AddBlockedHost adds a hostname to block

func (*FilteringInterceptor) AddBlockedPath

func (fi *FilteringInterceptor) AddBlockedPath(path string)

AddBlockedPath adds a URL path to block

func (*FilteringInterceptor) AddBlockedUserAgent

func (fi *FilteringInterceptor) AddBlockedUserAgent(userAgent string)

AddBlockedUserAgent adds a user agent to block

func (*FilteringInterceptor) ProcessRequest

func (fi *FilteringInterceptor) ProcessRequest(req *http.Request) (*http.Request, bool, error)

ProcessRequest filters requests

func (*FilteringInterceptor) ProcessResponse

func (fi *FilteringInterceptor) ProcessResponse(resp *http.Response, req *http.Request) (*http.Response, error)

ProcessResponse processes responses Returns custom response if the request was blocked in request processing

func (*FilteringInterceptor) SetBlockResponse

func (fi *FilteringInterceptor) SetBlockResponse(status int, message, body string)

SetBlockResponse is used to set a custom response for blocked requests

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for making HTTP requests

type HTTPInterceptor

type HTTPInterceptor interface {
	// Request processing - returns the modified request
	// Returning an error will stop the processing
	// Returning true will skip subsequent interceptors (termination)
	ProcessRequest(*http.Request) (*http.Request, bool, error)

	// Response processing - returns the modified response
	// Can also access the related request
	// Returning an error will stop the processing
	ProcessResponse(*http.Response, *http.Request) (*http.Response, error)
}

HTTPInterceptor is a basic interface that can process both requests and responses

type InspectingHTTPClient

type InspectingHTTPClient struct {
	Client      HTTPClient
	RequestLog  []string
	ResponseLog []string
	BodyLog     []string
}

InspectingHTTPClient is an HTTPClient that records HTTP requests and responses

func NewInspectingHTTPClient

func NewInspectingHTTPClient(client HTTPClient) *InspectingHTTPClient

NewInspectingHTTPClient creates a new InspectingHTTPClient

func (*InspectingHTTPClient) Do

Do executes an HTTP request and records the request and response

type LoggingInterceptor

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

LoggingInterceptor is an interceptor that logs the contents of requests and responses

func NewLoggingInterceptor

func NewLoggingInterceptor(logger *slog.Logger) *LoggingInterceptor

NewLoggingInterceptor creates a new LoggingInterceptor

func (*LoggingInterceptor) ProcessRequest

func (li *LoggingInterceptor) ProcessRequest(req *http.Request) (*http.Request, bool, error)

ProcessRequest logs the contents of a request

func (*LoggingInterceptor) ProcessResponse

func (li *LoggingInterceptor) ProcessResponse(resp *http.Response, req *http.Request) (*http.Response, error)

ProcessResponse logs the contents of a response

type ProxyError

type ProxyError struct {
	Type    ErrorType // Type of error
	Op      string    // Operation where the error occurred
	Message string    // Error message
	Err     error     // Original error
}

ProxyError represents an error that occurs during proxy processing

func GetProxyError

func GetProxyError(err error) *ProxyError

GetProxyError retrieves a ProxyError from an error Returns nil if not a ProxyError

func NewProxyError

func NewProxyError(typ ErrorType, op string, message string, err error) *ProxyError

NewProxyError creates a new ProxyError

func (*ProxyError) Error

func (e *ProxyError) Error() string

Error implements the error interface

func (*ProxyError) Is

func (e *ProxyError) Is(target error) bool

Is is a method used for errors.Is It allows comparison with ProxyErrors that have the same error type

func (*ProxyError) Unwrap

func (e *ProxyError) Unwrap() error

Unwrap returns the original error

type RequestCreator

type RequestCreator interface {
	CreateRequest(conn net.Conn) (*http.Request, error)
}

RequestCreator is an interface for creating requests

type RequestIDInterceptor

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

RequestIDInterceptor is an interceptor that generates and tracks request IDs

func NewRequestIDInterceptor

func NewRequestIDInterceptor(logger *slog.Logger) *RequestIDInterceptor

NewRequestIDInterceptor creates a new RequestIDInterceptor

func (*RequestIDInterceptor) GetRequestByID

func (ri *RequestIDInterceptor) GetRequestByID(id string) *http.Request

Gets the request with the specified ID

func (*RequestIDInterceptor) GetResponseByID

func (ri *RequestIDInterceptor) GetResponseByID(id string) *http.Response

Gets the response with the specified ID

func (*RequestIDInterceptor) ProcessRequest

func (ri *RequestIDInterceptor) ProcessRequest(req *http.Request) (*http.Request, bool, error)

ProcessRequest assigns an ID to a request

func (*RequestIDInterceptor) ProcessResponse

func (ri *RequestIDInterceptor) ProcessResponse(resp *http.Response, req *http.Request) (*http.Response, error)

ProcessResponse processes responses and associates request IDs

type ServerMux

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

func CreateMitmProxy

func CreateMitmProxy(certPath, keyPath string, logger *slog.Logger) (*ServerMux, error)

CreateMitmProxy load pem, and then it return MitmProxy

func (*ServerMux) AddInterceptor

func (mp *ServerMux) AddInterceptor(interceptor HTTPInterceptor)

AddInterceptor adds an interceptor to the list

func (*ServerMux) CreateRequest

func (mp *ServerMux) CreateRequest(conn net.Conn) (*http.Request, error)

CreateRequest implements the RequestCreator interface

func (*ServerMux) ServeHTTP

func (mp *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServerMux) TLSHandshake

func (mp *ServerMux) TLSHandshake(con net.Conn, hostName string) (*tls.Conn, error)

TLSHandshake implements the TLSHandshaker interface

type TLSHandshaker

type TLSHandshaker interface {
	TLSHandshake(con net.Conn, hostName string) (*tls.Conn, error)
}

TLSHandshaker is an interface for performing TLS handshakes

Directories

Path Synopsis
example
mitm command
proxy command

Jump to

Keyboard shortcuts

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