28 releases

new 0.4.0 Jan 27, 2025
0.3.0 Jul 12, 2024
0.2.24 May 10, 2024
0.2.20 Jan 16, 2024
0.1.0 May 11, 2023

#97 in Network programming

Download history 3306/week @ 2024-10-09 2897/week @ 2024-10-16 2959/week @ 2024-10-23 2563/week @ 2024-10-30 2409/week @ 2024-11-06 2321/week @ 2024-11-13 3107/week @ 2024-11-20 2833/week @ 2024-11-27 3278/week @ 2024-12-04 3620/week @ 2024-12-11 3260/week @ 2024-12-18 2251/week @ 2024-12-25 2090/week @ 2025-01-01 3047/week @ 2025-01-08 3754/week @ 2025-01-15 3223/week @ 2025-01-22

12,415 downloads per month
Used in 38 crates (3 directly)

MIT license

125KB
3.5K SLoC

rustls-tokio-stream

rustls-tokio-stream is a Rust crate that provides an AsyncRead/AsyncWrite interface for rustls.

Features:

  • Supports duplex I/O via tokio::io::split and other methods out-of-the-box
  • Does not require either read or write polling to perform handshakes

Examples

Create a server and client running on localhost:

  fn server_config() -> ServerConfig {
    ServerConfig::builder()
      .with_safe_defaults()
      .with_no_client_auth()
      .with_single_cert(vec![certificate()], private_key())
      .expect("Failed to build server config")
  }

  fn client_config() -> ClientConfig {
    ClientConfig::builder()
      .with_safe_defaults()
      .with_no_client_auth()
  }

  async fn tcp_pair() -> (TcpStream, TcpStream) {
    let listener = TcpListener::bind(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0)))
      .await
      .unwrap();
    let port = listener.local_addr().unwrap().port();
    let server = spawn(async move { listener.accept().await.unwrap().0 });
    let client = spawn(async move {
      TcpSocket::new_v4()
        .unwrap()
        .connect(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)))
        .await
        .unwrap()
    });

    let (server, client) = (server.await.unwrap(), client.await.unwrap());
    (server, client)
  }

  async fn tls_pair() -> (TlsStream, TlsStream) {
    let (server, client) = tcp_pair().await;
    let server = TlsStream::new_server_side(server, server_config().into());
    let client = TlsStream::new_client_side(
      client,
      client_config().into(),
      "example.com".try_into().unwrap(),
    );

    (server, client)
  }

Dependencies

~10–19MB
~352K SLoC