tokio-tls-helper

Crates.iotokio-tls-helper
lib.rstokio-tls-helper
version0.3.0
sourcesrc
created_at2021-03-18 06:25:25.678211
updated_at2021-03-24 04:24:52.163681
descriptionUtility tool to make TLS easy to use. Most of the code is extracted from tonic.
homepagehttps://github.com/tyrchen/tokio-tls-helper
repositoryhttps://github.com/tyrchen/tokio-tls-helper
max_upload_size
id370434
size44,417
Tyr Chen (tyrchen)

documentation

https://docs.rs/tokio-tls-helper

README

Tokio TLS Helper

This code is modified and derived from tonic. It reduces the complexity of using TLS for your tokio TCP applications.

Usage:

Server:

// you could also build your config with cert and identity separately. See tests.
let config: ServerTlsConfig = toml::from_str(config_file).unwrap();
let acceptor = config.tls_acceptor().unwrap();
let listener = TcpListener::bind(addr).await.unwrap();
tokio::spawn(async move {
    loop {
        let (stream, peer_addr) = listener.accept().await.unwrap();
        let stream = acceptor.accept(stream).await.unwrap();
        info!("server: Accepted client conn with TLS");

        let fut = async move {
            let (mut reader, mut writer) = split(stream);
            let n = copy(&mut reader, &mut writer).await?;
            writer.flush().await?;
            debug!("Echo: {} - {}", peer_addr, n);
        }

        tokio::spawn(async move {
            if let Err(err) = fut.await {
                error!("{:?}", err);
            }
        });
    }
});

Client:

let msg = b"Hello world\n";
let mut buf = [0; 12];

// you could also build your config with cert and identity separately. See tests.
let config: ClientTlsConfig = toml::from_str(config_file).unwrap();
let connector = config.tls_connector(Uri::from_static("localhost")).unwrap();

let stream = TcpStream::connect(addr).await.unwrap();
let mut stream = connector.connect(stream).await.unwrap();
info!("client: TLS conn established");

stream.write_all(msg).await.unwrap();

info!("client: send data");

let (mut reader, _writer) = split(stream);

reader.read_exact(buf).await.unwrap();

info!("client: read echoed data");

Note TLS is one of many choices to secure your TCP connections, you may also consider snow which implemented Noise protocol.

License

tokio-tls-helper is distributed under the terms of MIT.

See LICENSE for details.

Copyright 2021 Tyr Chen

Commit count: 9

cargo fmt