-
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.
* feat: add tls connector for client * feat: add example for tls * ci: ignore fixtures dir for license check * fix: remove webpki-roots dependency * style: code fmt * fix: fix cargo check error
- Loading branch information
Showing
16 changed files
with
616 additions
and
13 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
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
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
130 changes: 130 additions & 0 deletions
130
dubbo/src/triple/transport/connector/https_connector.rs
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 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
use std::{ | ||
net::{Ipv4Addr, SocketAddr, SocketAddrV4}, | ||
str::FromStr, | ||
sync::Arc, | ||
}; | ||
|
||
use dubbo_logger::tracing; | ||
use http::Uri; | ||
use hyper::client::connect::dns::Name; | ||
use rustls_native_certs::load_native_certs; | ||
use tokio::net::TcpStream; | ||
use tokio_rustls::{ | ||
client::TlsStream, | ||
rustls::{self}, | ||
TlsConnector as TlsConnectorTokio, | ||
}; | ||
use tower_service::Service; | ||
|
||
use crate::triple::transport::resolver::{dns::DnsResolver, Resolve}; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct HttpsConnector<R = DnsResolver> { | ||
resolver: R, | ||
} | ||
|
||
impl HttpsConnector { | ||
pub fn new() -> Self { | ||
Self { | ||
resolver: DnsResolver::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<R> HttpsConnector<R> { | ||
pub fn new_with_resolver(resolver: R) -> HttpsConnector<R> { | ||
Self { resolver } | ||
} | ||
} | ||
|
||
impl<R> Service<Uri> for HttpsConnector<R> | ||
where | ||
R: Resolve + Clone + Send + Sync + 'static, | ||
R::Future: Send, | ||
{ | ||
type Response = TlsStream<TcpStream>; | ||
|
||
type Error = crate::Error; | ||
|
||
type Future = crate::BoxFuture<Self::Response, Self::Error>; | ||
|
||
fn poll_ready( | ||
&mut self, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), Self::Error>> { | ||
self.resolver.poll_ready(cx).map_err(|err| err.into()) | ||
} | ||
|
||
fn call(&mut self, uri: Uri) -> Self::Future { | ||
let mut inner = self.clone(); | ||
|
||
Box::pin(async move { inner.call_async(uri).await }) | ||
} | ||
} | ||
|
||
impl<R> HttpsConnector<R> | ||
where | ||
R: Resolve + Send + Sync + 'static, | ||
{ | ||
async fn call_async(&mut self, uri: Uri) -> Result<TlsStream<TcpStream>, crate::Error> { | ||
let host = uri.host().unwrap(); | ||
let port = uri.port_u16().unwrap(); | ||
|
||
let addr = if let Ok(addr) = host.parse::<Ipv4Addr>() { | ||
tracing::info!("host is ip address: {:?}", host); | ||
SocketAddr::V4(SocketAddrV4::new(addr, port)) | ||
} else { | ||
tracing::info!("host is dns: {:?}", host); | ||
let addrs = self | ||
.resolver | ||
.resolve(Name::from_str(host).unwrap()) | ||
.await | ||
.map_err(|err| err.into())?; | ||
let addrs: Vec<SocketAddr> = addrs | ||
.map(|mut addr| { | ||
addr.set_port(port); | ||
addr | ||
}) | ||
.collect(); | ||
addrs[0] | ||
}; | ||
|
||
let mut root_store = rustls::RootCertStore::empty(); | ||
|
||
for cert in load_native_certs()? { | ||
root_store.add(&rustls::Certificate(cert.0))?; | ||
} | ||
|
||
let config = rustls::ClientConfig::builder() | ||
.with_safe_defaults() | ||
.with_root_certificates(root_store) | ||
.with_no_client_auth(); | ||
|
||
let connector = TlsConnectorTokio::from(Arc::new(config)); | ||
|
||
let stream = TcpStream::connect(&addr).await?; | ||
let domain = rustls::ServerName::try_from(host).map_err(|err| { | ||
crate::status::Status::new(crate::status::Code::Internal, err.to_string()) | ||
})?; | ||
let stream = connector.connect(domain, stream).await?; | ||
|
||
Ok(stream) | ||
} | ||
} |
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
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
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,21 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDiTCCAnGgAwIBAgIUGJWxrMGe9qRZzAfd5w4XIT3lkcEwDQYJKoZIhvcNAQEL | ||
BQAwVDEVMBMGA1UEAwwMVGVzdCBSb290IENBMQswCQYDVQQGEwJVUzENMAsGA1UE | ||
CAwEVGVzdDENMAsGA1UEBwwEVGVzdDEQMA4GA1UECgwHT3BlbmRhbDAeFw0yMzA4 | ||
MTQxMTEzMzRaFw0yNDA4MTMxMTEzMzRaMFQxFTATBgNVBAMMDFRlc3QgUm9vdCBD | ||
QTELMAkGA1UEBhMCVVMxDTALBgNVBAgMBFRlc3QxDTALBgNVBAcMBFRlc3QxEDAO | ||
BgNVBAoMB09wZW5kYWwwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCt | ||
UF6mcDgSyH5t78XnJusvQxsUfv2XydHtvLcIpwkCkIuIj7nF2WH064Gv12x+y42W | ||
mb+5z6JTgHRMRqcyQM8q4PQFrKvxPX8R2Limd7VLBJzYjR7Ma7JIrDohLnfywxUP | ||
19P5SzaGiro+ZK3t3xCnmtHcYoM+An0mQdKyVV7ytzAfg1PqkfDme19I28fH8cOP | ||
tF+RU8/LEHnte519O1bawx7xNdPsyykMrFij02o1VUeum2K9Wya8xHDixokveYDW | ||
swg5G4Tsy1QfgqFgxAXahIroPIwQvZOGkWVsmPXRXHtHNFG91ntJivv2HBFniUTq | ||
A0UbVdj09T+h+JLc19G9AgMBAAGjUzBRMB0GA1UdDgQWBBQ2672x8uh6Lud0EkjO | ||
wt2aEioeKjAfBgNVHSMEGDAWgBQ2672x8uh6Lud0EkjOwt2aEioeKjAPBgNVHRMB | ||
Af8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCQkLp3GzZOXXXOKiMF6Iev1OUW | ||
w1jr7hVdJHOVGNCD6uZLuwSXJOWnEP8+hp8WvMl7SQAPpVYsTjdqhLATLaAZDucG | ||
sDq6oUTh/v8QVIBm0qF8+iMU8XZfgoeKuY13RXs23hneMAPQ5rcPwQhQEQkkqUvi | ||
Fq8qYFVd5mEr6Z62DT0s544WaBrpHr37mHOv0hIkHtX7Dy2Juc23MYw+W4PSD4fm | ||
sr1kARwHtY1meX+H3iRsX+7juTa33v+7H4IivhcPobIxFp+Hs9R5mx5u80wKMjVv | ||
t3STmB4nE7pABzucrjkSo43jIUwYN4rwydlSma9VkzvY6ry86HQuemycRb9H | ||
-----END CERTIFICATE----- |
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,23 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID1zCCAr+gAwIBAgIUJKTfvASV+RnwF7oLO84HZJDyvLwwDQYJKoZIhvcNAQEL | ||
BQAwVDEVMBMGA1UEAwwMVGVzdCBSb290IENBMQswCQYDVQQGEwJVUzENMAsGA1UE | ||
CAwEVGVzdDENMAsGA1UEBwwEVGVzdDEQMA4GA1UECgwHT3BlbmRhbDAeFw0yMzA4 | ||
MTQxNDU5MzZaFw0yNDA2MDkxNDU5MzZaMFkxGjAYBgNVBAMMEVJlZGlzIGNlcnRp | ||
ZmljYXRlMQswCQYDVQQGEwJVUzENMAsGA1UECAwEVGVzdDENMAsGA1UEBwwEVGVz | ||
dDEQMA4GA1UECgwHT3BlbmRhbDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC | ||
ggEBAIwREKDrRgZ2jlR3tpLHvMiW8JDu4JiLBxyrlJJE5ndhuH7MEgwz8HnXvxbD | ||
eyuamzkAzQIvqfVFVTRuVEYyEtoGzIegDL76H9ybuMGhKBK1m0TmiH7bOsAVMqZN | ||
vDtQJiw8qePtSq3G3H7Sw+/oudrJIc/f7kDox/lndKHTBmLbjSrvpkOJk2qnvhPJ | ||
ih4SuLNiW+tHv4sUdYBXXxn2wLHXNLGrlpeW28jtWGfu2noRCzikOYL/jwg2xzXV | ||
cBSuFwQ3swLDG/htqpePVA/sLxbXTt03A8fCajYcKiJdW88gqw4dW01ya8rCr5MU | ||
1C7lPwNCB8qNn8pdkmrh/Oc0zDsCAwEAAaOBmzCBmDAfBgNVHSMEGDAWgBQ2672x | ||
8uh6Lud0EkjOwt2aEioeKjAJBgNVHRMEAjAAMAsGA1UdDwQEAwIE8DA+BgNVHREE | ||
NzA1gglsb2NhbGhvc3SHBH8AAAGHBKweAAKHBKweAAOHBKweAASHBKweAAWHBKwe | ||
AAaHBKweAAcwHQYDVR0OBBYEFGvNF07RBwyi3tbpFIJtvWhXAGblMA0GCSqGSIb3 | ||
DQEBCwUAA4IBAQAd57+0YXfg8eIe2UkqLshIEonoIpKhmsIpRJyXLOUWYaSHri4w | ||
aDqPjogA39w34UcZsumfSReWBGrCyBroSCqQZOM166tw79+AVdjHHtgNm8pFRhO7 | ||
0vnFdAU30TOQP+mRF3mXz3hcK68U/4cRhXC5jXq8YRLiAG74G3PmXmmk2phtluEL | ||
SLLCvF5pCz3EaYsEKP+ZQpdY3BLp6Me7XDpGWPuNYVwVTJwwM9CLjQ8pxMlz1O1x | ||
HVN7xGtLz4dw9nEqnmjYBvH8aum+iAQPiHVuGfQfqIea28XeuyV4c5TL2b+OUsLY | ||
BRhX+z5OkGHXcMc1QDKo3PZcs8C1w8SC1x9D | ||
-----END CERTIFICATE----- |
Oops, something went wrong.