/
tls_client_connection.rs
70 lines (60 loc) · 2.23 KB
/
tls_client_connection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//! TLS based DNS client connection for Client impls
//! TODO: This modules was moved from trust-dns-rustls, it really doesn't need to exist if tests are refactored...
use std::pin::Pin;
use std::sync::Arc;
use std::{marker::PhantomData, net::SocketAddr};
use futures::Future;
use trust_dns_client::client::ClientConnection;
use trust_dns_client::rr::dnssec::Signer;
use trust_dns_proto::error::ProtoError;
use trust_dns_proto::tcp::Connect;
use trust_dns_proto::xfer::{DnsMultiplexer, DnsMultiplexerConnect};
use rustls::ClientConfig;
use trust_dns_rustls::{tls_client_connect, TlsClientStream};
/// Tls client connection
///
/// Use with `trust_dns_client::client::Client` impls
pub struct TlsClientConnection<T> {
name_server: SocketAddr,
dns_name: String,
client_config: Arc<ClientConfig>,
marker: PhantomData<T>,
}
#[cfg(all(feature = "dns-over-openssl", not(feature = "dns-over-rustls")))]
impl<T> TlsClientConnection<T> {
pub fn new(
name_server: SocketAddr,
dns_name: String,
client_config: Arc<ClientConfig>,
) -> Self {
TlsClientConnection {
name_server,
dns_name,
client_config,
marker: PhantomData,
}
}
}
#[allow(clippy::type_complexity)]
impl<T: Connect> ClientConnection for TlsClientConnection<T> {
type Sender = DnsMultiplexer<TlsClientStream<T>, Signer>;
type SenderFuture = DnsMultiplexerConnect<
Pin<Box<dyn Future<Output = Result<TlsClientStream<T>, ProtoError>> + Send>>,
TlsClientStream<T>,
Signer,
>;
fn new_stream(&self, signer: Option<Arc<Signer>>) -> Self::SenderFuture {
let (tls_client_stream, handle) = tls_client_connect(
self.name_server,
self.dns_name.clone(),
self.client_config.clone(),
);
DnsMultiplexer::new(Box::pin(tls_client_stream), Box::new(handle), signer)
}
}