Proxy TLS SNI
SNI le permite a OpenSSL elegir el certificado correcto según el nombre de host que solicitó el cliente. tls_server_add_cert registra dominios adicionales además del certificado por defecto. Esto permite que una sola IP:443 sirva muchos sitios HTTPS.
Código
// nyx-proxy TLS with SNI — multi-domain HTTPS on port 443
// TLS con SNI nyx-proxy — HTTPS multi-dominio en puerto 443
fn main() -> int {
// SNI (Server Name Indication) lets one IP:443 serve multiple domains
// with different TLS certificates. The client sends the hostname in
// the TLS handshake, and the server picks the matching cert.
// Primary certificate (default)
let default_cert: String = "/etc/letsencrypt/live/nyxlang.com/fullchain.pem"
let default_key: String = "/etc/letsencrypt/live/nyxlang.com/privkey.pem"
// Initialize TLS server with default cert
// tls_server_init(default_cert, default_key)
// Add additional certs for other domains
// tls_server_add_cert("/etc/letsencrypt/live/nyxkv.com/fullchain.pem",
// "/etc/letsencrypt/live/nyxkv.com/privkey.pem")
// tls_server_add_cert("/etc/letsencrypt/live/serve.nyxlang.com/fullchain.pem",
// "/etc/letsencrypt/live/serve.nyxlang.com/privkey.pem")
print("SNI config example:")
print(" default: nyxlang.com")
print(" +cert: nyxkv.com")
print(" +cert: serve.nyxlang.com")
print(" +cert: proxy.nyxlang.com")
print("")
print("OpenSSL selects the cert based on the client's SNI hint.")
print("This is how nyx-proxy serves 4 domains on one port :443.")
return 0
}
Salida
SNI config example: default: nyxlang.com +cert: nyxkv.com +cert: serve.nyxlang.com +cert: proxy.nyxlang.com OpenSSL selects the cert based on the client's SNI hint. This is how nyx-proxy serves 4 domains on one port :443.
Explicación
Antes de SNI, cada sitio HTTPS necesitaba su propia dirección IP porque el certificado debía elegirse antes de que el cliente pudiera decir qué dominio quería — un problema del huevo y la gallina incrustado en el handshake de TLS. SNI (RFC 6066) resolvió eso permitiendo que el cliente envíe el nombre de host en el primer paquete ClientHello, en claro, para que el servidor pueda elegir el certificado correspondiente antes de cifrar cualquier otra cosa. tls_server_init establece el certificado por defecto y tls_server_add_cert agrega más; por debajo, OpenSSL mantiene una tabla de hostname a SSL_CTX y cambia de contexto en cada handshake. Esta es la magia que le permite a nyx-proxy servir nyxlang.com, nyxkv.com, serve.nyxlang.com y proxy.nyxlang.com, todos desde una sola IPv4 en el puerto 443.