rustls instead of native-tls

This commit is contained in:
Martin Algesten
2018-12-19 10:30:43 +01:00
parent 131476bd1a
commit 707d194b74
5 changed files with 85 additions and 308 deletions

View File

@@ -1,12 +1,5 @@
use std::io::Error as IoError;
#[cfg(feature = "tls")]
use native_tls::Error as TlsError;
#[cfg(feature = "tls")]
use native_tls::HandshakeError;
#[cfg(feature = "tls")]
use std::net::TcpStream;
/// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic).
#[derive(Debug)]
pub enum Error {
@@ -26,12 +19,6 @@ pub enum Error {
BadHeader,
/// Some unspecified `std::io::Error`. Synthetic error `500`.
Io(IoError),
/// Some unspecified TLS error. Synthetic error `400`.
#[cfg(feature = "tls")]
Tls(TlsError),
/// Some unspecified TLS handshake error. Synthetic error `500`.
#[cfg(feature = "tls")]
TlsHandshake(HandshakeError<TcpStream>),
}
impl Error {
@@ -46,10 +33,6 @@ impl Error {
Error::BadStatus => 500,
Error::BadHeader => 500,
Error::Io(_) => 500,
#[cfg(feature = "tls")]
Error::Tls(_) => 400,
#[cfg(feature = "tls")]
Error::TlsHandshake(_) => 500,
}
}
@@ -64,10 +47,6 @@ impl Error {
Error::BadStatus => "Bad Status",
Error::BadHeader => "Bad Header",
Error::Io(_) => "Network Error",
#[cfg(feature = "tls")]
Error::Tls(_) => "TLS Error",
#[cfg(feature = "tls")]
Error::TlsHandshake(_) => "TLS Handshake Error",
}
}
@@ -82,10 +61,6 @@ impl Error {
Error::BadStatus => "Bad Status".to_string(),
Error::BadHeader => "Bad Header".to_string(),
Error::Io(ioe) => format!("Network Error: {}", ioe),
#[cfg(feature = "tls")]
Error::Tls(tls) => format!("TLS Error: {}", tls),
#[cfg(feature = "tls")]
Error::TlsHandshake(he) => format!("TLS Handshake Error: {}", he),
}
}
}
@@ -95,17 +70,3 @@ impl From<IoError> for Error {
Error::Io(err)
}
}
#[cfg(feature = "tls")]
impl From<TlsError> for Error {
fn from(err: TlsError) -> Error {
Error::Tls(err)
}
}
#[cfg(feature = "tls")]
impl From<HandshakeError<TcpStream>> for Error {
fn from(err: HandshakeError<TcpStream>) -> Error {
Error::TlsHandshake(err)
}
}

View File

@@ -1,18 +1,17 @@
use crate::agent::Unit;
use crate::error::Error;
use lazy_static::lazy_static;
use std::io::{Cursor, Read, Result as IoResult, Write};
use std::net::SocketAddr;
use std::net::TcpStream;
use std::net::ToSocketAddrs;
use std::time::Duration;
#[cfg(feature = "tls")]
use native_tls::TlsStream;
#[allow(clippy::large_enum_variant)]
pub enum Stream {
Http(TcpStream),
#[cfg(feature = "tls")]
Https(TlsStream<TcpStream>),
Https(rustls::StreamOwned<rustls::ClientSession, TcpStream>),
Cursor(Cursor<Vec<u8>>),
#[cfg(test)]
Test(Box<dyn Read + Send>, Vec<u8>),
@@ -100,14 +99,27 @@ pub fn connect_http(unit: &Unit) -> Result<Stream, Error> {
#[cfg(feature = "tls")]
pub fn connect_https(unit: &Unit) -> Result<Stream, Error> {
use native_tls::TlsConnector;
use std::sync::Arc;
lazy_static! {
static ref TLS_CONF: Arc<rustls::ClientConfig> = {
let mut config = rustls::ClientConfig::new();
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
Arc::new(config)
};
}
let hostname = unit.url.host_str().unwrap();
let port = unit.url.port().unwrap_or(443);
let socket = connect_host(unit, hostname, port)?;
let connector = TlsConnector::builder().build()?;
let stream = connector.connect(hostname, socket)?;
let sni = webpki::DNSNameRef::try_from_ascii_str(hostname).unwrap();
let sess = rustls::ClientSession::new(&*TLS_CONF, sni);
let sock = connect_host(unit, hostname, port)?;
let stream = rustls::StreamOwned::new(sess, sock);
Ok(Stream::Https(stream))
}