GithubHelp home page GithubHelp logo

Comments (5)

cpu avatar cpu commented on May 30, 2024

There isnt'! Would you be interested in providing one?

from hyper-rustls.

CalderWhite avatar CalderWhite commented on May 30, 2024

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.

from hyper-rustls.

CalderWhite avatar CalderWhite commented on May 30, 2024

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;

from hyper-rustls.

djc avatar djc commented on May 30, 2024

I don't see how that example is using trust-dns-resolver for DNS resolution.

from hyper-rustls.

CalderWhite avatar CalderWhite commented on May 30, 2024

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.

from hyper-rustls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.