-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fac9883
commit c46627e
Showing
6 changed files
with
136 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use domain::base::Dname; | ||
use domain::base::Rtype::Aaaa; | ||
use domain::base::{MessageBuilder, StaticCompressor, StreamTarget}; | ||
use domain::net::client::multi_stream; | ||
use domain::net::client::query::QueryMessage2; | ||
use domain::net::client::redundant; | ||
use domain::net::client::tcp_factory::TcpConnFactory; | ||
use domain::net::client::tls_factory::TlsConnFactory; | ||
use domain::net::client::udp_tcp; | ||
use std::net::{IpAddr, SocketAddr}; | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Create DNS request message | ||
// Create a message builder wrapping a compressor wrapping a stream | ||
// target. | ||
let mut msg = | ||
MessageBuilder::from_target(StaticCompressor::new(StreamTarget::new_vec())).unwrap(); | ||
msg.header_mut().set_rd(true); | ||
let mut msg = msg.question(); | ||
msg.push((Dname::<Vec<u8>>::vec_from_str("example.com").unwrap(), Aaaa)) | ||
.unwrap(); | ||
let mut msg = msg.as_builder_mut().clone(); | ||
|
||
// Destination for UDP and TCP | ||
let server_addr = SocketAddr::new(IpAddr::from_str("::1").unwrap(), 53); | ||
|
||
// Create a new UDP+TCP transport connection. Pass the destination address | ||
// and port as parameter. | ||
let udptcp_conn = udp_tcp::Connection::new(server_addr).unwrap(); | ||
|
||
// Create a clone for the run function. Start the run function on a | ||
// separate task. | ||
let conn_run = udptcp_conn.clone(); | ||
tokio::spawn(async move { | ||
conn_run.run().await; | ||
}); | ||
|
||
// Send a query message. | ||
let mut query = udptcp_conn.query(&mut msg).await.unwrap(); | ||
|
||
// Get the reply | ||
let reply = query.get_result().await; | ||
println!("UDP+TCP reply: {:?}", reply); | ||
|
||
// Create a factory of TCP connections. Pass the destination address and | ||
// port as parameter. | ||
let tcp_factory = TcpConnFactory::new(server_addr); | ||
|
||
// A muli_stream transport connection sets up new TCP connections when | ||
// needed. | ||
let tcp_conn = multi_stream::Connection::<Vec<u8>>::new().unwrap(); | ||
|
||
// Start the run function as a separate task. The run function receives | ||
// the factory as a parameter. | ||
let conn_run = tcp_conn.clone(); | ||
tokio::spawn(async move { | ||
conn_run.run(tcp_factory).await; | ||
}); | ||
|
||
// Send a query message. | ||
let mut query = tcp_conn.query(&mut msg).await.unwrap(); | ||
|
||
// Get the reply | ||
let reply = query.get_result().await; | ||
println!("TCP reply: {:?}", reply); | ||
|
||
// Some TLS boiler plate for the root certificates. | ||
let mut root_store = RootCertStore::empty(); | ||
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| { | ||
OwnedTrustAnchor::from_subject_spki_name_constraints( | ||
ta.subject, | ||
ta.spki, | ||
ta.name_constraints, | ||
) | ||
})); | ||
|
||
// TLS config | ||
let client_config = Arc::new( | ||
ClientConfig::builder() | ||
.with_safe_defaults() | ||
.with_root_certificates(root_store) | ||
.with_no_client_auth(), | ||
); | ||
|
||
// Currently the only support TLS connections are the ones that have a | ||
// valid certificate. Use a well known public resolver. | ||
let server_addr = SocketAddr::new(IpAddr::from_str("8.8.8.8").unwrap(), 853); | ||
|
||
// Create a new TLS connection factory. We pass the TLS config, the name of | ||
// the remote server and the destination address and port. | ||
let tls_factory = TlsConnFactory::new(client_config, "dns.google", server_addr); | ||
|
||
// Again create a multi_stream transport connection. | ||
let tls_conn = multi_stream::Connection::new().unwrap(); | ||
|
||
// Can start the run function. | ||
let conn_run = tls_conn.clone(); | ||
tokio::spawn(async move { | ||
conn_run.run(tls_factory).await; | ||
}); | ||
|
||
let mut query = tls_conn.query(&mut msg).await.unwrap(); | ||
let reply = query.get_result().await; | ||
println!("TLS reply: {:?}", reply); | ||
|
||
// Create a transport connection for redundant connections. | ||
let redun = redundant::Connection::new().unwrap(); | ||
|
||
// Start the run function on a separate task. | ||
let redun_run = redun.clone(); | ||
tokio::spawn(async move { | ||
redun_run.run().await; | ||
}); | ||
|
||
// Add the previously created transports. | ||
redun.add(Box::new(udptcp_conn)).await; | ||
redun.add(Box::new(tcp_conn)).await; | ||
redun.add(Box::new(tls_conn)).await; | ||
|
||
// Start a few queries. | ||
for _i in 1..10 { | ||
let mut query = redun.query(&mut msg).await.unwrap(); | ||
let reply = query.get_result().await; | ||
println!("redundant connection reply: {:?}", reply); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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