Skip to content

Commit

Permalink
restore rust-tls support
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Sep 28, 2018
1 parent ecfda64 commit 1ff86e5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ script:
- |
if [[ "$TRAVIS_RUST_VERSION" != "stable" ]]; then
cargo clean
cargo test --features="ssl,tls" -- --nocapture
cargo test --features="ssl,tls,rust-tls" -- --nocapture
fi
- |
if [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install -f cargo-tarpaulin
cargo tarpaulin --features="ssl,tls" --out Xml --no-count
cargo tarpaulin --features="ssl,tls,rust-tls" --out Xml --no-count
bash <(curl -s https://codecov.io/bash)
echo "Uploaded code coverage"
fi
Expand All @@ -46,7 +46,7 @@ script:
after_success:
- |
if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" = "false" && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_RUST_VERSION" == "beta" ]]; then
cargo doc --features "ssl,session" --no-deps &&
cargo doc --features "ssl,tls,rust-tls,session" --no-deps &&
echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html &&
git clone https://github.com/davisp/ghp-import.git &&
./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://"$GH_TOKEN"@github.com/"$TRAVIS_REPO_SLUG.git" target/doc &&
Expand Down
50 changes: 33 additions & 17 deletions src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use native_tls::TlsAcceptor;
#[cfg(any(feature = "alpn", feature = "ssl"))]
use openssl::ssl::SslAcceptorBuilder;

//#[cfg(feature = "rust-tls")]
//use rustls::ServerConfig;
#[cfg(feature = "rust-tls")]
use rustls::ServerConfig;

use super::acceptor::{AcceptorServiceFactory, DefaultAcceptor};
use super::builder::DefaultPipelineFactory;
Expand Down Expand Up @@ -313,22 +313,38 @@ where
Ok(self)
}

// #[cfg(feature = "rust-tls")]
// /// Use listener for accepting incoming tls connection requests
// ///
// /// This method sets alpn protocols to "h2" and "http/1.1"
// pub fn listen_rustls(self, lst: net::TcpListener, builder: ServerConfig) -> Self {
// use super::{RustlsAcceptor, ServerFlags};
#[cfg(feature = "rust-tls")]
/// Use listener for accepting incoming tls connection requests
///
/// This method sets alpn protocols to "h2" and "http/1.1"
pub fn listen_rustls(mut self, lst: net::TcpListener, config: ServerConfig) -> Self {
use super::{RustlsAcceptor, ServerFlags};
use actix_net::service::NewServiceExt;

// // alpn support
// let flags = if self.no_http2 {
// ServerFlags::HTTP1
// } else {
// ServerFlags::HTTP1 | ServerFlags::HTTP2
// };
//
// self.listen_with(lst, RustlsAcceptor::with_flags(builder, flags))
// }
// alpn support
let flags = if self.no_http2 {
ServerFlags::HTTP1
} else {
ServerFlags::HTTP1 | ServerFlags::HTTP2
};

let addr = lst.local_addr().unwrap();
self.sockets.push(Socket {
lst,
addr,
scheme: "https",
handler: Box::new(HttpServiceBuilder::new(
self.factory.clone(),
move || {
RustlsAcceptor::with_flags(config.clone(), flags).map_err(|_| ())
},
DefaultPipelineFactory::new(),
)),
});

//Ok(self)
self
}

/// The socket address to bind
///
Expand Down
8 changes: 4 additions & 4 deletions src/server/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use self::openssl::*;
#[cfg(feature = "tls")]
mod nativetls;

//#[cfg(feature = "rust-tls")]
//mod rustls;
//#[cfg(feature = "rust-tls")]
//pub use self::rustls::RustlsAcceptor;
#[cfg(feature = "rust-tls")]
mod rustls;
#[cfg(feature = "rust-tls")]
pub use self::rustls::RustlsAcceptor;
43 changes: 12 additions & 31 deletions src/server/ssl/rustls.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
use std::net::Shutdown;
use std::sync::Arc;
use std::{io, time};

use actix_net::ssl; //::RustlsAcceptor;
use rustls::{ClientSession, ServerConfig, ServerSession};
use tokio_io::AsyncWrite;
use tokio_rustls::{AcceptAsync, ServerConfigExt, TlsStream};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsStream;

use server::{AcceptorService, IoStream, ServerFlags};
use server::{IoStream, ServerFlags};

#[derive(Clone)]
/// Support `SSL` connections via rustls package
///
/// `rust-tls` feature enables `RustlsAcceptor` type
pub struct RustlsAcceptor {
config: Arc<ServerConfig>,
pub struct RustlsAcceptor<T> {
_t: ssl::RustlsAcceptor<T>,
}

impl RustlsAcceptor {
/// Create `OpensslAcceptor` with enabled `HTTP/2` and `HTTP1.1` support.
pub fn new(config: ServerConfig) -> Self {
RustlsAcceptor::with_flags(config, ServerFlags::HTTP1 | ServerFlags::HTTP2)
}

/// Create `OpensslAcceptor` with custom server flags.
pub fn with_flags(mut config: ServerConfig, flags: ServerFlags) -> Self {
impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
/// Create `RustlsAcceptor` with custom server flags.
pub fn with_flags(
mut config: ServerConfig, flags: ServerFlags,
) -> ssl::RustlsAcceptor<T> {
let mut protos = Vec::new();
if flags.contains(ServerFlags::HTTP2) {
protos.push("h2".to_string());
Expand All @@ -35,22 +31,7 @@ impl RustlsAcceptor {
config.set_protocols(&protos);
}

RustlsAcceptor {
config: Arc::new(config),
}
}
}

impl<Io: IoStream> AcceptorService<Io> for RustlsAcceptor {
type Accepted = TlsStream<Io, ServerSession>;
type Future = AcceptAsync<Io>;

fn scheme(&self) -> &'static str {
"https"
}

fn accept(&self, io: Io) -> Self::Future {
ServerConfigExt::accept_async(&self.config, io)
ssl::RustlsAcceptor::new(config)
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use openssl::ssl::SslAcceptorBuilder;
use rustls::ServerConfig;
#[cfg(feature = "alpn")]
use server::OpensslAcceptor;
#[cfg(feature = "rust-tls")]
use server::RustlsAcceptor;

use application::{App, HttpApplication};
use body::Binary;
Expand Down Expand Up @@ -350,7 +348,7 @@ where
let ssl = self.rust_ssl.take();
if let Some(ssl) = ssl {
let tcp = net::TcpListener::bind(addr).unwrap();
srv = srv.listen_with(tcp, RustlsAcceptor::new(ssl));
srv = srv.listen_rustls(tcp, ssl);
}
}
if !has_ssl {
Expand Down

0 comments on commit 1ff86e5

Please sign in to comment.