use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::{Arc, OnceLock};
use rustls::pki_types::ServerName;
use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned};
pub enum HttpTransportStream {
Plain(TcpStream),
Tls(Box<StreamOwned<ClientConnection, TcpStream>>),
}
impl Read for HttpTransportStream {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Self::Plain(stream) => stream.read(buf),
Self::Tls(stream) => stream.read(buf),
}
}
}
impl Write for HttpTransportStream {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
Self::Plain(stream) => stream.write(buf),
Self::Tls(stream) => stream.write(buf),
}
}
fn flush(&mut self) -> std::io::Result<()> {
match self {
Self::Plain(stream) => stream.flush(),
Self::Tls(stream) => stream.flush(),
}
}
}
static TLS_CLIENT_CONFIG: OnceLock<Arc<ClientConfig>> = OnceLock::new();
pub fn get_or_init_tls_config() -> Arc<ClientConfig> {
TLS_CLIENT_CONFIG
.get_or_init(|| {
let mut root_store = RootCertStore::empty();
root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let mut config = ClientConfig::builder_with_provider(Arc::new(
rustls::crypto::ring::default_provider(),
))
.with_safe_default_protocol_versions()
.expect("Valid default protocol versions")
.with_root_certificates(root_store)
.with_no_client_auth();
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
Arc::new(config)
})
.clone()
}
use std::time::Duration;
impl HttpTransportStream {
pub fn alpn_protocol(&self) -> Option<&[u8]> {
match self {
Self::Plain(_) => None,
Self::Tls(stream) => stream.conn.alpn_protocol(),
}
}
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> std::io::Result<()> {
match self {
Self::Plain(stream) => stream.set_read_timeout(timeout),
Self::Tls(stream) => stream.sock.set_read_timeout(timeout),
}
}
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> std::io::Result<()> {
match self {
Self::Plain(stream) => stream.set_write_timeout(timeout),
Self::Tls(stream) => stream.sock.set_write_timeout(timeout),
}
}
pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
match self {
Self::Plain(stream) => stream.set_nodelay(nodelay),
Self::Tls(stream) => stream.sock.set_nodelay(nodelay),
}
}
}
pub fn create_tls_stream(
mut tcp_stream: TcpStream,
host: &str,
) -> Result<HttpTransportStream, String> {
let server_name = ServerName::try_from(host.to_string())
.map_err(|e| format!("Invalid DNS hostname for TLS SNI '{host}': {e}"))?;
let config = get_or_init_tls_config();
let mut conn = ClientConnection::new(config, server_name)
.map_err(|e| format!("TLS client handshake initialization failed: {e}"))?;
while conn.is_handshaking() {
conn.complete_io(&mut tcp_stream)
.map_err(|e| format!("TLS handshake failed: {e}"))?;
}
Ok(HttpTransportStream::Tls(Box::new(StreamOwned::new(
conn, tcp_stream,
))))
}