From 6a88b02423e0fdb26a98bc15445eb3de41edae64 Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Thu, 27 Nov 2025 18:18:56 +0800 Subject: [PATCH] refactor --- Cargo.toml | 2 +- src/jsonrpc.rs | 1 + src/main.rs | 5 ++--- src/proxy.rs | 44 +++++++++++++++++++++------------------ src/{ => proxy}/crypto.rs | 0 5 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 src/jsonrpc.rs rename src/{ => proxy}/crypto.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index df5a200..8e7ab0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/jsonrpc.rs b/src/jsonrpc.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/jsonrpc.rs @@ -0,0 +1 @@ + diff --git a/src/main.rs b/src/main.rs index 2298590..efdc001 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()?; diff --git a/src/proxy.rs b/src/proxy.rs index b192522..8490d51 100644 --- a/src/proxy.rs +++ b/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>, @@ -18,7 +19,7 @@ pub async fn proxy_handler( req: Request, ) -> 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 { - let mut request_data: Value = serde_json::from_slice(body_bytes)?; +fn process_and_log_request(body: &str, key: &str, iv: &str) -> anyhow::Result { + 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 { +async fn decrypt_response(body_text: &str, key: &str, iv: &str) -> anyhow::Result { let decrypted = crypto::decrypt(body_text, key, iv)?; let formatted: Value = serde_json::from_str(&decrypted)?; Ok(serde_json::to_string_pretty(&formatted)?) diff --git a/src/crypto.rs b/src/proxy/crypto.rs similarity index 100% rename from src/crypto.rs rename to src/proxy/crypto.rs