Open hermantolim opened 5 years ago
There isnt'! Would you be interested in providing one?
Hey, I'm currently working on this. The only thing blocking me is going from having a use mio::net::TcpStream
and rustls::ClientConnection
to creating an HttpsConnector
.
For anyone who is interested, I think using tokio-rustls in conjunction with hyper is the way to go.
Here's a quick program to get you started:
use std::{
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
sync::Arc,
time::Instant,
};
use http_body_util::{BodyExt, Empty};
use hyper::{body::Bytes, Request};
use tokio::{
io::{self, AsyncWriteExt},
net::TcpStream,
};
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName};
use tokio_rustls::TlsConnector;
async fn async_request(ip: Ipv4Addr, domain: &str, url: &str) {
let mut root_cert_store = RootCertStore::empty();
root_cert_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let config = ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_cert_store)
.with_no_client_auth();
let connector = TlsConnector::from(Arc::new(config));
let dnsname = ServerName::try_from(domain).unwrap();
let addr = SocketAddr::V4(SocketAddrV4::new(ip, 443));
let stream = TcpStream::connect(&addr).await.expect("Could not connect");
let mut stream = connector
.connect(dnsname, stream)
.await
.expect("Could not connect connector?");
let (mut request_sender, connection) = hyper::client::conn::http1::handshake(stream)
.await
.expect("Could not perform http1 handshake");
// spawn a task to poll the connection and drive the HTTP state
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("Error in connection: {}", e);
}
});
let parsed_url = url.parse::<hyper::Uri>().expect("Could not parse url");
let request = Request::builder()
// We need to manually add the host header because SendRequest does not
.header("Host", "example.com")
.method("GET")
.uri(parsed_url)
.body(Empty::<Bytes>::new())
.expect("Could not build HTTP request");
let mut response = request_sender
.send_request(request)
.await
.expect("Could not send request");
while let Some(next) = response.frame().await {
let frame = next.expect("Could not unwrap next frame");
if let Some(chunk) = frame.data_ref() {
io::stdout()
.write_all(&chunk)
.await
.expect("Could not write to stdout");
}
}
}
All you need to do is call async_request
like so:
async_request(
Ipv4Addr::new(93, 184, 216, 34),
"example.com.",
"https://example.com/",
)
.await;
I don't see how that example is using trust-dns-resolver for DNS resolution.
I didn't actually integrate it with the Https resolver. This thread just comes up on google when you look for ways to use rustls without performing resolutions, so I provided an example I landed on. This example takes in an ip address so that no resolutions are necessary. I haven't actually checked if it does not perform resolutions or not.
To complete the example you could add the trustDNS resolver to provide the IP.
Not a full example, just leaving it in case people come upon the same path as me.
is ther an example to use the HttpsConnector with trust-dns-resolver?