-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add native tls support for actix_tls::connect module (#295)
Co-authored-by: Rob Ede <robjtede@icloud.com>
- Loading branch information
1 parent
f9262db
commit bb27bac
Showing
9 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ pub mod openssl; | |
|
||
#[cfg(feature = "rustls")] | ||
pub mod rustls; | ||
|
||
#[cfg(feature = "native-tls")] | ||
pub mod native_tls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::io; | ||
|
||
use actix_rt::net::ActixStream; | ||
use actix_service::{Service, ServiceFactory}; | ||
use futures_core::future::LocalBoxFuture; | ||
use log::trace; | ||
use tokio_native_tls::{TlsConnector as TokioNativetlsConnector, TlsStream}; | ||
|
||
pub use tokio_native_tls::native_tls::TlsConnector; | ||
|
||
use crate::connect::{Address, Connection}; | ||
|
||
/// Native-tls connector factory and service | ||
pub struct NativetlsConnector { | ||
connector: TokioNativetlsConnector, | ||
} | ||
|
||
impl NativetlsConnector { | ||
pub fn new(connector: TlsConnector) -> Self { | ||
Self { | ||
connector: TokioNativetlsConnector::from(connector), | ||
} | ||
} | ||
} | ||
|
||
impl NativetlsConnector { | ||
pub fn service(connector: TlsConnector) -> Self { | ||
Self::new(connector) | ||
} | ||
} | ||
|
||
impl Clone for NativetlsConnector { | ||
fn clone(&self) -> Self { | ||
Self { | ||
connector: self.connector.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Address, U> ServiceFactory<Connection<T, U>> for NativetlsConnector | ||
where | ||
U: ActixStream + 'static, | ||
{ | ||
type Response = Connection<T, TlsStream<U>>; | ||
type Error = io::Error; | ||
type Config = (); | ||
type Service = Self; | ||
type InitError = (); | ||
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>; | ||
|
||
fn new_service(&self, _: ()) -> Self::Future { | ||
let connector = self.clone(); | ||
Box::pin(async { Ok(connector) }) | ||
} | ||
} | ||
|
||
// NativetlsConnector is both it's ServiceFactory and Service impl type. | ||
// As the factory and service share the same type and state. | ||
impl<T, U> Service<Connection<T, U>> for NativetlsConnector | ||
where | ||
T: Address, | ||
U: ActixStream + 'static, | ||
{ | ||
type Response = Connection<T, TlsStream<U>>; | ||
type Error = io::Error; | ||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
actix_service::always_ready!(); | ||
|
||
fn call(&self, stream: Connection<T, U>) -> Self::Future { | ||
let (io, stream) = stream.replace_io(()); | ||
let connector = self.connector.clone(); | ||
Box::pin(async move { | ||
trace!("SSL Handshake start for: {:?}", stream.host()); | ||
connector | ||
.connect(stream.host(), io) | ||
.await | ||
.map(|res| { | ||
trace!("SSL Handshake success: {:?}", stream.host()); | ||
stream.replace_io(res).1 | ||
}) | ||
.map_err(|e| { | ||
trace!("SSL Handshake error: {:?}", e); | ||
io::Error::new(io::ErrorKind::Other, format!("{}", e)) | ||
}) | ||
}) | ||
} | ||
} |