proxy

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package proxy implements the HTTP/HTTPS MITM proxy server.

This file (entry.go) contains the HTTP server entry point and request routing logic.

Overview

The "entry" is the bridge between Go's standard HTTP server and the proxy's custom MITM logic. It serves as the initial request handler that routes traffic to appropriate handlers based on request type and configuration.

Architecture

The entry system consists of three main components:

  1. wrapListener: Wraps the TCP listener to intercept and prepare connections - Attaches connection context (ConnContext) to each client connection - Triggers ClientConnected addon events

  2. entry: The HTTP server and request router - Implements http.Handler via ServeHTTP - Routes requests based on method and URL - Manages proxy lifecycle (start, shutdown, close)

  3. Request handlers: Process different types of requests - ServeHTTP: Routes all incoming requests - handleConnect: Handles CONNECT requests for HTTPS tunneling - directTransfer: Transparent tunneling without interception - httpsDialFirstAttack: MITM with upstream-first connection - httpsDialLazyAttack: MITM with client-first connection

Request Flow

HTTP Proxy Request:

Client → wrapListener → entry.ServeHTTP → attacker.Attack → Upstream

CONNECT Request (Non-Intercepted):

Client → wrapListener → entry.ServeHTTP → handleConnect → directTransfer → Upstream

CONNECT Request (Intercepted):

Client → wrapListener → entry.ServeHTTP → handleConnect → httpsDialLazyAttack → attacker → Upstream

Connection Context

The entry system maintains connection-level state through ConnContext:

  • Created by wrapListener.Accept() for each connection
  • Attached to request context by entry.server.ConnContext
  • Accessible throughout request lifecycle via proxycontext.GetConnContext()
  • Contains client connection info, TLS state, interception settings, etc.

Interception Modes

The proxy supports three modes for CONNECT requests:

  1. Direct Transfer (shouldIntercept = false): - Transparent TCP tunnel without inspection - No certificate forgery - Minimal overhead

  2. Lazy Attack (default, UpstreamCert = false): - Establishes client tunnel first - Peeks at traffic to detect protocol - Connects to upstream based on SNI

  3. Dial-First Attack (UpstreamCert = true): - Connects to upstream first - Obtains real certificate - Creates matching fake certificate for client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addon

type Addon = types.Addon

Addon defines the interface for proxy addons.

type BaseAddon

type BaseAddon = types.BaseAddon

BaseAddon provides default no-op implementations of all Addon methods.

type ClientConn

type ClientConn = conn.ClientConn

ClientConn represents a client connection.

type ClientFactory

type ClientFactory = types.ClientFactory

ClientFactory is responsible for creating HTTP clients for different scenarios.

type Config

type Config struct {
	Addr               string
	StreamLargeBodies  int64
	InsecureSkipVerify bool
	Upstream           string
	ClientFactory      ClientFactory
}

Config holds the proxy configuration settings.

type ConnContext

type ConnContext = conn.Context

ConnContext represents the connection context.

type DefaultClientFactory

type DefaultClientFactory = types.DefaultClientFactory

DefaultClientFactory is the default implementation of ClientFactory.

func NewDefaultClientFactory

func NewDefaultClientFactory() *DefaultClientFactory

NewDefaultClientFactory creates a new DefaultClientFactory.

type Flow

type Flow = types.Flow

Flow represents a complete HTTP request/response flow.

type InstanceLogger

type InstanceLogger struct {
	InstanceID   string
	InstanceName string
	Port         string
	LogFilePath  string
	// contains filtered or unexported fields
}

func NewInstanceLogger

func NewInstanceLogger(addr, instanceName string) *InstanceLogger

NewInstanceLogger creates a logger with instance identification.

func NewInstanceLoggerWithFile

func NewInstanceLoggerWithFile(addr, instanceName, logFilePath string) *InstanceLogger

NewInstanceLoggerWithFile creates a logger with instance identification and optional file output.

func (*InstanceLogger) GetLogger

func (il *InstanceLogger) GetLogger() *slog.Logger

GetLogger returns the underlying slog logger.

func (*InstanceLogger) WithFields

func (il *InstanceLogger) WithFields(args ...any) *slog.Logger

WithFields adds additional fields to the logger.

type Proxy

type Proxy struct {
	Version string
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(config Config, ca cert.CA) (*Proxy, error)

NewProxy creates a new Proxy with the given configuration and CA. This function creates all internal dependencies with default settings.

func (*Proxy) AddAddon

func (p *Proxy) AddAddon(addon Addon)

func (*Proxy) Close

func (p *Proxy) Close() error

func (*Proxy) GetCertificate

func (p *Proxy) GetCertificate() x509.Certificate

func (*Proxy) GetCertificateByCN

func (p *Proxy) GetCertificateByCN(commonName string) (*tls.Certificate, error)

func (*Proxy) NotifyClientDisconnected

func (p *Proxy) NotifyClientDisconnected(clientConn *conn.ClientConn)

NotifyClientDisconnected implements conn.AddonNotifier interface.

func (*Proxy) NotifyServerDisconnected

func (p *Proxy) NotifyServerDisconnected(connCtx *conn.Context)

NotifyServerDisconnected implements conn.AddonNotifier interface.

func (*Proxy) SetAuthProxy

func (p *Proxy) SetAuthProxy(fn func(res http.ResponseWriter, req *http.Request) (bool, error))

func (*Proxy) SetShouldInterceptRule

func (p *Proxy) SetShouldInterceptRule(rule func(req *http.Request) bool)

func (*Proxy) SetUpstreamProxy

func (p *Proxy) SetUpstreamProxy(fn func(req *http.Request) (*url.URL, error))

func (*Proxy) Shutdown

func (p *Proxy) Shutdown(ctx context.Context) error

func (*Proxy) Start

func (p *Proxy) Start() error

type Request

type Request = types.Request

Request represents an HTTP request in the proxy flow.

type Response

type Response = types.Response

Response represents an HTTP response in the proxy flow.

type ServerConn

type ServerConn = conn.ServerConn

ServerConn represents a server connection.

type UpstreamManager

type UpstreamManager = types.UpstreamManager

UpstreamManager defines the interface for managing upstream proxy connections.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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