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