tokio-tls: fix examples build and run (#1963)

This commit is contained in:
baizhenxuan 2019-12-23 03:48:08 +08:00 committed by Carl Lerche
parent 0133bc1883
commit 99fa93bf0e
5 changed files with 56 additions and 60 deletions

View File

@ -30,6 +30,7 @@ tokio = { version = "0.2.0", path = "../tokio" }
[dev-dependencies]
tokio = { version = "0.2.0", path = "../tokio", features = ["macros", "stream", "rt-core", "io-util", "net"] }
tokio-util = { version = "0.2.0", path = "../tokio-util", features = ["full"] }
cfg-if = "0.1"
env_logger = { version = "0.6", default-features = false }

View File

@ -0,0 +1,55 @@
#![warn(rust_2018_idioms)]
// A tiny async TLS echo server with Tokio
use native_tls;
use native_tls::Identity;
use tokio;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio_tls;
/**
an example to setup a tls server.
how to test:
wget https://127.0.0.1:12345 --no-check-certificate
*/
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Bind the server's socket
let addr = "127.0.0.1:12345".to_string();
let mut tcp: TcpListener = TcpListener::bind(&addr).await?;
// 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()?);
loop {
// Asynchronously wait for an inbound socket.
let (socket, remote_addr) = tcp.accept().await?;
let tls_acceptor = tls_acceptor.clone();
println!("accept connection from {}", remote_addr);
tokio::spawn(async move {
// Accept the TLS connection.
let mut tls_stream = tls_acceptor.accept(socket).await.expect("accept error");
// In a loop, read data from the socket and write the data back.
let mut buf = [0; 1024];
let n = tls_stream
.read(&mut buf)
.await
.expect("failed to read data from socket");
if n == 0 {
return;
}
println!("read={}", unsafe {
String::from_utf8_unchecked(buf[0..n].into())
});
tls_stream
.write_all(&buf[0..n])
.await
.expect("failed to write data to socket");
});
}
}

View File

@ -1,60 +0,0 @@
#![warn(rust_2018_idioms)]
// A tiny async TLS echo server with Tokio
use native_tls;
use native_tls::Identity;
use tokio;
use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
use tokio_tls;
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(())
}