From 51631f7a8aaa079d6d60e598148634a1c779d223 Mon Sep 17 00:00:00 2001 From: doroved Date: Wed, 18 Dec 2024 18:25:16 +0300 Subject: [PATCH] Update proxer-cli to version 0.3.5 and refactor error logging - Bump version of `proxer-cli` in `Cargo.toml` and `Cargo.lock` from 0.3.4 to 0.3.5. - Refactor error logging in `src/server/proxy.rs` to use a new `tracing_error` utility function for improved readability and consistency in error messages. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/server/proxy.rs | 18 +++++++++--------- src/server/utils.rs | 4 ++++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 332b5a5..03efd01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -744,7 +744,7 @@ dependencies = [ [[package]] name = "proxer-cli" -version = "0.3.4" +version = "0.3.5" dependencies = [ "base64", "bytes", diff --git a/Cargo.toml b/Cargo.toml index fdc28f5..21fbc87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proxer-cli" -version = "0.3.4" +version = "0.3.5" edition = "2021" authors = ["doroved"] description = "Proxy TCP traffic on macOS with domain filtering." diff --git a/src/server/proxy.rs b/src/server/proxy.rs index b69d3a5..246de60 100644 --- a/src/server/proxy.rs +++ b/src/server/proxy.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use super::ProxyConfig; use crate::server::tunnel::{tunnel_direct, tunnel_via_proxy}; +use crate::server::utils::tracing_error; use bytes::Bytes; use http::{Method, Request, Response}; @@ -29,21 +30,19 @@ pub async fn handle_request( if let Err(e) = tunnel_via_proxy(upgraded, &addr, proxy, &filter_name).await { - tracing::error!( - "\x1B[31m{addr} → PROXY connection error: {e}\x1B[0m" - ); + tracing_error(&format!("{addr} → PROXY connection error: {e}")); }; } else if let Err(e) = tunnel_direct(upgraded, &addr).await { - tracing::error!("\x1B[31m{addr} → DIRECT connection error: {e}\x1B[0m"); + tracing_error(&format!("{addr} → DIRECT connection error: {e}")); } } - Err(e) => tracing::error!("\x1B[31m{addr} → UPGRADE error: {e}\x1B[0m"), + Err(e) => tracing_error(&format!("{addr} → UPGRADE error: {e}")), } }); Ok(Response::new(empty())) } else { - tracing::error!("CONNECT host is not socket addr: {:?}", req.uri()); + tracing_error(&format!("CONNECT host is not socket addr: {:?}", req.uri())); let mut resp = Response::new(full("CONNECT must be to a socket address")); *resp.status_mut() = http::StatusCode::BAD_REQUEST; @@ -68,15 +67,16 @@ pub async fn handle_request( tokio::spawn(async move { if let Err(err) = conn.await { - tracing::error!("\x1B[31mConnection failed: {:?}\x1B[0m", err); + tracing_error(&format!("Connection failed: {:?}", err)); } }); let resp = sender.send_request(req).await?; Ok(resp.map(|b| b.boxed())) } - Err(e) => { - tracing::error!("\x1B[31m{host}:{port} → Failed to connect: {:?}\x1B[0m", e); + Err(err) => { + tracing_error(&format!("{host}:{port} → Failed to connect: {:?}", err)); + let mut resp = Response::new(full("Failed to connect to host")); *resp.status_mut() = http::StatusCode::BAD_GATEWAY; diff --git a/src/server/utils.rs b/src/server/utils.rs index c5cf4fc..8fa90e8 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -15,3 +15,7 @@ pub fn terminate_proxer() { "Failed to execute `kill $(pgrep proxer-cli)` command to terminate proxer processes", ); } + +pub fn tracing_error(message: &str) { + tracing::error!("\x1B[31m{message}\x1B[0m"); +}