-
Notifications
You must be signed in to change notification settings - Fork 94
Proxy Module Refactor #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e7a51b
wip
0xForerunner 39ef56e
wip
0xForerunner e49f054
wip
0xForerunner f77ff56
wip
0xForerunner 6249337
clean things up
0xForerunner 0641965
fix for cloned service
0xForerunner 79bab4a
cleanup process_response
0xForerunner 8ceabac
eyre bail
0xForerunner bf55d6e
remove unnecessary deps
0xForerunner 0a1d9eb
working
0xForerunner adbf5f3
Update src/client/http.rs
0xForerunner 636f7e9
Merge branch 'main' into forerunner/proxy
0xForerunner f5e8f69
parse response cod
0xForerunner 7497301
clippy fix
0xForerunner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| use crate::server::PayloadSource; | ||
| use alloy_rpc_types_engine::JwtSecret; | ||
| use http::Uri; | ||
| use http_body_util::BodyExt; | ||
| use hyper_rustls::HttpsConnector; | ||
| use hyper_util::client::legacy::Client; | ||
| use hyper_util::client::legacy::connect::HttpConnector; | ||
| use hyper_util::rt::TokioExecutor; | ||
| use jsonrpsee::core::BoxError; | ||
| use jsonrpsee::http_client::HttpBody; | ||
| use opentelemetry::trace::SpanKind; | ||
| use tower::{Service as _, ServiceBuilder, ServiceExt}; | ||
| use tower_http::decompression::{Decompression, DecompressionLayer}; | ||
| use tracing::{debug, error, instrument}; | ||
|
|
||
| use super::auth::{AuthClientLayer, AuthClientService}; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct HttpClient { | ||
| client: Decompression<AuthClientService<Client<HttpsConnector<HttpConnector>, HttpBody>>>, | ||
| url: Uri, | ||
| target: PayloadSource, | ||
| } | ||
|
|
||
| impl HttpClient { | ||
| pub(crate) fn new(url: Uri, secret: JwtSecret, target: PayloadSource) -> Self { | ||
| let connector = hyper_rustls::HttpsConnectorBuilder::new() | ||
| .with_native_roots() | ||
| .expect("no native root CA certificates found") | ||
| .https_or_http() | ||
| .enable_http1() | ||
| .enable_http2() | ||
| .build(); | ||
|
|
||
| let client = Client::builder(TokioExecutor::new()).build(connector); | ||
|
|
||
| let client = ServiceBuilder::new() | ||
| .layer(DecompressionLayer::new()) | ||
| .layer(AuthClientLayer::new(secret)) | ||
| .service(client); | ||
|
|
||
| Self { | ||
| client, | ||
| url, | ||
| target, | ||
| } | ||
| } | ||
|
|
||
| /// Forwards an HTTP request to the `authrpc`, attaching the provided JWT authorization. | ||
| #[instrument( | ||
| skip(self, req), | ||
| fields(otel.kind = ?SpanKind::Client), | ||
| err(Debug) | ||
| )] | ||
| pub async fn forward( | ||
| &mut self, | ||
| mut req: http::Request<HttpBody>, | ||
| method: String, | ||
| ) -> Result<http::Response<HttpBody>, BoxError> { | ||
| debug!("forwarding {} to {}", method, self.target); | ||
| *req.uri_mut() = self.url.clone(); | ||
|
|
||
| let res = self.client.ready().await?.call(req).await?; | ||
|
|
||
| let (parts, body) = res.into_parts(); | ||
| let body_bytes = body.collect().await?.to_bytes().to_vec(); | ||
|
|
||
| if let Some(code) = parse_response_code(&body_bytes)? { | ||
| error!(%code, "error in forwarded response"); | ||
| } | ||
|
|
||
| Ok(http::Response::from_parts( | ||
| parts, | ||
| HttpBody::from(body_bytes), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| fn parse_response_code(body_bytes: &[u8]) -> eyre::Result<Option<i32>> { | ||
| #[derive(serde::Deserialize, Debug)] | ||
| struct RpcResponse { | ||
| error: Option<JsonRpcError>, | ||
| } | ||
|
|
||
| #[derive(serde::Deserialize, Debug)] | ||
| struct JsonRpcError { | ||
| code: i32, | ||
| } | ||
|
|
||
| let res = serde_json::from_slice::<RpcResponse>(body_bytes)?; | ||
|
|
||
| Ok(res.error.map(|e| e.code)) | ||
| } | ||
This file contains hidden or 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,3 @@ | ||
| pub mod auth; | ||
| pub mod http; | ||
| pub mod rpc; |
This file contains hidden or 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 hidden or 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,67 @@ | ||
| use std::{ | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use futures::FutureExt as _; | ||
| use jsonrpsee::{ | ||
| core::BoxError, | ||
| http_client::{HttpBody, HttpRequest, HttpResponse}, | ||
| }; | ||
| use tower::{Layer, Service, util::Either}; | ||
|
|
||
| /// A [`Layer`] that filters out /healthz requests and responds with a 200 OK. | ||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct HealthLayer; | ||
|
|
||
| impl<S> Layer<S> for HealthLayer { | ||
| type Service = HealthService<S>; | ||
|
|
||
| fn layer(&self, inner: S) -> Self::Service { | ||
| HealthService { inner } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct HealthService<S> { | ||
| inner: S, | ||
| } | ||
|
|
||
| impl<S> Service<HttpRequest<HttpBody>> for HealthService<S> | ||
| where | ||
| S: Service<HttpRequest<HttpBody>, Response = HttpResponse> + Send + Sync + Clone + 'static, | ||
| S::Response: 'static, | ||
| S::Error: Into<BoxError> + 'static, | ||
| S::Future: Send + 'static, | ||
| { | ||
| type Response = HttpResponse; | ||
| type Error = BoxError; | ||
| type Future = Either< | ||
| Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>, | ||
| S::Future, | ||
| >; | ||
|
|
||
| fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.inner.poll_ready(cx).map_err(Into::into) | ||
| } | ||
|
|
||
| fn call(&mut self, request: HttpRequest<HttpBody>) -> Self::Future { | ||
| if request.uri().path() == "/healthz" { | ||
| Either::A(Self::healthz().boxed()) | ||
| } else { | ||
| Either::B(self.inner.call(request)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S> HealthService<S> | ||
| where | ||
| S: Service<HttpRequest<HttpBody>, Response = HttpResponse> + Send + Sync + Clone + 'static, | ||
| S::Response: 'static, | ||
| S::Error: Into<BoxError> + 'static, | ||
| S::Future: Send + 'static, | ||
| { | ||
| async fn healthz() -> Result<HttpResponse, BoxError> { | ||
| Ok(HttpResponse::new(HttpBody::from("OK"))) | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you tested this against the optimism package? does this handle gzip responses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we've run it in the optimism kurtosis devnet and everything seems to work great!