-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
367 additions
and
252 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,20 @@ | ||
use std::fmt::Display; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub enum RpcClientApp { | ||
/// Client application identified itself. | ||
Identified(String), | ||
|
||
/// Client application is unknown. | ||
#[default] | ||
Unknown, | ||
} | ||
|
||
impl Display for RpcClientApp { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
RpcClientApp::Identified(name) => write!(f, "{}", name), | ||
RpcClientApp::Unknown => write!(f, "unknown"), | ||
} | ||
} | ||
} |
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,58 @@ | ||
use core::future::Future; | ||
use core::pin::Pin; | ||
use std::collections::HashMap; | ||
|
||
use futures::TryFutureExt; | ||
use jsonrpsee::client_transport::ws::Uri; | ||
use jsonrpsee::core::BoxError; | ||
use jsonrpsee::server::HttpBody; | ||
use jsonrpsee::server::HttpRequest; | ||
use jsonrpsee::server::HttpResponse; | ||
use tower::Service; | ||
|
||
use crate::eth::rpc::RpcClientApp; | ||
|
||
#[derive(Debug, Clone, derive_new::new)] | ||
pub struct RpcHttpMiddleware<S> { | ||
service: S, | ||
} | ||
|
||
impl<S> Service<HttpRequest<HttpBody>> for RpcHttpMiddleware<S> | ||
where | ||
S: Service<HttpRequest, Response = HttpResponse>, | ||
S::Error: Into<BoxError> + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
type Response = S::Response; | ||
type Error = BoxError; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> { | ||
self.service.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
fn call(&mut self, mut request: HttpRequest<HttpBody>) -> Self::Future { | ||
let client_app = parse_client_app(request.uri()); | ||
request.extensions_mut().insert(client_app); | ||
|
||
Box::pin(self.service.call(request).map_err(Into::into)) | ||
} | ||
} | ||
|
||
/// Extracts the client application name from the `app` query parameter. | ||
fn parse_client_app(uri: &Uri) -> RpcClientApp { | ||
let Some(query_params_str) = uri.query() else { return RpcClientApp::Unknown }; | ||
|
||
let query_params: HashMap<String, String> = match serde_urlencoded::from_str(query_params_str) { | ||
Ok(url) => url, | ||
Err(e) => { | ||
tracing::error!(reason = ?e, "failed to parse http request query parameters"); | ||
return RpcClientApp::Unknown; | ||
} | ||
}; | ||
|
||
match query_params.get("app") { | ||
Some(app) => RpcClientApp::Identified(app.to_owned()), | ||
None => RpcClientApp::Unknown, | ||
} | ||
} |
Oops, something went wrong.