use std::net::SocketAddr;
use futures::Future;
use rustls::{Certificate, ClientConfig, ClientSession};
use tokio_rustls::TlsStream as TokioTlsStream;
use tokio_tcp::TcpStream as TokioTcpStream;
use trust_dns_proto::error::ProtoError;
use trust_dns_proto::tcp::TcpClientStream;
use trust_dns_proto::xfer::BufDnsStreamHandle;
use TlsStreamBuilder;
pub type TlsClientStream = TcpClientStream<TokioTlsStream<TokioTcpStream, ClientSession>>;
#[derive(Clone)]
pub struct TlsClientStreamBuilder(TlsStreamBuilder);
impl TlsClientStreamBuilder {
pub fn new() -> Self {
TlsClientStreamBuilder(TlsStreamBuilder::new())
}
pub fn with_client_config(client_config: ClientConfig) -> Self {
TlsClientStreamBuilder(TlsStreamBuilder::with_client_config(client_config))
}
pub fn add_ca(&mut self, ca: Certificate) {
self.0.add_ca(ca);
}
#[cfg(feature = "mtls")]
pub fn identity(&mut self, pkcs12: Pkcs12) {
self.0.identity(pkcs12);
}
pub fn build(
self,
name_server: SocketAddr,
dns_name: String,
) -> (
Box<Future<Item = TlsClientStream, Error = ProtoError> + Send>,
BufDnsStreamHandle,
) {
let (stream_future, sender) = self.0.build(name_server, dns_name);
let new_future = Box::new(
stream_future
.map(move |tls_stream| TcpClientStream::from_stream(tls_stream))
.map_err(ProtoError::from),
);
let sender = BufDnsStreamHandle::new(name_server, sender);
(new_future, sender)
}
}