From 4b59ae24760b361c85b04967611c5ddeae16c912 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 27 Sep 2018 22:15:38 -0700 Subject: [PATCH] fix ssl config for client connector --- src/client/connector.rs | 60 +++++++++++++++++++++++++++++------------ src/test.rs | 25 ++++++++--------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/client/connector.rs b/src/client/connector.rs index 896f98a41..6e82e3fd8 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -16,13 +16,16 @@ use http::{Error as HttpError, HttpTryFrom, Uri}; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_timer::Delay; -#[cfg(feature = "alpn")] +#[cfg(any(feature = "alpn", feature = "ssl"))] use { openssl::ssl::{Error as SslError, SslConnector, SslMethod}, tokio_openssl::SslConnectorExt, }; -#[cfg(all(feature = "tls", not(feature = "alpn")))] +#[cfg(all( + feature = "tls", + not(any(feature = "alpn", feature = "ssl", feature = "rust-tls")) +))] use { native_tls::{Error as SslError, TlsConnector as NativeTlsConnector}, tokio_tls::TlsConnector as SslConnector, @@ -30,7 +33,7 @@ use { #[cfg(all( feature = "rust-tls", - not(any(feature = "alpn", feature = "tls")) + not(any(feature = "alpn", feature = "tls", feature = "ssl")) ))] use { rustls::ClientConfig, std::io::Error as SslError, std::sync::Arc, @@ -39,11 +42,16 @@ use { #[cfg(all( feature = "rust-tls", - not(any(feature = "alpn", feature = "tls")) + not(any(feature = "alpn", feature = "tls", feature = "ssl")) ))] type SslConnector = Arc; -#[cfg(not(any(feature = "alpn", feature = "tls", feature = "rust-tls")))] +#[cfg(not(any( + feature = "alpn", + feature = "ssl", + feature = "tls", + feature = "rust-tls", +)))] type SslConnector = (); use server::IoStream; @@ -150,7 +158,12 @@ pub enum ClientConnectorError { SslIsNotSupported, /// SSL error - #[cfg(any(feature = "tls", feature = "alpn", feature = "rust-tls"))] + #[cfg(any( + feature = "tls", + feature = "alpn", + feature = "ssl", + feature = "rust-tls", + ))] #[fail(display = "{}", _0)] SslError(#[cause] SslError), @@ -247,19 +260,22 @@ impl SystemService for ClientConnector {} impl Default for ClientConnector { fn default() -> ClientConnector { let connector = { - #[cfg(all(feature = "alpn"))] + #[cfg(all(any(feature = "alpn", feature = "ssl")))] { SslConnector::builder(SslMethod::tls()).unwrap().build() } - #[cfg(all(feature = "tls", not(feature = "alpn")))] + #[cfg(all( + feature = "tls", + not(any(feature = "alpn", feature = "ssl", feature = "rust-tls")) + ))] { NativeTlsConnector::builder().build().unwrap().into() } #[cfg(all( feature = "rust-tls", - not(any(feature = "alpn", feature = "tls")) + not(any(feature = "alpn", feature = "tls", feature = "ssl")) ))] { let mut config = ClientConfig::new(); @@ -269,7 +285,12 @@ impl Default for ClientConnector { Arc::new(config) } - #[cfg(not(any(feature = "alpn", feature = "tls", feature = "rust-tls")))] + #[cfg(not(any( + feature = "alpn", + feature = "ssl", + feature = "tls", + feature = "rust-tls", + )))] { () } @@ -280,7 +301,7 @@ impl Default for ClientConnector { } impl ClientConnector { - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] /// Create `ClientConnector` actor with custom `SslConnector` instance. /// /// By default `ClientConnector` uses very a simple SSL configuration. @@ -325,7 +346,7 @@ impl ClientConnector { #[cfg(all( feature = "rust-tls", - not(any(feature = "alpn", feature = "tls")) + not(any(feature = "alpn", feature = "ssl", feature = "tls")) ))] /// Create `ClientConnector` actor with custom `SslConnector` instance. /// @@ -376,7 +397,7 @@ impl ClientConnector { #[cfg(all( feature = "tls", - not(any(feature = "alpn", feature = "rust-tls")) + not(any(feature = "ssl", feature = "alpn", feature = "rust-tls")) ))] /// Create `ClientConnector` actor with custom `SslConnector` instance. /// @@ -714,7 +735,7 @@ impl ClientConnector { act.release_key(&key2); () }).and_then(move |res, act, _| { - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] match res { Err(err) => { let _ = waiter.tx.send(Err(err.into())); @@ -756,7 +777,7 @@ impl ClientConnector { } } - #[cfg(all(feature = "tls", not(feature = "alpn")))] + #[cfg(all(feature = "tls", not(any(feature = "alpn", feature = "ssl"))))] match res { Err(err) => { let _ = waiter.tx.send(Err(err.into())); @@ -800,7 +821,7 @@ impl ClientConnector { #[cfg(all( feature = "rust-tls", - not(any(feature = "alpn", feature = "tls")) + not(any(feature = "alpn", feature = "ssl", feature = "tls")) ))] match res { Err(err) => { @@ -844,7 +865,12 @@ impl ClientConnector { } } - #[cfg(not(any(feature = "alpn", feature = "tls", feature = "rust-tls")))] + #[cfg(not(any( + feature = "alpn", + feature = "ssl", + feature = "tls", + feature = "rust-tls" + )))] match res { Err(err) => { let _ = waiter.tx.send(Err(err.into())); diff --git a/src/test.rs b/src/test.rs index 83b0b83b7..d0cfb255a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -13,12 +13,10 @@ use http::{HeaderMap, HttpTryFrom, Method, Uri, Version}; use net2::TcpBuilder; use tokio::runtime::current_thread::Runtime; -#[cfg(feature = "alpn")] +#[cfg(any(feature = "alpn", feature = "ssl"))] use openssl::ssl::SslAcceptorBuilder; #[cfg(feature = "rust-tls")] use rustls::ServerConfig; -#[cfg(feature = "alpn")] -use server::OpensslAcceptor; use application::{App, HttpApplication}; use body::Binary; @@ -136,7 +134,7 @@ impl TestServer { } fn get_conn() -> Addr { - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] { use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode}; @@ -144,7 +142,10 @@ impl TestServer { builder.set_verify(SslVerifyMode::NONE); ClientConnector::with_connector(builder.build()).start() } - #[cfg(all(feature = "rust-tls", not(feature = "alpn")))] + #[cfg(all( + feature = "rust-tls", + not(any(feature = "alpn", feature = "ssl")) + ))] { use rustls::ClientConfig; use std::fs::File; @@ -154,7 +155,7 @@ impl TestServer { config.root_store.add_pem_file(pem_file).unwrap(); ClientConnector::with_connector(config).start() } - #[cfg(not(any(feature = "alpn", feature = "rust-tls")))] + #[cfg(not(any(feature = "alpn", feature = "ssl", feature = "rust-tls")))] { ClientConnector::default().start() } @@ -263,7 +264,7 @@ where F: Fn() -> S + Send + Clone + 'static, { state: F, - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] ssl: Option, #[cfg(feature = "rust-tls")] rust_ssl: Option, @@ -277,14 +278,14 @@ where pub fn new(state: F) -> TestServerBuilder { TestServerBuilder { state, - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] ssl: None, #[cfg(feature = "rust-tls")] rust_ssl: None, } } - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] /// Create ssl server pub fn ssl(mut self, ssl: SslAcceptorBuilder) -> Self { self.ssl = Some(ssl); @@ -308,7 +309,7 @@ where let mut has_ssl = false; - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] { has_ssl = has_ssl || self.ssl.is_some(); } @@ -335,12 +336,12 @@ where tx.send((System::current(), addr, TestServer::get_conn())) .unwrap(); - #[cfg(feature = "alpn")] + #[cfg(any(feature = "alpn", feature = "ssl"))] { let ssl = self.ssl.take(); if let Some(ssl) = ssl { let tcp = net::TcpListener::bind(addr).unwrap(); - srv = srv.listen_with(tcp, OpensslAcceptor::new(ssl).unwrap()); + srv = srv.listen_ssl(tcp, ssl).unwrap(); } } #[cfg(feature = "rust-tls")]