-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from AloeareV/cratify_netutils
move some rpc code to new zingo-netutils crate, for consumption by zingo-proxy
- Loading branch information
Showing
16 changed files
with
513 additions
and
463 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,23 @@ | ||
[package] | ||
name = "zingo-netutils" | ||
version = "0.1.0" | ||
authors = ["[email protected]"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
http.workspace = true | ||
tokio-rustls.workspace = true | ||
zcash_client_backend.workspace = true | ||
tower.workspace = true | ||
hyper-rustls.workspace = true | ||
webpki-roots = "0.21.0" | ||
hyper.workspace = true | ||
http-body.workspace = true | ||
tonic.workspace = true | ||
prost.workspace = true | ||
rustls-pemfile = "1.0.0" | ||
|
||
[features] | ||
test = [] | ||
|
||
|
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,125 @@ | ||
use std::sync::Arc; | ||
use tower::ServiceExt; | ||
|
||
use http::Uri; | ||
use http_body::combinators::UnsyncBoxBody; | ||
use hyper::client::HttpConnector; | ||
use tokio_rustls::rustls::{ClientConfig, RootCertStore}; | ||
use tonic::Status; | ||
use tower::util::BoxCloneService; | ||
use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient; | ||
|
||
type UnderlyingService = BoxCloneService< | ||
http::Request<UnsyncBoxBody<prost::bytes::Bytes, Status>>, | ||
http::Response<hyper::Body>, | ||
hyper::Error, | ||
>; | ||
|
||
#[derive(Clone)] | ||
pub struct GrpcConnector { | ||
uri: http::Uri, | ||
} | ||
|
||
impl GrpcConnector { | ||
pub fn new(uri: http::Uri) -> Self { | ||
Self { uri } | ||
} | ||
|
||
pub fn uri(&self) -> &Uri { | ||
&self.uri | ||
} | ||
|
||
pub fn get_client( | ||
&self, | ||
) -> impl std::future::Future< | ||
Output = Result<CompactTxStreamerClient<UnderlyingService>, Box<dyn std::error::Error>>, | ||
> { | ||
let uri = Arc::new(self.uri.clone()); | ||
async move { | ||
let mut http_connector = HttpConnector::new(); | ||
http_connector.enforce_http(false); | ||
if uri.scheme_str() == Some("https") { | ||
let mut roots = RootCertStore::empty(); | ||
roots.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map( | ||
|anchor_ref| { | ||
tokio_rustls::rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( | ||
anchor_ref.subject, | ||
anchor_ref.spki, | ||
anchor_ref.name_constraints, | ||
) | ||
}, | ||
)); | ||
|
||
#[cfg(test)] | ||
add_test_cert_to_roots(&mut roots); | ||
|
||
let tls = ClientConfig::builder() | ||
.with_safe_defaults() | ||
.with_root_certificates(roots) | ||
.with_no_client_auth(); | ||
let connector = tower::ServiceBuilder::new() | ||
.layer_fn(move |s| { | ||
let tls = tls.clone(); | ||
|
||
hyper_rustls::HttpsConnectorBuilder::new() | ||
.with_tls_config(tls) | ||
.https_or_http() | ||
.enable_http2() | ||
.wrap_connector(s) | ||
}) | ||
.service(http_connector); | ||
let client = Box::new(hyper::Client::builder().build(connector)); | ||
let uri = uri.clone(); | ||
let svc = tower::ServiceBuilder::new() | ||
//Here, we take all the pieces of our uri, and add in the path from the Requests's uri | ||
.map_request(move |mut req: http::Request<tonic::body::BoxBody>| { | ||
let uri = Uri::builder() | ||
.scheme(uri.scheme().unwrap().clone()) | ||
.authority(uri.authority().unwrap().clone()) | ||
//here. The Request's uri contains the path to the GRPC sever and | ||
//the method being called | ||
.path_and_query(req.uri().path_and_query().unwrap().clone()) | ||
.build() | ||
.unwrap(); | ||
|
||
*req.uri_mut() = uri; | ||
req | ||
}) | ||
.service(client); | ||
|
||
Ok(CompactTxStreamerClient::new(svc.boxed_clone())) | ||
} else { | ||
let connector = tower::ServiceBuilder::new().service(http_connector); | ||
let client = Box::new(hyper::Client::builder().http2_only(true).build(connector)); | ||
let uri = uri.clone(); | ||
let svc = tower::ServiceBuilder::new() | ||
//Here, we take all the pieces of our uri, and add in the path from the Requests's uri | ||
.map_request(move |mut req: http::Request<tonic::body::BoxBody>| { | ||
let uri = Uri::builder() | ||
.scheme(uri.scheme().unwrap().clone()) | ||
.authority(uri.authority().unwrap().clone()) | ||
//here. The Request's uri contains the path to the GRPC sever and | ||
//the method being called | ||
.path_and_query(req.uri().path_and_query().unwrap().clone()) | ||
.build() | ||
.unwrap(); | ||
|
||
*req.uri_mut() = uri; | ||
req | ||
}) | ||
.service(client); | ||
|
||
Ok(CompactTxStreamerClient::new(svc.boxed_clone())) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
fn add_test_cert_to_roots(roots: &mut RootCertStore) { | ||
const TEST_PEMFILE_PATH: &str = "test-data/localhost.pem"; | ||
let fd = std::fs::File::open(TEST_PEMFILE_PATH).unwrap(); | ||
let mut buf = std::io::BufReader::new(&fd); | ||
let certs = rustls_pemfile::certs(&mut buf).unwrap(); | ||
roots.add_parsable_certificates(&certs); | ||
} |
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
Oops, something went wrong.