mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-31 00:00:07 +02:00
* use dyn Trait syntax where appropriate recent rust nightly started warning that not using `dyn` was deprecated. This requires MSRV 1.27.0+. * rustfmt fallout from dyn additions * stop explicit allow of rust_2018_idioms * more dyn Trait syntax * drop tokio-macros from 0.1.x workspace Since tokio-macros specifies an edition=2018, we would otherwise require MSRV 1.31.0 to build/test it. And tokio-macros isn't used with tokio 0.1.x. * reactor: narrow tokio-io-pool dev dep to 0.1.4 Since 0.1.5-6 is now a edition=2018 crate, which has effective MSRV 1.31.0. * narrow tempfile dev-dep to avoid MSRV bump tempfile 3.1.0 pulls in rand 0.7.0 and is MSRV 1.32.0 * narrow flate2 dev-dep to avoid MSRV bump flate2 1.0.10-11 have MSRV 1.34.0. github refs: alexcrichton/flate2-rs#207 * fs: drop deprecated tempdir crate use in tests In particular because it pulls in old rand duplicates. Replace use with tempfile::tempdir() which has been available since tempfile 3.0.0. backport-of: #1312 * increase CI MSRV to 1.28.0
60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
// A tiny async TLS echo server with Tokio
|
|
extern crate native_tls;
|
|
extern crate tokio;
|
|
extern crate tokio_tls;
|
|
|
|
use native_tls::Identity;
|
|
use tokio::io;
|
|
use tokio::net::TcpListener;
|
|
use tokio::prelude::*;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Bind the server's socket
|
|
let addr = "127.0.0.1:12345".parse()?;
|
|
let tcp = TcpListener::bind(&addr)?;
|
|
|
|
// Create the TLS acceptor.
|
|
let der = include_bytes!("identity.p12");
|
|
let cert = Identity::from_pkcs12(der, "mypass")?;
|
|
let tls_acceptor =
|
|
tokio_tls::TlsAcceptor::from(native_tls::TlsAcceptor::builder(cert).build()?);
|
|
|
|
// Iterate incoming connections
|
|
let server = tcp
|
|
.incoming()
|
|
.for_each(move |tcp| {
|
|
// Accept the TLS connection.
|
|
let tls_accept = tls_acceptor
|
|
.accept(tcp)
|
|
.and_then(move |tls| {
|
|
// Split up the read and write halves
|
|
let (reader, writer) = tls.split();
|
|
|
|
// Copy the data back to the client
|
|
let conn = io::copy(reader, writer)
|
|
// print what happened
|
|
.map(|(n, _, _)| println!("wrote {} bytes", n))
|
|
// Handle any errors
|
|
.map_err(|err| println!("IO error {:?}", err));
|
|
|
|
// Spawn the future as a concurrent task
|
|
tokio::spawn(conn);
|
|
|
|
Ok(())
|
|
})
|
|
.map_err(|err| {
|
|
println!("TLS accept error: {:?}", err);
|
|
});
|
|
tokio::spawn(tls_accept);
|
|
|
|
Ok(())
|
|
})
|
|
.map_err(|err| {
|
|
println!("server error {:?}", err);
|
|
});
|
|
|
|
// Start the runtime and spin up the server
|
|
tokio::run(server);
|
|
Ok(())
|
|
}
|