Skip to content

Commit

Permalink
feat: TLS session resumption
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 7, 2024
1 parent 3b2f18f commit 643a3c6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/net/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
use std::sync::Arc;

use anyhow::Result;
use once_cell::sync::Lazy;

use crate::net::session::SessionStream;

use rustls::client::ClientSessionStore;

pub async fn wrap_tls(
strict_tls: bool,
hostname: &str,
Expand All @@ -30,6 +33,10 @@ pub async fn wrap_tls(
}
}

// This is the default as of version 0.23.16, but make it shared between clients.
static RESUMPTION_STORE: Lazy<Arc<dyn ClientSessionStore>> =
Lazy::new(|| Arc::new(rustls::client::ClientSessionMemoryCache::new(256)));

pub async fn wrap_rustls(
hostname: &str,
alpn: &[&str],
Expand All @@ -43,6 +50,9 @@ pub async fn wrap_rustls(
.with_no_client_auth();
config.alpn_protocols = alpn.iter().map(|s| s.as_bytes().to_vec()).collect();

let resumption = rustls::client::Resumption::store(Arc::clone(&RESUMPTION_STORE));
config.resumption = resumption;

let tls = tokio_rustls::TlsConnector::from(Arc::new(config));
let name = rustls_pki_types::ServerName::try_from(hostname)?.to_owned();
let tls_stream = tls.connect(name, stream).await?;
Expand Down

0 comments on commit 643a3c6

Please sign in to comment.