diff options
author | baizhenxuan <baizhenxuan@gmail.com> | 2019-12-23 03:48:08 +0800 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2019-12-22 11:48:08 -0800 |
commit | 99fa93bf0ea504413f6e53b46fbefdee4c0a0904 (patch) | |
tree | bad5f25724797dc0a4257c99c2b1dd037a760978 | |
parent | 0133bc188327c19ef0834904c46218a0dad58845 (diff) |
tokio-tls: fix examples build and run (#1963)
-rw-r--r-- | tokio-tls/Cargo.toml | 1 | ||||
-rw-r--r-- | tokio-tls/examples/download-rust-lang.rs (renamed from tokio-tls/examples_old/download-rust-lang.rs) | 0 | ||||
-rw-r--r-- | tokio-tls/examples/echo.rs | 55 | ||||
-rw-r--r-- | tokio-tls/examples/identity.p12 (renamed from tokio-tls/examples_old/identity.p12) | bin | 3386 -> 3386 bytes | |||
-rw-r--r-- | tokio-tls/examples_old/echo.rs | 60 |
5 files changed, 56 insertions, 60 deletions
diff --git a/tokio-tls/Cargo.toml b/tokio-tls/Cargo.toml index 1458be03..d5a5c6de 100644 --- a/tokio-tls/Cargo.toml +++ b/tokio-tls/Cargo.toml @@ -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 } diff --git a/tokio-tls/examples_old/download-rust-lang.rs b/tokio-tls/examples/download-rust-lang.rs index 324c0775..324c0775 100644 --- a/tokio-tls/examples_old/download-rust-lang.rs +++ b/tokio-tls/examples/download-rust-lang.rs diff --git a/tokio-tls/examples/echo.rs b/tokio-tls/examples/echo.rs new file mode 100644 index 00000000..96309567 --- /dev/null +++ b/tokio-tls/examples/echo.rs @@ -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"); + }); + } +} diff --git a/tokio-tls/examples_old/identity.p12 b/tokio-tls/examples/identity.p12 Binary files differindex d16abb8c..d16abb8c 100644 --- a/tokio-tls/examples_old/identity.p12 +++ b/tokio-tls/examples/identity.p12 diff --git a/tokio-tls/examples_old/echo.rs b/tokio-tls/examples_old/echo.rs deleted file mode 100644 index 730e3d9e..00000000 --- a/tokio-tls/examples_old/echo.rs +++ /dev/null @@ -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(()) -} |