SNI Proxy with TCP Multiplexer

0.45.2 · active · verified Thu Apr 16

Snitun is a Python library that provides a Server Name Indication (SNI) proxy with TCP multiplexing capabilities, useful for routing traffic based on the SNI header. It is actively maintained by NabuCasa (the developers behind Home Assistant), currently at version 0.45.2, with a steady release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure and instantiate a `SnitunServer`. It outlines the required `Config` parameters, especially for TLS certificates and routing rules. Note that running a functional TLS proxy requires actual certificate and key files, which are indicated by placeholder paths in this example.

import asyncio
import os
from snitun.config import Config
from snitun.server import SnitunServer

# In a real-world scenario, you would provide paths to your
# actual TLS server certificate and key files.
# For this quickstart, we use placeholder paths. 
# Running this code as-is will likely fail unless these files exist
# and contain valid cert/key pairs for a TLS server.
# You can generate dummy ones or provide real paths via environment variables.
# Example: SNITUN_SERVER_CERT=./server.pem SNITUN_SERVER_KEY=./server.key python your_script.py
DUMMY_CERT = os.environ.get("SNITUN_SERVER_CERT", "/path/to/server.pem")
DUMMY_KEY = os.environ.get("SNITUN_SERVER_KEY", "/path/to/server.key")

async def main():
    # Define the Snitun configuration
    config = Config(
        listen_host="127.0.0.1",
        listen_port=8443,
        server_certs=DUMMY_CERT, # Required for TLS
        server_key=DUMMY_KEY,    # Required for TLS
        routes={
            "example.com": {  # SNI hostname to route
                "host": "192.168.1.100", # Target host
                "port": 443,             # Target port
                "no_verify_ssl": False   # Verify upstream SSL certs
            },
            "another.example.org": {
                "host": "127.0.0.1",
                "port": 8080,
                "no_verify_ssl": True
            }
        }
    )

    # Create an instance of the Snitun server
    server = SnitunServer(config)

    print(f"Snitun server configured to listen on {config.listen_host}:{config.listen_port}")
    print(f"Routes defined: {list(config.routes.keys())}")
    print("\nNOTE: To actually run and test this server, you must ensure valid TLS certificate and key files ")
    print("       are accessible at the configured `server_certs` and `server_key` paths.")
    print("       See Snitun documentation for proper setup.")
    print("\nTo start the server (after ensuring valid certs/keys):")
    print("  await server.start()")
    print("  await asyncio.Future() # Keep running indefinitely")
    print("  await server.stop()")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →