mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
add native-tls support
This commit is contained in:
parent
0bca21ec6d
commit
ecfda64f6d
@ -32,12 +32,12 @@ script:
|
|||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" != "stable" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" != "stable" ]]; then
|
||||||
cargo clean
|
cargo clean
|
||||||
cargo test --features="ssl" -- --nocapture
|
cargo test --features="ssl,tls" -- --nocapture
|
||||||
fi
|
fi
|
||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
|
||||||
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install -f cargo-tarpaulin
|
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install -f cargo-tarpaulin
|
||||||
cargo tarpaulin --features="ssl" --out Xml --no-count
|
cargo tarpaulin --features="ssl,tls" --out Xml --no-count
|
||||||
bash <(curl -s https://codecov.io/bash)
|
bash <(curl -s https://codecov.io/bash)
|
||||||
echo "Uploaded code coverage"
|
echo "Uploaded code coverage"
|
||||||
fi
|
fi
|
||||||
|
@ -32,7 +32,7 @@ path = "src/lib.rs"
|
|||||||
default = ["session", "brotli", "flate2-c"]
|
default = ["session", "brotli", "flate2-c"]
|
||||||
|
|
||||||
# tls
|
# tls
|
||||||
tls = ["native-tls", "tokio-tls"]
|
tls = ["native-tls", "tokio-tls", "actix-net/tls"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
ssl = ["openssl", "tokio-openssl", "actix-net/ssl"]
|
ssl = ["openssl", "tokio-openssl", "actix-net/ssl"]
|
||||||
@ -41,7 +41,7 @@ ssl = ["openssl", "tokio-openssl", "actix-net/ssl"]
|
|||||||
alpn = ["openssl", "tokio-openssl", "actix-net/ssl"]
|
alpn = ["openssl", "tokio-openssl", "actix-net/ssl"]
|
||||||
|
|
||||||
# rustls
|
# rustls
|
||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots", "actix-net/rust-tls"]
|
||||||
|
|
||||||
# unix sockets
|
# unix sockets
|
||||||
uds = ["tokio-uds"]
|
uds = ["tokio-uds"]
|
||||||
|
@ -9,8 +9,8 @@ use net2::TcpBuilder;
|
|||||||
use num_cpus;
|
use num_cpus;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
//#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
//use native_tls::TlsAcceptor;
|
use native_tls::TlsAcceptor;
|
||||||
|
|
||||||
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
||||||
use openssl::ssl::SslAcceptorBuilder;
|
use openssl::ssl::SslAcceptorBuilder;
|
||||||
@ -258,16 +258,27 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
// /// Use listener for accepting incoming tls connection requests
|
/// Use listener for accepting incoming tls connection requests
|
||||||
// ///
|
///
|
||||||
// /// HttpServer does not change any configuration for TcpListener,
|
/// HttpServer does not change any configuration for TcpListener,
|
||||||
// /// it needs to be configured before passing it to listen() method.
|
/// it needs to be configured before passing it to listen() method.
|
||||||
// pub fn listen_tls(self, lst: net::TcpListener, acceptor: TlsAcceptor) -> Self {
|
pub fn listen_tls(mut self, lst: net::TcpListener, acceptor: TlsAcceptor) -> Self {
|
||||||
// use super::NativeTlsAcceptor;
|
use actix_net::service::NewServiceExt;
|
||||||
//
|
|
||||||
// self.listen_with(lst, NativeTlsAcceptor::new(acceptor))
|
let addr = lst.local_addr().unwrap();
|
||||||
// }
|
self.sockets.push(Socket {
|
||||||
|
lst,
|
||||||
|
addr,
|
||||||
|
scheme: "https",
|
||||||
|
handler: Box::new(HttpServiceBuilder::new(
|
||||||
|
self.factory.clone(),
|
||||||
|
move || ssl::NativeTlsAcceptor::new(acceptor.clone()).map_err(|_| ()),
|
||||||
|
DefaultPipelineFactory::new(),
|
||||||
|
)),
|
||||||
|
});
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
||||||
/// Use listener for accepting incoming tls connection requests
|
/// Use listener for accepting incoming tls connection requests
|
||||||
|
@ -3,10 +3,8 @@ mod openssl;
|
|||||||
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
#[cfg(any(feature = "alpn", feature = "ssl"))]
|
||||||
pub use self::openssl::*;
|
pub use self::openssl::*;
|
||||||
|
|
||||||
//#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
//mod nativetls;
|
mod nativetls;
|
||||||
//#[cfg(feature = "tls")]
|
|
||||||
//pub use self::nativetls::{NativeTlsAcceptor, TlsStream};
|
|
||||||
|
|
||||||
//#[cfg(feature = "rust-tls")]
|
//#[cfg(feature = "rust-tls")]
|
||||||
//mod rustls;
|
//mod rustls;
|
||||||
|
@ -1,61 +1,9 @@
|
|||||||
use std::net::Shutdown;
|
use std::net::Shutdown;
|
||||||
use std::{io, time};
|
use std::{io, time};
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
use actix_net::ssl::TlsStream;
|
||||||
use native_tls::{self, HandshakeError, TlsAcceptor};
|
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
|
||||||
|
|
||||||
use server::{AcceptorService, IoStream};
|
use server::IoStream;
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
/// Support `SSL` connections via native-tls package
|
|
||||||
///
|
|
||||||
/// `tls` feature enables `NativeTlsAcceptor` type
|
|
||||||
pub struct NativeTlsAcceptor {
|
|
||||||
acceptor: TlsAcceptor,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A wrapper around an underlying raw stream which implements the TLS or SSL
|
|
||||||
/// protocol.
|
|
||||||
///
|
|
||||||
/// A `TlsStream<S>` represents a handshake that has been completed successfully
|
|
||||||
/// and both the server and the client are ready for receiving and sending
|
|
||||||
/// data. Bytes read from a `TlsStream` are decrypted from `S` and bytes written
|
|
||||||
/// to a `TlsStream` are encrypted when passing through to `S`.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct TlsStream<S> {
|
|
||||||
inner: native_tls::TlsStream<S>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Future returned from `NativeTlsAcceptor::accept` which will resolve
|
|
||||||
/// once the accept handshake has finished.
|
|
||||||
pub struct Accept<S> {
|
|
||||||
inner: Option<Result<native_tls::TlsStream<S>, HandshakeError<S>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NativeTlsAcceptor {
|
|
||||||
/// Create `NativeTlsAcceptor` instance
|
|
||||||
pub fn new(acceptor: TlsAcceptor) -> Self {
|
|
||||||
NativeTlsAcceptor {
|
|
||||||
acceptor: acceptor.into(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io: IoStream> AcceptorService<Io> for NativeTlsAcceptor {
|
|
||||||
type Accepted = TlsStream<Io>;
|
|
||||||
type Future = Accept<Io>;
|
|
||||||
|
|
||||||
fn scheme(&self) -> &'static str {
|
|
||||||
"https"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn accept(&self, io: Io) -> Self::Future {
|
|
||||||
Accept {
|
|
||||||
inner: Some(self.acceptor.accept(io)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Io: IoStream> IoStream for TlsStream<Io> {
|
impl<Io: IoStream> IoStream for TlsStream<Io> {
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -74,70 +22,3 @@ impl<Io: IoStream> IoStream for TlsStream<Io> {
|
|||||||
self.get_mut().get_mut().set_linger(dur)
|
self.get_mut().get_mut().set_linger(dur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io: IoStream> Future for Accept<Io> {
|
|
||||||
type Item = TlsStream<Io>;
|
|
||||||
type Error = io::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
match self.inner.take().expect("cannot poll MidHandshake twice") {
|
|
||||||
Ok(stream) => Ok(TlsStream { inner: stream }.into()),
|
|
||||||
Err(HandshakeError::Failure(e)) => {
|
|
||||||
Err(io::Error::new(io::ErrorKind::Other, e))
|
|
||||||
}
|
|
||||||
Err(HandshakeError::WouldBlock(s)) => match s.handshake() {
|
|
||||||
Ok(stream) => Ok(TlsStream { inner: stream }.into()),
|
|
||||||
Err(HandshakeError::Failure(e)) => {
|
|
||||||
Err(io::Error::new(io::ErrorKind::Other, e))
|
|
||||||
}
|
|
||||||
Err(HandshakeError::WouldBlock(s)) => {
|
|
||||||
self.inner = Some(Err(HandshakeError::WouldBlock(s)));
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> TlsStream<S> {
|
|
||||||
/// Get access to the internal `native_tls::TlsStream` stream which also
|
|
||||||
/// transitively allows access to `S`.
|
|
||||||
pub fn get_ref(&self) -> &native_tls::TlsStream<S> {
|
|
||||||
&self.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get mutable access to the internal `native_tls::TlsStream` stream which
|
|
||||||
/// also transitively allows mutable access to `S`.
|
|
||||||
pub fn get_mut(&mut self) -> &mut native_tls::TlsStream<S> {
|
|
||||||
&mut self.inner
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: io::Read + io::Write> io::Read for TlsStream<S> {
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
self.inner.read(buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: io::Read + io::Write> io::Write for TlsStream<S> {
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
||||||
self.inner.write(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
|
||||||
self.inner.flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: AsyncRead + AsyncWrite> AsyncRead for TlsStream<S> {}
|
|
||||||
|
|
||||||
impl<S: AsyncRead + AsyncWrite> AsyncWrite for TlsStream<S> {
|
|
||||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
||||||
match self.inner.shutdown() {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => (),
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
|
||||||
self.inner.get_mut().shutdown()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user