Skip to content

Commit

Permalink
unifiy proxy logic and refactor the basics
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Jul 31, 2024
1 parent b74827f commit d6e51c7
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 582 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ once_cell = "1.12.0"
url = { version = "2.2.2", features = ["serde"] }
redis = { version = "0.21.5", default-features = false, features = ["tokio-comp"] }
serde = "1.0.137"
serde_json = "1.0.81"
serde_json = { version = "1.0.81", features = ["preserve_order", "raw_value"] }
sha3 = "0.9"
simple_logger = "2.1.0"
tokio = { version = "1.12.0", default-features = false, features = ["macros", "rt-multi-thread", "sync", "time"] }
Expand Down
2 changes: 2 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ impl Db {
}
}

#[allow(dead_code)]
pub(crate) async fn key_exists(&mut self, key: &str) -> GenericResult<bool> {
Ok(redis::cmd("EXISTS")
.arg(key)
.query_async(&mut self.connection)
.await?)
}

#[allow(dead_code)]
pub(crate) async fn insert_cache(
&mut self,
key: &str,
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ mod rate_limiter;
mod rpc;
#[path = "net/server.rs"]
mod server;
#[path = "net/websocket.rs"]
mod websocket;

#[cfg(all(target_os = "linux", target_arch = "x86_64", target_env = "gnu"))]
#[global_allocator]
Expand Down
4 changes: 2 additions & 2 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub(crate) async fn http_handler(
"{}",
log_format!(
remote_addr.ip(),
payload.signed_message().address,
payload.proxy_sign().address,
req_uri,
"Request and payload data received."
)
Expand All @@ -160,7 +160,7 @@ pub(crate) async fn http_handler(
"{}",
log_format!(
remote_addr.ip(),
payload.signed_message().address,
payload.proxy_sign().address,
req_uri,
"Error type casting of IpAddr into HeaderValue, returning 500."
)
Expand Down
45 changes: 45 additions & 0 deletions src/net/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#![allow(dead_code)] // We will need this module for KDF RPCs

use bytes::Buf;
use ctx::AppConfig;
use http::{insert_jwt_to_http_header, APPLICATION_JSON};
use hyper::{body::aggregate, header, Body, Request};
use hyper_tls::HttpsConnector;
use proxy_signature::ProxySign;
use serde::{Deserialize, Serialize};
use serde_json::from_reader;

use super::*;
Expand All @@ -14,6 +18,47 @@ pub(crate) struct RpcClient {
pub(crate) url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub(crate) enum Id {
String(String),
Number(usize),
}

/// Payload for JSON-RPC calls
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub(crate) struct RpcPayload {
pub(crate) method: String,
pub(crate) params: serde_json::value::Value,
pub(crate) id: Id,
pub(crate) jsonrpc: String,
}

/// Used for websocket connection.
/// It combines standard JSON RPC method call fields (method, params, id, jsonrpc) with a `SignedMessage`
/// for authentication and validation, facilitating secure and validated interactions with the Quicknode service.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub(crate) struct RpcSocketPayload {
pub(crate) method: String,
pub(crate) params: serde_json::value::Value,
pub(crate) id: Id,
pub(crate) jsonrpc: String,
pub(crate) proxy_sign: ProxySign,
}

impl RpcSocketPayload {
pub(crate) fn into_parts(self) -> (RpcPayload, ProxySign) {
let payload = RpcPayload {
method: self.method,
params: self.params,
id: self.id,
jsonrpc: self.jsonrpc,
};
let proxy_sign = self.proxy_sign;
(payload, proxy_sign)
}
}

impl RpcClient {
pub(crate) fn new(url: String) -> Self {
RpcClient { url }
Expand Down
2 changes: 1 addition & 1 deletion src/net/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{GenericError, GenericResult};
use crate::ctx::{AppConfig, DEFAULT_PORT};
use crate::http::{http_handler, response_by_status, X_FORWARDED_FOR};
use crate::log_format;
use crate::websocket::{should_upgrade_to_socket_conn, socket_handler};
use crate::proxy::websocket::{should_upgrade_to_socket_conn, socket_handler};

// TODO: replace this macro with a helper function.
#[macro_export]
Expand Down
Loading

0 comments on commit d6e51c7

Please sign in to comment.