local

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 35 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package local implements a low-level forwarding handler for raw TCP and UDP connections. It serves as the fallback handler for protocols that cannot be matched to a dedicated handler, and supports optional protocol sniffing to detect HTTP and TLS traffic for deep inspection (MITM, WebSocket recording).

The handler is registered under the names "tcp", "udp", and "forward" via NewHandler in init().

Connection processing flow

Each inbound net.Conn is processed by Handle, which wraps the connection with I/O stats, checks the rate limiter, and dispatches to one of two forwarding paths:

Handle()
  ├─ stats_wrapper.WrapConn (per-connection I/O counters)
  ├─ newRecorderObject (session metadata: service, SID, addresses)
  ├─ checkRateLimit (connection rate limiter, if configured)
  ├─ Router nil check → errRouterNotAvailable
  ├─ [sniffing enabled] sniffing.Sniff (peek buffer, detect protocol)
  │     └─ handleSniffedProtocol()
  │           ├─ sniffing.ProtoHTTP → SnifferBuilder.Build().HandleHTTP()
  │           ├─ sniffing.ProtoTLS  → SnifferBuilder.Build().HandleTLS()
  │           └─ default → fall through to raw forwarding
  └─ [sniffing disabled / unrecognised] handleRawForwarding()

Protocol sniffing dispatch (handleSniffedProtocol)

When sniffing is enabled and the initial bytes match HTTP or TLS, the connection is delegated to the forwarder package for protocol-aware handling:

  • HTTP: parses the request, applies MITM if configured, forwards to the upstream, and records WebSocket frames when enabled.
  • TLS: parses the ClientHello to extract the server name, applies MITM or SNI-based routing through the hop, and records the TLS handshake.

SnifferBuilder is populated once during Init and reused via Build() to create per-connection forwarder.Sniffer instances.

Raw forwarding (handleRawForwarding)

The default path for unrecognised traffic. It selects a target node from the hop, dials the upstream through the router, wraps the connection with proxy protocol (HAProxy) when enabled, and pipes data bidirectionally:

  1. Node selection — getHop().Select() with optional protocol hint.
  2. Router dial — Router.Dial() through the configured chain.
  3. Proxy protocol — proxyproto.WrapClientConn() prepends HAProxy header when metadata proxyProtocol is set.
  4. Bidirectional pipe — xnet.Pipe(ctx, conn, cc) with read timeout.

Hop management

The handler implements handler.Forwarder. Forward() is called during service startup (and on reload) to install or update the hop. The hop is protected by a mutex because Forward() runs on the loader goroutine while Handle() reads it from per-connection goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(opts ...handler.Option) handler.Handler

NewHandler creates a local forwarding handler with the given options. The handler registers for "tcp", "udp", and "forward" protocols.

Types

type SnifferBuilder added in v0.28.1

type SnifferBuilder struct {
	Websocket           bool
	WebsocketSampleRate float64
	Recorder            recorder.Recorder
	RecorderOptions     *recorder.Options
	Certificate         *x509.Certificate
	PrivateKey          crypto.PrivateKey
	ALPN                string
	CertPool            tls_util.CertPool
	MitmBypass          bypass.Bypass
	// ReadTimeout is the timeout for reading upstream HTTP response headers
	// and TLS ServerHello during sniffing. Passed through to forwarder.Sniffer.
	// See forwarder.Sniffer.ReadTimeout for details.
	ReadTimeout time.Duration
	// Cache is the HTTP response cache (nil when caching is disabled).
	Cache *httpcache.Cache
}

SnifferBuilder holds configuration for creating per-connection protocol sniffers. It is populated once during Init and reused for each connection.

func (*SnifferBuilder) Build added in v0.28.1

func (b *SnifferBuilder) Build() *forwarder.Sniffer

Build creates a new forwarder.Sniffer from the builder's configuration.

Jump to

Keyboard shortcuts

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