refactor
This commit is contained in:
@@ -20,4 +20,4 @@ anyhow = "1"
|
||||
thiserror = "2"
|
||||
http-body-util = "0.1.1"
|
||||
chrono = { version = "0.4", features = ["clock"] }
|
||||
reqwest = { version = "0.12", features = ["json", "rustls-tls", "gzip", "deflate"], default-features = false }
|
||||
reqwest = { version = "0.12", features = ["json", "rustls-tls", "gzip"], default-features = false }
|
||||
|
||||
1
src/jsonrpc.rs
Normal file
1
src/jsonrpc.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -7,7 +7,7 @@ use tower_http::compression::CompressionLayer;
|
||||
use tracing::{error, info, level_filters::LevelFilter};
|
||||
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
|
||||
|
||||
mod crypto;
|
||||
mod jsonrpc;
|
||||
mod proxy;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -25,7 +25,7 @@ const DEFAULT_PORT: &str = "8080";
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Load environment variables from .env file
|
||||
dotenv().ok();
|
||||
let _ = dotenv();
|
||||
|
||||
// Set up logging
|
||||
tracing_subscriber::registry()
|
||||
@@ -56,7 +56,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Create a reqwest client that ignores SSL certificate verification
|
||||
let client = reqwest::Client::builder()
|
||||
.gzip(true)
|
||||
.deflate(true)
|
||||
.danger_accept_invalid_certs(true)
|
||||
.build()?;
|
||||
|
||||
|
||||
44
src/proxy.rs
44
src/proxy.rs
@@ -1,7 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
extract::{Path, State},
|
||||
http::{Request, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
@@ -10,7 +9,9 @@ use http_body_util::BodyExt;
|
||||
use serde_json::Value;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::{AppState, crypto};
|
||||
use crate::AppState;
|
||||
|
||||
mod crypto;
|
||||
|
||||
pub async fn proxy_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
@@ -18,7 +19,7 @@ pub async fn proxy_handler(
|
||||
req: Request<axum::body::Body>,
|
||||
) -> impl IntoResponse {
|
||||
let (parts, body) = req.into_parts();
|
||||
let body_bytes = match body.collect().await {
|
||||
let body = match body.collect().await {
|
||||
Ok(body) => body.to_bytes(),
|
||||
Err(e) => {
|
||||
error!("Failed to read request body: {}", e);
|
||||
@@ -30,24 +31,28 @@ pub async fn proxy_handler(
|
||||
}
|
||||
};
|
||||
|
||||
let decrypted_request_log = match process_and_log_request(&body_bytes, &state.key, &state.iv) {
|
||||
Ok(log) => log,
|
||||
Err(e) => {
|
||||
let decrypted_request_log = match str::from_utf8(&body)
|
||||
.map(|body| process_and_log_request(body, &state.key, &state.iv))
|
||||
{
|
||||
Ok(Ok(log)) => log,
|
||||
Ok(Err(e)) => {
|
||||
warn!("Failed to process request for logging: {}", e);
|
||||
Value::String("Could not decrypt request".to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to decode request for logging: {}", e);
|
||||
Value::String("Could not decrypt request".to_string())
|
||||
}
|
||||
};
|
||||
|
||||
let mut target_url = state.target_url.clone();
|
||||
target_url.set_path(&path);
|
||||
target_url.set_query(parts.uri.query());
|
||||
|
||||
let mut forwarded_req = state.client.request(parts.method, target_url);
|
||||
|
||||
if !parts.headers.is_empty() {
|
||||
forwarded_req = forwarded_req.headers(parts.headers);
|
||||
}
|
||||
forwarded_req = forwarded_req.body(body_bytes);
|
||||
forwarded_req = forwarded_req.body(body);
|
||||
|
||||
let resp = match forwarded_req.send().await {
|
||||
Ok(resp) => resp,
|
||||
@@ -72,14 +77,13 @@ pub async fn proxy_handler(
|
||||
}
|
||||
};
|
||||
|
||||
let decrypted_response_log =
|
||||
match process_and_log_response(&resp_body, &state.key, &state.iv).await {
|
||||
Ok(log) => log,
|
||||
Err(e) => {
|
||||
warn!("Failed to process response for logging: {}", e);
|
||||
"Could not decrypt response".to_string()
|
||||
}
|
||||
};
|
||||
let decrypted_response_log = match decrypt_response(&resp_body, &state.key, &state.iv).await {
|
||||
Ok(log) => log,
|
||||
Err(e) => {
|
||||
warn!("Failed to process response for logging: {}", e);
|
||||
"Could not decrypt response".to_string()
|
||||
}
|
||||
};
|
||||
|
||||
info!(
|
||||
"\nRequest:\n{}\nResponse:\n{}\n{}",
|
||||
@@ -97,8 +101,8 @@ pub async fn proxy_handler(
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn process_and_log_request(body_bytes: &Bytes, key: &str, iv: &str) -> anyhow::Result<Value> {
|
||||
let mut request_data: Value = serde_json::from_slice(body_bytes)?;
|
||||
fn process_and_log_request(body: &str, key: &str, iv: &str) -> anyhow::Result<Value> {
|
||||
let mut request_data: Value = serde_json::from_str(body)?;
|
||||
|
||||
if let Some(params_value) = request_data.get_mut("params")
|
||||
&& let Some(params_str) = params_value.as_str()
|
||||
@@ -118,7 +122,7 @@ fn process_and_log_request(body_bytes: &Bytes, key: &str, iv: &str) -> anyhow::R
|
||||
Ok(request_data)
|
||||
}
|
||||
|
||||
async fn process_and_log_response(body_text: &str, key: &str, iv: &str) -> anyhow::Result<String> {
|
||||
async fn decrypt_response(body_text: &str, key: &str, iv: &str) -> anyhow::Result<String> {
|
||||
let decrypted = crypto::decrypt(body_text, key, iv)?;
|
||||
let formatted: Value = serde_json::from_str(&decrypted)?;
|
||||
Ok(serde_json::to_string_pretty(&formatted)?)
|
||||
|
||||
Reference in New Issue
Block a user