2018-08-19 19:47:04 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_net;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate openssl;
|
|
|
|
extern crate tokio_io;
|
|
|
|
extern crate tokio_tcp;
|
|
|
|
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use futures::{future, Future};
|
|
|
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
|
2018-09-11 19:55:49 +02:00
|
|
|
use actix_net::server::Server;
|
|
|
|
use actix_net::service::NewServiceExt;
|
|
|
|
use actix_net::ssl;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ServiceState {
|
|
|
|
num: Arc<AtomicUsize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn service<T: AsyncRead + AsyncWrite>(
|
2018-08-22 02:08:23 +02:00
|
|
|
st: &mut ServiceState, _: T,
|
2018-09-08 23:50:16 +02:00
|
|
|
) -> impl Future<Item = (), Error = ()> {
|
2018-08-19 19:47:04 +02:00
|
|
|
let num = st.num.fetch_add(1, Ordering::Relaxed);
|
|
|
|
println!("got ssl connection {:?}", num);
|
|
|
|
future::ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let sys = actix::System::new("test");
|
|
|
|
|
|
|
|
// load ssl keys
|
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
|
|
|
builder
|
|
|
|
.set_private_key_file("./examples/key.pem", SslFiletype::PEM)
|
|
|
|
.unwrap();
|
|
|
|
builder
|
|
|
|
.set_certificate_chain_file("./examples/cert.pem")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
2018-09-08 23:50:16 +02:00
|
|
|
let openssl = ssl::OpensslAcceptor::new(builder.build());
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-08-23 22:39:13 +02:00
|
|
|
// server start mutiple workers, it runs supplied `Fn` in each worker.
|
2018-08-24 00:42:34 +02:00
|
|
|
Server::default()
|
|
|
|
.bind("0.0.0.0:8443", move || {
|
|
|
|
let num = num.clone();
|
|
|
|
|
|
|
|
// configure service
|
2018-08-27 23:20:41 +02:00
|
|
|
openssl
|
|
|
|
.clone()
|
2018-09-08 23:50:16 +02:00
|
|
|
.map_err(|e| println!("Openssl error: {}", e))
|
|
|
|
.and_then((service, move || Ok(ServiceState { num: num.clone() })))
|
2018-08-24 00:42:34 +02:00
|
|
|
}).unwrap()
|
|
|
|
.start();
|
2018-08-19 19:47:04 +02:00
|
|
|
|
|
|
|
sys.run();
|
|
|
|
}
|