hyperium / hyper-tls

Apache License 2.0
189 stars 95 forks source link

Setting self signed certificate fails. #94

Closed sungjun0405 closed 1 year ago

sungjun0405 commented 1 year ago

Hello~ I would like to set the self signed certificate to client and connect. If you look at the impl, you can set the TlsConnector created through from. However, when I try the direct implementation, it fails and throws an error because the trait bounds don't match. Can't it be set using the hyper_tls::native_tls::TlsConnector build?

use hyper::{client::HttpConnector, Client};

use hyper_tls::{
    native_tls::{Certificate, TlsConnector},
    HttpsConnector,
};

fn main() {
    let mut tls_connector_builder = TlsConnector::builder();
    const SELF_SIGNED_CERT: &[u8] = include_bytes!(
        "./data/self_signed_certs/public.crt"
    );
    let certificate = Certificate::from_pem(SELF_SIGNED_CERT).unwrap();
    tls_connector_builder.add_root_certificate(certificate);
    tls_connector_builder.danger_accept_invalid_hostnames(true);
    let mut tls_connector = tls_connector_builder.build().unwrap();

    let httpss = HttpsConnector::new();
    let mut http = HttpConnector::new();
    http.enforce_http(false);

    let https = HttpsConnector::<HttpConnector>::from((http, tls_connector));
    let client = Client::builder().build::<_, hyper::Body>(https);
}

스크린샷 2023-02-07 오후 2 55 41

sungjun0405 commented 1 year ago

solved my question. I just learned and had a lack of understanding about Newtype .

    let https = HttpsConnector::<HttpConnector>::from((
        http,
        tokio_native_tls::TlsConnector::from(tls_connector),
    ));