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