2017-02-22 03:42:32 +00:00
|
|
|
extern crate clap;
|
|
|
|
extern crate rustls;
|
|
|
|
extern crate futures;
|
2017-03-16 02:15:06 +00:00
|
|
|
extern crate tokio_io;
|
2018-02-28 06:36:37 +00:00
|
|
|
extern crate tokio;
|
2017-02-22 03:42:32 +00:00
|
|
|
extern crate webpki_roots;
|
|
|
|
extern crate tokio_rustls;
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
use std::io::BufReader;
|
|
|
|
use std::fs::File;
|
|
|
|
use futures::{ Future, Stream };
|
2017-09-03 22:58:55 +00:00
|
|
|
use rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig };
|
2017-02-22 03:42:32 +00:00
|
|
|
use rustls::internal::pemfile::{ certs, rsa_private_keys };
|
2017-03-16 02:15:06 +00:00
|
|
|
use tokio_io::{ io, AsyncRead };
|
2018-02-28 06:36:37 +00:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::executor::current_thread;
|
2017-02-22 03:42:32 +00:00
|
|
|
use clap::{ App, Arg };
|
2017-02-22 05:30:01 +00:00
|
|
|
use tokio_rustls::ServerConfigExt;
|
2017-02-22 03:42:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
fn app() -> App<'static, 'static> {
|
|
|
|
App::new("server")
|
|
|
|
.about("tokio-rustls server example")
|
|
|
|
.arg(Arg::with_name("addr").value_name("ADDR").required(true))
|
|
|
|
.arg(Arg::with_name("cert").short("c").long("cert").value_name("FILE").help("cert file.").required(true))
|
|
|
|
.arg(Arg::with_name("key").short("k").long("key").value_name("FILE").help("key file, rsa only.").required(true))
|
|
|
|
.arg(Arg::with_name("echo").short("e").long("echo-mode").help("echo mode."))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_certs(path: &str) -> Vec<Certificate> {
|
|
|
|
certs(&mut BufReader::new(File::open(path).unwrap())).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_keys(path: &str) -> Vec<PrivateKey> {
|
|
|
|
rsa_private_keys(&mut BufReader::new(File::open(path).unwrap())).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let matches = app().get_matches();
|
|
|
|
|
|
|
|
let addr = matches.value_of("addr").unwrap()
|
|
|
|
.to_socket_addrs().unwrap()
|
|
|
|
.next().unwrap();
|
|
|
|
let cert_file = matches.value_of("cert").unwrap();
|
|
|
|
let key_file = matches.value_of("key").unwrap();
|
|
|
|
let flag_echo = matches.occurrences_of("echo") > 0;
|
|
|
|
|
2017-09-03 22:58:55 +00:00
|
|
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
2017-02-22 03:42:32 +00:00
|
|
|
config.set_single_cert(load_certs(cert_file), load_keys(key_file).remove(0));
|
|
|
|
let arc_config = Arc::new(config);
|
|
|
|
|
2018-02-28 06:36:37 +00:00
|
|
|
let socket = TcpListener::bind(&addr).unwrap();
|
2017-02-22 03:42:32 +00:00
|
|
|
let done = socket.incoming()
|
2018-02-28 06:36:37 +00:00
|
|
|
.for_each(move |stream| if flag_echo {
|
|
|
|
let addr = stream.peer_addr().ok();
|
|
|
|
let addr2 = addr.clone();
|
2017-02-22 03:42:32 +00:00
|
|
|
let done = arc_config.accept_async(stream)
|
|
|
|
.and_then(|stream| {
|
|
|
|
let (reader, writer) = stream.split();
|
|
|
|
io::copy(reader, writer)
|
|
|
|
})
|
2018-02-28 06:36:37 +00:00
|
|
|
.map(move |(n, ..)| println!("Echo: {} - {:?}", n, addr))
|
|
|
|
.map_err(move |err| println!("Error: {:?} - {:?}", err, addr2));
|
|
|
|
current_thread::spawn(done);
|
2017-02-22 03:42:32 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2018-02-28 06:36:37 +00:00
|
|
|
let addr = stream.peer_addr().ok();
|
|
|
|
let addr2 = addr.clone();
|
2017-02-22 03:42:32 +00:00
|
|
|
let done = arc_config.accept_async(stream)
|
|
|
|
.and_then(|stream| io::write_all(
|
|
|
|
stream,
|
2017-04-14 04:43:03 +00:00
|
|
|
&b"HTTP/1.0 200 ok\r\n\
|
2017-02-22 03:42:32 +00:00
|
|
|
Connection: close\r\n\
|
|
|
|
Content-length: 12\r\n\
|
|
|
|
\r\n\
|
2017-04-14 04:43:03 +00:00
|
|
|
Hello world!"[..]
|
2017-02-22 03:42:32 +00:00
|
|
|
))
|
2017-02-28 00:53:52 +00:00
|
|
|
.and_then(|(stream, _)| io::flush(stream))
|
2018-02-28 06:36:37 +00:00
|
|
|
.map(move |_| println!("Accept: {:?}", addr))
|
|
|
|
.map_err(move |err| println!("Error: {:?} - {:?}", err, addr2));
|
|
|
|
current_thread::spawn(done);
|
2017-02-22 03:42:32 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2018-02-28 06:36:37 +00:00
|
|
|
current_thread::run(|_| current_thread::spawn(done.map_err(drop)));
|
2017-02-22 03:42:32 +00:00
|
|
|
}
|