Update example
This commit is contained in:
parent
d42540f52f
commit
34b1bc9c83
@ -22,8 +22,8 @@ config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
|||||||
let config = TlsConnector::from(Arc::new(config));
|
let config = TlsConnector::from(Arc::new(config));
|
||||||
let dnsname = DNSNameRef::try_from_ascii_str("www.rust-lang.org").unwrap();
|
let dnsname = DNSNameRef::try_from_ascii_str("www.rust-lang.org").unwrap();
|
||||||
|
|
||||||
TcpStream::connect(&addr)
|
let stream = TcpStream::connect(&addr).await?;
|
||||||
.and_then(move |socket| config.connect(dnsname, socket))
|
let mut stream = config.connect(dnsname, stream).await?;
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
@ -5,9 +5,9 @@ authors = ["quininer <quininer@live.com>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = { package = "futures-preview", version = "0.3.0-alpha.16", features = ["io-compat"] }
|
futures-util = "0.3"
|
||||||
romio = "0.3.0-alpha.8"
|
tokio = { version = "0.2", features = [ "net", "io-util", "rt-threaded" ] }
|
||||||
structopt = "0.2"
|
structopt = "0.2"
|
||||||
tokio-rustls = { path = "../.." }
|
tokio-rustls = { path = "../.." }
|
||||||
webpki-roots = "0.16"
|
webpki-roots = "0.18"
|
||||||
tokio-stdin-stdout = "0.1"
|
tokio-stdin-stdout = "0.1"
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
#![feature(async_await)]
|
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
use futures_util::future;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use romio::TcpStream;
|
use tokio::runtime;
|
||||||
use futures::prelude::*;
|
use tokio::net::TcpStream;
|
||||||
use futures::executor;
|
use tokio::io::{ AsyncWriteExt, copy, split };
|
||||||
use futures::compat::{ AsyncRead01CompatExt, AsyncWrite01CompatExt };
|
|
||||||
use tokio_rustls::{ TlsConnector, rustls::ClientConfig, webpki::DNSNameRef };
|
use tokio_rustls::{ TlsConnector, rustls::ClientConfig, webpki::DNSNameRef };
|
||||||
use tokio_stdin_stdout::{ stdin as tokio_stdin, stdout as tokio_stdout };
|
use tokio_stdin_stdout::{ stdin as tokio_stdin, stdout as tokio_stdout };
|
||||||
|
|
||||||
@ -46,6 +44,10 @@ fn main() -> io::Result<()> {
|
|||||||
domain
|
domain
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut runtime = runtime::Builder::new()
|
||||||
|
.basic_scheduler()
|
||||||
|
.enable_io()
|
||||||
|
.build()?;
|
||||||
let mut config = ClientConfig::new();
|
let mut config = ClientConfig::new();
|
||||||
if let Some(cafile) = &options.cafile {
|
if let Some(cafile) = &options.cafile {
|
||||||
let mut pem = BufReader::new(File::open(cafile)?);
|
let mut pem = BufReader::new(File::open(cafile)?);
|
||||||
@ -58,6 +60,8 @@ fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
let fut = async {
|
let fut = async {
|
||||||
let stream = TcpStream::connect(&addr).await?;
|
let stream = TcpStream::connect(&addr).await?;
|
||||||
|
|
||||||
|
// TODO tokio-compat
|
||||||
let (mut stdin, mut stdout) = (tokio_stdin(0).compat(), tokio_stdout(0).compat());
|
let (mut stdin, mut stdout) = (tokio_stdin(0).compat(), tokio_stdout(0).compat());
|
||||||
|
|
||||||
let domain = DNSNameRef::try_from_ascii_str(&domain)
|
let domain = DNSNameRef::try_from_ascii_str(&domain)
|
||||||
@ -66,14 +70,14 @@ fn main() -> io::Result<()> {
|
|||||||
let mut stream = connector.connect(domain, stream).await?;
|
let mut stream = connector.connect(domain, stream).await?;
|
||||||
stream.write_all(content.as_bytes()).await?;
|
stream.write_all(content.as_bytes()).await?;
|
||||||
|
|
||||||
let (mut reader, mut writer) = stream.split();
|
let (mut reader, mut writer) = split(stream);
|
||||||
future::try_join(
|
future::try_join(
|
||||||
reader.copy_into(&mut stdout),
|
copy(&mut reader, &mut stdout),
|
||||||
stdin.copy_into(&mut writer)
|
copy(&mut stdin, &mut writer)
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
executor::block_on(fut)
|
runtime.block_on(fut)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ authors = ["quininer <quininer@live.com>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = { package = "futures-preview", version = "0.3.0-alpha.16" }
|
futures-util = "0.3"
|
||||||
romio = "0.3.0-alpha.8"
|
tokio = { version = "0.2", features = [ "net", "io-util", "rt-threaded" ] }
|
||||||
structopt = "0.2"
|
structopt = "0.2"
|
||||||
tokio-rustls = { path = "../.." }
|
tokio-rustls = { path = "../.." }
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
#![feature(async_await)]
|
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use std::path::{ PathBuf, Path };
|
use std::path::{ PathBuf, Path };
|
||||||
use std::io::{ self, BufReader };
|
use std::io::{ self, BufReader };
|
||||||
|
use futures_util::future::TryFutureExt;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use futures::task::SpawnExt;
|
use tokio::runtime;
|
||||||
use futures::prelude::*;
|
use tokio::net::TcpListener;
|
||||||
use futures::executor;
|
use tokio::io::{ AsyncWriteExt, copy, split };
|
||||||
use romio::TcpListener;
|
|
||||||
use tokio_rustls::rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig };
|
use tokio_rustls::rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig };
|
||||||
use tokio_rustls::rustls::internal::pemfile::{ certs, rsa_private_keys };
|
use tokio_rustls::rustls::internal::pemfile::{ certs, rsa_private_keys };
|
||||||
use tokio_rustls::TlsAcceptor;
|
use tokio_rustls::TlsAcceptor;
|
||||||
@ -53,27 +51,30 @@ fn main() -> io::Result<()> {
|
|||||||
let mut keys = load_keys(&options.key)?;
|
let mut keys = load_keys(&options.key)?;
|
||||||
let flag_echo = options.echo;
|
let flag_echo = options.echo;
|
||||||
|
|
||||||
let mut pool = executor::ThreadPool::new()?;
|
let mut runtime = runtime::Builder::new()
|
||||||
|
.threaded_scheduler()
|
||||||
|
.enable_io()
|
||||||
|
.build()?;
|
||||||
|
let handle = runtime.handle().clone();
|
||||||
let mut config = ServerConfig::new(NoClientAuth::new());
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
||||||
config.set_single_cert(certs, keys.remove(0))
|
config.set_single_cert(certs, keys.remove(0))
|
||||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||||
let acceptor = TlsAcceptor::from(Arc::new(config));
|
let acceptor = TlsAcceptor::from(Arc::new(config));
|
||||||
|
|
||||||
let fut = async {
|
let fut = async {
|
||||||
let mut listener = TcpListener::bind(&addr)?;
|
let mut listener = TcpListener::bind(&addr).await?;
|
||||||
let mut incoming = listener.incoming();
|
|
||||||
|
|
||||||
while let Some(stream) = incoming.next().await {
|
loop {
|
||||||
|
let (stream, peer_addr) = listener.accept().await?;
|
||||||
let acceptor = acceptor.clone();
|
let acceptor = acceptor.clone();
|
||||||
|
|
||||||
let fut = async move {
|
let fut = async move {
|
||||||
let stream = stream?;
|
|
||||||
let peer_addr = stream.peer_addr()?;
|
|
||||||
let mut stream = acceptor.accept(stream).await?;
|
let mut stream = acceptor.accept(stream).await?;
|
||||||
|
|
||||||
if flag_echo {
|
if flag_echo {
|
||||||
let (mut reader, mut writer) = stream.split();
|
let (mut reader, mut writer) = split(stream);
|
||||||
let n = reader.copy_into(&mut writer).await?;
|
let n = copy(&mut reader, &mut writer).await?;
|
||||||
|
writer.flush().await?;
|
||||||
println!("Echo: {} - {}", peer_addr, n);
|
println!("Echo: {} - {}", peer_addr, n);
|
||||||
} else {
|
} else {
|
||||||
stream.write_all(
|
stream.write_all(
|
||||||
@ -90,11 +91,9 @@ fn main() -> io::Result<()> {
|
|||||||
Ok(()) as io::Result<()>
|
Ok(()) as io::Result<()>
|
||||||
};
|
};
|
||||||
|
|
||||||
pool.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err))).unwrap();
|
handle.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
};
|
};
|
||||||
|
|
||||||
executor::block_on(fut)
|
runtime.block_on(fut)
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ use futures_util::future::TryFutureExt;
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
use tokio::runtime;
|
use tokio::runtime;
|
||||||
use tokio::net::{ TcpListener, TcpStream };
|
|
||||||
use tokio::io::{ copy, split };
|
use tokio::io::{ copy, split };
|
||||||
|
use tokio::net::{ TcpListener, TcpStream };
|
||||||
use rustls::{ ServerConfig, ClientConfig };
|
use rustls::{ ServerConfig, ClientConfig };
|
||||||
use rustls::internal::pemfile::{ certs, rsa_private_keys };
|
use rustls::internal::pemfile::{ certs, rsa_private_keys };
|
||||||
use tokio_rustls::{ TlsConnector, TlsAcceptor };
|
use tokio_rustls::{ TlsConnector, TlsAcceptor };
|
||||||
|
Loading…
Reference in New Issue
Block a user