This code is modified and derived from tonic. It reduces the complexity of using TLS for your tokio TCP applications.
Usage:
Server:
```rust // you could also build your config with cert and identity separately. See tests. let config: ServerTlsConfig = toml::fromstr(configfile).unwrap(); let acceptor = config.tlsacceptor().unwrap(); let listener = TcpListener::bind(addr).await.unwrap(); tokio::spawn(async move { loop { let (stream, peeraddr) = 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:
```rust 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::fromstr(configfile).unwrap(); let connector = config.tlsconnector(Uri::fromstatic("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.
tokio-tls-helper
is distributed under the terms of MIT.
See LICENSE for details.
Copyright 2021 Tyr Chen