Documentation
¶
Overview ¶
Package sni implements a TLS SNI-based forwarding handler for protocol-aware routing. It sniffs every inbound TCP connection, parses the TLS ClientHello (or HTTP request header) to extract the server name, and routes traffic through the proxy chain.
How it differs from the "tcp" / "forward" handler ¶
The SNI handler is purpose-built for TLS SNI routing — it always sniffs and only handles HTTP and TLS traffic:
- Non-HTTP/non-TLS connections are silently dropped (no raw forwarding fallback). If you need raw TCP fallback, use the "tcp" or "forward" handler instead.
- It does not implement handler.Forwarder, so it has no hop-based node selection, load balancing, or per-node authentication/rewrite settings. All routing uses the SNI hostname as the destination address through the configured chain.
Connection processing flow ¶
Each inbound net.Conn is processed by Handle:
Handle() ├─ newRecorderObject (session metadata: service, SID, addresses) ├─ checkRateLimit (connection rate limiter, if configured) ├─ Router nil check → errRouterNotAvailable ├─ sniffing.Sniff (peek buffer, detect protocol) │ └─ handleSniffedProtocol() │ ├─ sniffing.ProtoHTTP → SnifferBuilder.Build().HandleHTTP() │ ├─ sniffing.ProtoTLS → SnifferBuilder.Build().HandleTLS() │ └─ default → silent drop └─ unrecognised protocol → silently returns nil
SnifferBuilder pattern ¶
The sniffer configuration (MITM certs, WebSocket recording, bypass rules) is immutable after Init. A SnifferBuilder is populated once during Init and reused via SnifferBuilder.Build to create per-connection sniffing.Sniffer instances.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 [sniffing.Sniffer].
// See sniffing.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 via SnifferBuilder.Build for each connection to avoid repeated allocation.
func (*SnifferBuilder) Build ¶ added in v0.28.1
func (b *SnifferBuilder) Build() *sniffing.Sniffer
Build creates a new sniffing.Sniffer from the builder's configuration. Each call produces a fresh instance, safe for per-connection use.