sni

package
v0.28.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 27 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 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

func NewHandler

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

NewHandler creates an SNI handler with the given options. The handler registers for the "sni" protocol.

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.

Jump to

Keyboard shortcuts

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