fix: gzip
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use std::fmt;
|
||||
|
||||
use aes::Aes128;
|
||||
use base64::{engine::general_purpose::STANDARD, Engine as _};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use cbc::{
|
||||
cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit},
|
||||
Decryptor,
|
||||
cipher::{BlockDecryptMut, KeyIvInit, block_padding::Pkcs7},
|
||||
};
|
||||
|
||||
type Aes128CbcDec = Decryptor<Aes128>;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::{routing::any, Router};
|
||||
use axum::{Router, routing::any};
|
||||
use dotenvy::dotenv;
|
||||
use tower_http::compression::CompressionLayer;
|
||||
use tracing::{error, info, level_filters::LevelFilter};
|
||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
|
||||
|
||||
mod crypto;
|
||||
mod proxy;
|
||||
@@ -54,6 +55,8 @@ 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()?;
|
||||
|
||||
@@ -68,6 +71,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Build our application with a single route
|
||||
let app = Router::new()
|
||||
.route("/{*path}", any(proxy::proxy_handler))
|
||||
.layer(CompressionLayer::new().gzip(true))
|
||||
.with_state(state);
|
||||
|
||||
// Run the server
|
||||
|
||||
45
src/proxy.rs
45
src/proxy.rs
@@ -10,7 +10,7 @@ use http_body_util::BodyExt;
|
||||
use serde_json::Value;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::{crypto, AppState};
|
||||
use crate::{AppState, crypto};
|
||||
|
||||
pub async fn proxy_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
@@ -61,7 +61,7 @@ pub async fn proxy_handler(
|
||||
}
|
||||
};
|
||||
|
||||
let (resp_parts, resp_body_bytes) = match clone_response(resp).await {
|
||||
let (resp_parts, resp_body) = match clone_response(resp).await {
|
||||
Ok(tuple) => tuple,
|
||||
Err(_) => {
|
||||
return (
|
||||
@@ -73,7 +73,7 @@ pub async fn proxy_handler(
|
||||
};
|
||||
|
||||
let decrypted_response_log =
|
||||
match process_and_log_response(&resp_body_bytes, &state.key, &state.iv).await {
|
||||
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);
|
||||
@@ -93,7 +93,7 @@ pub async fn proxy_handler(
|
||||
*response_builder.headers_mut().unwrap() = resp_parts.headers;
|
||||
}
|
||||
response_builder
|
||||
.body(axum::body::Body::from(resp_body_bytes))
|
||||
.body(axum::body::Body::from(resp_body))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
@@ -101,35 +101,32 @@ fn process_and_log_request(body_bytes: &Bytes, key: &str, iv: &str) -> anyhow::R
|
||||
let mut request_data: Value = serde_json::from_slice(body_bytes)?;
|
||||
|
||||
if let Some(params_value) = request_data.get_mut("params")
|
||||
&& let Some(params_str) = params_value.as_str() {
|
||||
let params_str_owned = params_str.to_string();
|
||||
match crypto::decrypt(¶ms_str_owned, key, iv) {
|
||||
Ok(decrypted_str) => {
|
||||
let decrypted_params: Value = serde_json::from_str(&decrypted_str)
|
||||
.unwrap_or(Value::String(decrypted_str));
|
||||
*params_value = decrypted_params;
|
||||
}
|
||||
Err(e) => {
|
||||
*params_value = Value::String(format!("decrypt failed: {}", e));
|
||||
}
|
||||
&& let Some(params_str) = params_value.as_str()
|
||||
{
|
||||
let params_str_owned = params_str.to_string();
|
||||
match crypto::decrypt(¶ms_str_owned, key, iv) {
|
||||
Ok(decrypted_str) => {
|
||||
let decrypted_params: Value =
|
||||
serde_json::from_str(&decrypted_str).unwrap_or(Value::String(decrypted_str));
|
||||
*params_value = decrypted_params;
|
||||
}
|
||||
Err(e) => {
|
||||
*params_value = Value::String(format!("decrypt failed: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(request_data)
|
||||
}
|
||||
|
||||
async fn process_and_log_response(
|
||||
body_bytes: &Bytes,
|
||||
key: &str,
|
||||
iv: &str,
|
||||
) -> anyhow::Result<String> {
|
||||
let decrypted = crypto::decrypt(std::str::from_utf8(body_bytes)?, key, iv)?;
|
||||
async fn process_and_log_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)?)
|
||||
}
|
||||
|
||||
async fn clone_response(
|
||||
resp: reqwest::Response,
|
||||
) -> anyhow::Result<(axum::http::response::Parts, Bytes)> {
|
||||
) -> anyhow::Result<(axum::http::response::Parts, String)> {
|
||||
let mut parts_builder = axum::http::response::Builder::new()
|
||||
.status(resp.status())
|
||||
.version(resp.version());
|
||||
@@ -138,7 +135,7 @@ async fn clone_response(
|
||||
*parts_builder.headers_mut().unwrap() = resp.headers().clone();
|
||||
}
|
||||
let parts = parts_builder.body(())?.into_parts().0;
|
||||
let body_bytes = resp.bytes().await?;
|
||||
let body_text = resp.text().await?;
|
||||
|
||||
Ok((parts, body_bytes))
|
||||
Ok((parts, body_text))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user