Repository navigation
Description
Summary
It would be great if Sōzu could route a TCP connection to different backend clusters based on the SNI hostname, by prereading the TLS ClientHello, without terminating TLS. The encrypted stream would then be spliced through to the chosen backend untouched (true passthrough), preserving end-to-end TLS — including client-certificate (mTLS) auth — between the client and the backend.
This is the "L4 SNI routing" / "SSL preread" capability that nginx, Envoy, HAProxy and Traefik already offer (links below).
Motivation / use case
Some backends must terminate TLS themselves and cannot delegate it to the proxy — most notably because they authenticate clients with mutual TLS (client certificates). The canonical example is the Kubernetes API server: kubectl/client-go authenticate with a client cert, so the TLS handshake (and the client cert) has to reach the API server itself; a terminating proxy would break auth.
Today, to expose many such mTLS backends you need one IP and/or one port per backend, because the only L4 discriminator is the listening socket. With SNI-preread routing you could host many mTLS backends behind a single IP on a single port (e.g. :443), distinguished by their SNI hostname — e.g. multiple managed-Kubernetes API servers each reachable at https://<name>.example.com:443, all multiplexed by one Sōzu :443 listener. A predictable, firewall-friendly :443 endpoint is a frequent customer ask, and per-IP/per-port allocation doesn't scale (IPv4 exhaustion).
Background: SNI is available in cleartext
The SNI (server_name extension) is carried in the ClientHello, the first handshake message, sent before any encryption is negotiated — so it is in plaintext on the wire, in both TLS 1.2 and TLS 1.3. ALPN is in the same cleartext ClientHello. A proxy can therefore parse SNI/ALPN and make a routing decision with no key material and no decryption.
(Caveat: Encrypted Client Hello (ECH) encrypts the ClientHello. ECH needs explicit client+server+DNS opt-in and is not implemented by common API clients such as kubectl/client-go, so it is out of scope for this request — but worth noting as a future consideration.)
Current Sōzu behavior (the gap)
From the docs, Sōzu frontends split cleanly into:
| Frontend | TLS handling | SNI used for | Suitable for mTLS backends? |
|---|---|---|---|
| TCP | Opaque L4 byte forwarding (supports expect_proxy / PROXY protocol). No ClientHello inspection — routes only by listen address/port. |
nothing | ✅ passthrough, but ❌ cannot route by SNI |
| HTTPS | Terminates TLS (rustls); SNI drives certificate selection (reversed-domain trie) + HTTP/host routing. | cert selection + L7 routing | ❌ termination breaks client-cert mTLS |
So Sōzu currently offers either opaque TCP passthrough (no SNI routing) or full HTTPS termination (SNI routing but decrypts). There is no "preread SNI on a TCP listener and route without terminating" mode.
Proposed feature
On a TCP listener (or a new mode/flag), add an optional ClientHello SNI-preread step:
- Buffer the initial bytes of the connection and parse the TLS
ClientHello. - Extract the
server_name(SNI), and optionally ALPN. - Select the backend cluster by matching SNI against the configured frontends.
- Forward the original, still-encrypted stream to that backend opaquely (passthrough) — Sōzu never decrypts; the client's TLS/mTLS terminates at the backend.
- Define behavior when no SNI is present (e.g. clients connecting by bare IP) — e.g. route to a default backend or reject.
This seems like a natural fit for Sōzu's existing architecture: it already parses the ClientHello SNI and already has the reversed-domain SNI trie (used for certificate selection in HTTPS mode, incl. wildcard handling). This feature would reuse those building blocks to select a backend rather than a certificate, then forward bytes like the TCP path already does.
Prior art
- nginx —
ngx_stream_ssl_preread_module: "allows extracting information from the ClientHello message without terminating SSL/TLS"; route via$ssl_preread_server_name. https://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html - Envoy —
tls_inspectorlistener filter +filter_chain_match.server_names. SNI-based routing without TLS termination envoyproxy/envoy#1843 - HAProxy —
mode tcp+tcp-request inspect-delay+req.ssl_sni. - Traefik — TCP routers with
HostSNI(...).
Open questions / considerations
- Preread buffering / inspect-delay strategy and timeout for incomplete
ClientHellos. - Wildcard SNI matching (the existing cert trie already handles wildcards — can it be reused for backend selection?).
- Optional ALPN-based routing in the same preread (ALPN is also cleartext in the
ClientHello). - Interaction with the PROXY protocol (
expect_proxy) so the backend can still learn the client's address. - Config surface: a new transport/router type on TCP frontends keyed by SNI, analogous to the existing ALPN map on the TLS path.
- TLS 1.3: SNI remains cleartext, so no change there. ECH would defeat SNI-preread (future consideration only).
The key property this unlocks that TLS termination cannot: end-to-end encryption and client-cert mTLS preserved, while still multiplexing many hostnames onto one IP:port. Thanks for considering it!
Activity
feat(tcp): SNI+ALPN ClientHello preread passthrough routing (#1279)
release: 2.2.0