try openssl

This commit is contained in:
Lucio Franco
2019-08-31 17:49:42 -04:00
parent 4ea3fdfd1e
commit d43b2a3c63
5 changed files with 84 additions and 5 deletions
+2
View File
@@ -27,6 +27,8 @@ hyper = { git = "https://github.com/hyperium/hyper", optional = true}
tokio = { version = "=0.2.0-alpha.4", default-features = false, features = ["tcp"], optional = true }
tower-make = "=0.1.0-alpha.2"
tokio-rustls = { path = "../../tokio-rustls", optional = true }
tokio-openssl = "=0.4.0-alpha.4"
openssl = "*"
tower-reconnect = { path = "../../tower/tower-reconnect", optional = true }
tower-buffer = { path = "../../tower/tower-buffer", optional = true }
+2 -1
View File
@@ -49,7 +49,8 @@ impl Client {
pub async fn connect_with_tls<P: AsRef<Path>>(addr: Uri, ca: P) -> Result<Self, super::Error> {
let settings = Builder::new().http2_only(true).clone();
let tls_connector = TlsConnector::load(ca).await?;
// let tls_connector = TlsConnector::load(ca).await?;
let tls_connector = super::openssl::TlsConnector::load(ca).await?;
let maker = Connect::new(tls_connector, settings);
let svc = tower_reconnect::Reconnect::new(maker, addr.clone());
+1
View File
@@ -1,4 +1,5 @@
mod client;
mod openssl;
mod tls;
pub use self::client::Client;
+69
View File
@@ -0,0 +1,69 @@
use http::Uri;
use hyper::client::connect::HttpConnector;
use openssl::ssl::{ConnectConfiguration, SslConnector, SslMethod};
use std::{
future::Future,
path::Path,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::{fs, net::TcpStream};
use tokio_openssl::{connect, SslStream};
use tower_make::MakeConnection;
use tower_service::Service;
const ALPN_H2: &str = "h2";
#[derive(Clone)]
pub struct TlsConnector {
http: HttpConnector,
config: SslConnector,
}
impl TlsConnector {
pub async fn load<P: AsRef<Path>>(ca: P) -> Result<Self, super::Error> {
let mut config = SslConnector::builder(SslMethod::tls()).unwrap();
config.set_alpn_protos(ALPN_H2.as_bytes()).unwrap();
config.set_ca_file(ca).unwrap();
let config = config.build();
let mut http = HttpConnector::new();
http.enforce_http(false);
Ok(Self {
http,
config,
})
}
}
impl Service<Uri> for TlsConnector {
type Response = SslStream<TcpStream>;
type Error = super::Error;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
MakeConnection::poll_ready(&mut self.http, cx)
.map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))
}
fn call(&mut self, uri: Uri) -> Self::Future {
let config = self.config.configure().unwrap();
let tcp = self.http.make_connection(uri.clone());
let fut = async move {
let io = tcp.await.unwrap();
let domain = "foo.test.google.fr";
let tls = connect(config, &domain, io).await.unwrap();
Ok(tls)
};
Box::pin(fut)
}
}
+10 -4
View File
@@ -9,7 +9,10 @@ use std::{
};
use tokio::{fs, net::TcpStream};
use tokio_rustls::{
client::TlsStream, rustls::{ClientConfig, Session}, webpki::DNSNameRef, TlsConnector as RustlsConnector,
client::TlsStream,
rustls::{ClientConfig, Session},
webpki::DNSNameRef,
TlsConnector as RustlsConnector,
};
use tower_make::MakeConnection;
use tower_service::Service;
@@ -24,7 +27,9 @@ pub struct TlsConnector {
impl TlsConnector {
pub async fn load<P: AsRef<Path>>(ca: P) -> Result<Self, super::Error> {
let pem = fs::read(ca).await.map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))?;
let pem = fs::read(ca)
.await
.map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))?;
Ok(TlsConnector::new(pem))
}
@@ -54,12 +59,13 @@ impl Service<Uri> for TlsConnector {
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
MakeConnection::poll_ready(&mut self.http, cx).map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))
MakeConnection::poll_ready(&mut self.http, cx)
.map_err(|e| super::Error::from((super::ErrorKind::Client, e.into())))
}
fn call(&mut self, uri: Uri) -> Self::Future {
let auth = uri.authority_part().unwrap();
let dns = DNSNameRef::try_from_ascii_str("foo.test.google.fr")//auth.host())
let dns = DNSNameRef::try_from_ascii_str("foo.test.google.fr") //auth.host())
.unwrap()
.to_owned();
let config = self.config.clone();