Skip to main content

tls-switch

A TLS reverse proxy that routes incoming TLS connections to backend servers based on the requested hostname using SNI (Server Name Indication). Supports both TLS termination and TLS passthrough on a per-host basis.

tls-switch sits in front of your services on port 443 and inspects the TLS ClientHello to determine which hostname the client is requesting. SNI is a TLS extension that allows the client to indicate which hostname it is trying to connect to before the TLS handshake completes — this is how tls-switch knows where to route the connection without needing a separate IP address per service. It then routes the connection to the appropriate backend, either terminating TLS and forwarding plaintext, or passing the encrypted stream through unmodified.

Features

  • SNI-based routing — route TLS connections to different backends based on hostname
  • TLS termination — terminate TLS with your certificates and forward plaintext to backends
  • TLS passthrough — forward the raw TLS stream to a backend that handles its own TLS
  • Hot reload — config and certificate changes take effect on new connections without interrupting existing ones
  • Zero buffering — data is forwarded immediately with no processing, filtering, or modification
  • PROXY protocol — optional v1/v2 header emission per host so backends can see the original client IP
  • Zero runtime dependencies — single statically-linked Go binary, no Python or libc required at runtime

Use Cases

  • Run multiple HTTPS services on a single IP address, each with its own certificate
  • Put a TLS-terminating proxy in front of plain HTTP services
  • Route some domains through to their own TLS servers while terminating others locally
  • Consolidate port 443 across multiple services without a full reverse proxy

Requirements

  • Python 3.12+ (only required to install from PyPI; the binary itself has no runtime dependencies)
  • Root/administrator privileges (if binding to port 443)

Installation

pip install tls-switch

Or run directly with uv:

uvx tls-switch

Quick Start

Create a config file (config.json):

{
  "listen": ":443",
  "hosts": {
    "app.example.com": {
      "mode": "terminate",
      "cert": "/etc/tls-switch/app.example.com.crt",
      "key": "/etc/tls-switch/app.example.com.key",
      "backend": "127.0.0.1:8080"
    },
    "legacy.example.com": {
      "mode": "passthrough",
      "backend": "10.0.0.5:443",
      "proxy_protocol": "v2"
    }
  }
}

Run the server:

tls-switch -c config.json

In this example:

  • Connections to app.example.com have TLS terminated by tls-switch, and plaintext HTTP is forwarded to 127.0.0.1:8080
  • Connections to legacy.example.com are forwarded as raw TLS to 10.0.0.5:443, which handles its own certificates

How It Works

  1. A client connects to port 443 and begins a TLS handshake
  2. tls-switch reads the TLS ClientHello message and extracts the SNI (Server Name Indication) hostname
  3. The hostname is looked up in the configuration
  4. Depending on the mode:
    • terminate: tls-switch completes the TLS handshake using the configured certificate and key, then opens a plaintext TCP connection to the backend and copies data bidirectionally with no buffering or processing
    • passthrough: tls-switch opens a TCP connection to the backend, replays the original ClientHello, and then copies data bidirectionally — the backend server handles the TLS handshake itself
  5. If the hostname is not found in the configuration, tls-switch completes a TLS handshake using any available configured certificate and returns an HTTP 421 Misdirected Request error page — browsers display a clear error rather than a cryptic "can't connect" message

Configuration

Config File

The config file is JSON with the following structure:

{
  "listen": ":443",
  "hosts": {
    "hostname": {
      "mode": "terminate|passthrough",
      "cert": "/path/to/cert.pem",
      "key": "/path/to/key.pem",
      "backend": "host:port",
      "proxy_protocol": "v1|v2"
    }
  }
}
Field Description
listen Address to listen on (e.g. :443, 0.0.0.0:8443)
hosts Map of hostname to route configuration
mode terminate (TLS termination) or passthrough (forward raw TLS)
cert Path to PEM certificate file (terminate mode only)
key Path to PEM private key file (terminate mode only)
backend Backend address as host:port
proxy_protocol Optional. v1 (text) or v2 (binary) to emit a PROXY protocol header to the backend so it sees the original client IP. Works in both modes. The backend must be configured to expect it, and only to trust PROXY headers from the tls-switch listener address — otherwise clients can spoof their source IP. Omit to disable (default).

Hot Reload

tls-switch watches the config file and certificate files for changes. When a change is detected:

  • The new config is validated
  • If valid, new connections use the updated config
  • Existing connections continue with their original config until they close naturally
  • If invalid, the change is rejected and the current config remains active

Development

# Set up development environment
make dev

# Cross-compile Go binaries
make go-build

# Run format check and lint
make check

# Auto-format code
make format

# Build wheel and docs
make build

Architecture

tls-switch is a single statically-linked Go binary with no runtime dependencies. It bundles:

  • TCP listener and accept loop
  • TLS ClientHello parsing and SNI extraction
  • TLS termination via the Go crypto/tls standard library
  • Raw passthrough using io.Copy for zero-copy forwarding where supported by the OS
  • Optional PROXY protocol v1/v2 header emission to the backend
  • Config + certificate file watching for hot reload
  • Coloured terminal logging

The binary is built with CGO_ENABLED=0 and works across macOS, Linux, and Windows on amd64 + arm64. It is distributed as platform-specific Python wheels for ease of installation via pip / uvx, but no Python is required to run it.

Licence

Released under the Unlicense — public domain.

Metadata

Release files for tls-switch 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tls-switch 1.2.0
File Actions
tls_switch-1.2.0-py3-none-win_arm64.whl Python 3 none Windows ARM64 Details for tls_switch-1.2.0-py3-none-win_arm64.whl
tls_switch-1.2.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for tls_switch-1.2.0-py3-none-win_amd64.whl
tls_switch-1.2.0-py3-none-manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for tls_switch-1.2.0-py3-none-manylinux_2_17_x86_64.whl
tls_switch-1.2.0-py3-none-manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for tls_switch-1.2.0-py3-none-manylinux_2_17_aarch64.whl
tls_switch-1.2.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for tls_switch-1.2.0-py3-none-macosx_11_0_arm64.whl
tls_switch-1.2.0-py3-none-macosx_10_9_x86_64.whl Python 3 none macOS 10.9+ x86-64 Details for tls_switch-1.2.0-py3-none-macosx_10_9_x86_64.whl

Total release size: 14.5 MB

Release files / tls_switch-1.2.0-py3-none-win_arm64.whl

Download URL tls_switch-1.2.0-py3-none-win_arm64.whl
Size 2.3 MB
Tags Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
7ad20c0a235c3206c2c924dcbed63673fab526cdf384965e20abaa7b6a261c19
BLAKE2b-256 checksum
How to use checksums
2d6aba572ca9ed89783b347a295fce808072585128e82eb206e7f812feb71cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Release files / tls_switch-1.2.0-py3-none-win_amd64.whl

Download URL tls_switch-1.2.0-py3-none-win_amd64.whl
Size 2.6 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
d26b5dc04257406da2a81533accf4852411087e921ee47fe2f3356d8a9a9d55e
BLAKE2b-256 checksum
How to use checksums
457a8102b25d8528e068f5f532335f3d381ff18b622c500821479ac548fea62b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Release files / tls_switch-1.2.0-py3-none-manylinux_2_17_x86_64.whl

Download URL tls_switch-1.2.0-py3-none-manylinux_2_17_x86_64.whl
Size 2.5 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
3ff9fbdd652ecb5f88445e828a97149c97dca2c3e0f0b399bd74d2ed8df0d32c
BLAKE2b-256 checksum
How to use checksums
53c33d3b790da83d91a542debea200e4e99cc8d3525d97c4e12b3804086567e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Release files / tls_switch-1.2.0-py3-none-manylinux_2_17_aarch64.whl

Download URL tls_switch-1.2.0-py3-none-manylinux_2_17_aarch64.whl
Size 2.2 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
038bbff9f20f7540ab452f9d8d5e5ff6b5bebb4fdc97b6b371ca9594bc279dbc
BLAKE2b-256 checksum
How to use checksums
2b953099df805b560c213f54dbcbe0b0bffc60c4cd93e80005147b9d521492d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Release files / tls_switch-1.2.0-py3-none-macosx_11_0_arm64.whl

Download URL tls_switch-1.2.0-py3-none-macosx_11_0_arm64.whl
Size 2.3 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e4cea6d3963ca72eee1cc6f9bb43d5d72c0c6961f8b3de84a8490c0605086e79
BLAKE2b-256 checksum
How to use checksums
458c3e06fa0965c80e200f33680d11f8aa8b08eddedda3a7d574ecfd49feb972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Release files / tls_switch-1.2.0-py3-none-macosx_10_9_x86_64.whl

Download URL tls_switch-1.2.0-py3-none-macosx_10_9_x86_64.whl
Size 2.6 MB
Tags Python 3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
917b3c77eaebd2c4f4ac815e626c501dac12b7c67a723e121a9bb3f3fb58d337
BLAKE2b-256 checksum
How to use checksums
332eac110f70bd2cbd4573eb17a5cbec73c2b7b525b9e262bac3a2a83f2406e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.15.2

Security for tls-switch 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page