Fix rustls feature

This commit is contained in:
Lucio Franco
2019-09-01 13:21:54 -04:00
parent affd148525
commit 142bb8f2b9
2 changed files with 11 additions and 11 deletions
+3 -1
View File
@@ -12,6 +12,8 @@ use tokio_openssl::{connect, SslStream};
use tower_make::MakeConnection; use tower_make::MakeConnection;
use tower_service::Service; use tower_service::Service;
const ALPN_H2: &[u8] = b"\x02h2";
#[derive(Clone)] #[derive(Clone)]
pub struct TlsConnector { pub struct TlsConnector {
http: HttpConnector, http: HttpConnector,
@@ -23,7 +25,7 @@ impl TlsConnector {
pub fn new(ca: Vec<u8>, domain: String) -> Result<Self, super::Error> { pub fn new(ca: Vec<u8>, domain: String) -> Result<Self, super::Error> {
let mut config = SslConnector::builder(SslMethod::tls()).unwrap(); let mut config = SslConnector::builder(SslMethod::tls()).unwrap();
config.set_alpn_protos(b"\x06h2").unwrap(); config.set_alpn_protos(ALPN_H2).unwrap();
let ca = X509::from_pem(&ca[..]).unwrap(); let ca = X509::from_pem(&ca[..]).unwrap();
+8 -10
View File
@@ -2,12 +2,11 @@ use http::Uri;
use hyper::client::connect::HttpConnector; use hyper::client::connect::HttpConnector;
use std::{ use std::{
future::Future, future::Future,
path::Path,
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
task::{Context, Poll}, task::{Context, Poll},
}; };
use tokio::{fs, net::TcpStream}; use tokio::net::TcpStream;
use tokio_rustls::{ use tokio_rustls::{
client::TlsStream, client::TlsStream,
rustls::{ClientConfig, Session}, rustls::{ClientConfig, Session},
@@ -17,15 +16,17 @@ use tokio_rustls::{
use tower_make::MakeConnection; use tower_make::MakeConnection;
use tower_service::Service; use tower_service::Service;
const ALPN_H2: &str = "\x02h2"; const ALPN_H2: &str = "h2";
#[derive(Clone)] #[derive(Clone)]
pub struct TlsConnector { pub struct TlsConnector {
http: HttpConnector, http: HttpConnector,
config: Arc<ClientConfig>, config: Arc<ClientConfig>,
domain: String,
} }
impl TlsConnector { impl TlsConnector {
#[cfg_attr(feature = "openssl-1", allow(dead_code))]
pub fn new(ca: Vec<u8>, domain: String) -> Self { pub fn new(ca: Vec<u8>, domain: String) -> Self {
let mut buf = std::io::Cursor::new(ca); let mut buf = std::io::Cursor::new(ca);
@@ -40,6 +41,7 @@ impl TlsConnector {
Self { Self {
http, http,
config: Arc::new(config), config: Arc::new(config),
domain,
} }
} }
} }
@@ -57,8 +59,7 @@ impl Service<Uri> for TlsConnector {
} }
fn call(&mut self, uri: Uri) -> Self::Future { fn call(&mut self, uri: Uri) -> Self::Future {
let auth = uri.authority_part().unwrap(); let dns = DNSNameRef::try_from_ascii_str(self.domain.as_str())
let dns = DNSNameRef::try_from_ascii_str("foo.test.google.fr") //auth.host())
.unwrap() .unwrap()
.to_owned(); .to_owned();
let config = self.config.clone(); let config = self.config.clone();
@@ -73,10 +74,7 @@ impl Service<Uri> for TlsConnector {
RustlsConnector::from(config) RustlsConnector::from(config)
.connect(dns.as_ref(), io) .connect(dns.as_ref(), io)
.await .await
.map_err(|e| { .map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))
println!("TLS ERROR={:?}", e);
super::Error::from((super::ErrorKind::Client, e.into()))
})
.and_then(|conn| { .and_then(|conn| {
let (_, session) = conn.get_ref(); let (_, session) = conn.get_ref();
let negotiated_protocol = session.get_alpn_protocol(); let negotiated_protocol = session.get_alpn_protocol();
@@ -84,7 +82,7 @@ impl Service<Uri> for TlsConnector {
if Some(ALPN_H2.as_bytes()) == negotiated_protocol.as_ref().map(|x| &**x) { if Some(ALPN_H2.as_bytes()) == negotiated_protocol.as_ref().map(|x| &**x) {
Ok(conn) Ok(conn)
} else { } else {
Err(super::Error::from(super::ErrorKind::UnableToNegotiateH2).into()) Err(super::Error::from(super::ErrorKind::Client).into())
} }
}) })
}; };