diff --git a/Cargo.lock b/Cargo.lock index 7d7cf1d..2c3b7f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -963,7 +963,7 @@ dependencies = [ [[package]] name = "proxer" -version = "0.2.0" +version = "0.2.1" dependencies = [ "base64 0.22.1", "chrono", diff --git a/Cargo.toml b/Cargo.toml index a11ee64..546de45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proxer" -version = "0.2.0" +version = "0.2.1" edition = "2021" authors = ["doroved"] description = "Proxy all macOS network requests with domain-based filtering. Basic spoof DPI for direct connections." diff --git a/src/lib.rs b/src/lib.rs index c64f7e3..c0706df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,16 +92,16 @@ pub async fn handle_request( let found_proxy = find_matching_proxy(config.as_ref(), host); if req.method() == Method::CONNECT { - if let Some(_) = req.uri().authority().map(|auth| auth.to_string()) { + if req.uri().authority().map(|auth| auth.to_string()).is_some() { tokio::spawn(async move { match tunnel(req, found_proxy.clone()).await { Ok(_) => {} Err(e) => { let error_msg = e.to_string(); - if options.log_error_all { - eprintln!("\x1B[31m\x1B[1m[{time}] {addr} → {error_msg}\x1B[0m"); - } else if error_msg.contains("os error 60") + // Combined error logging condition + if options.log_error_all + || error_msg.contains("os error 60") || error_msg.contains("Proxy connection failed") || error_msg.contains("deadline has elapsed") { @@ -256,7 +256,7 @@ async fn tunnel( _ => return Err(format!("Unsupported proxy scheme: {}", proxy.scheme).into()), } - return Ok(()); + Ok(()) } else { println!("\x1b[1m[{time}] {addr} → Direct connection\x1b[0m"); @@ -311,7 +311,7 @@ async fn tunnel( // Wait for both tasks to complete tokio::try_join!(client_to_server, server_to_client)?; - return Ok(()); + Ok(()) } } @@ -331,7 +331,7 @@ fn package_info() -> Package { let content = include_str!("../Cargo.toml"); // Parse the content of the file - let cargo: CargoToml = from_str(&content).expect("Error parsing Cargo.toml"); + let cargo: CargoToml = from_str(content).expect("Error parsing Cargo.toml"); // Return the package information cargo.package @@ -340,7 +340,7 @@ fn package_info() -> Package { // Kill all proxer processes pub fn terminate_proxer() { let _ = Command::new("sh") - .args(&["-c", "kill $(pgrep proxer)"]) + .args(["-c", "kill $(pgrep proxer)"]) .output() .expect("Failed to execute `kill $(pgrep proxer)` command to terminate proxer processes"); } @@ -403,7 +403,7 @@ async fn handle_http_proxy( addr: &str, proxy: &ProxyConfig, ) -> Result<(), Box> { - send_connect_request(req, &mut tcp_stream, addr, &proxy).await?; + send_connect_request(req, &mut tcp_stream, addr, proxy).await?; Ok(()) } @@ -420,7 +420,7 @@ async fn handle_https_proxy( ) .await??; - send_connect_request(req, &mut tls_stream, addr, &proxy).await?; + send_connect_request(req, &mut tls_stream, addr, proxy).await?; Ok(()) } @@ -517,7 +517,7 @@ impl Proxy { &self.server, &self.port.to_string(), ]) - .expect(&format!("Failed to set {proxy_type}")); + .unwrap_or_else(|_| panic!("Failed to set {proxy_type}")); } } @@ -533,7 +533,7 @@ impl Proxy { let _ = self .execute_command(&[&command, &self.interface, proxy_state]) - .expect(&format!("Failed to set {proxy_type} state")); + .unwrap_or_else(|_| panic!("Failed to set {proxy_type} state")); } } diff --git a/src/main.rs b/src/main.rs index 36cd6a4..c41c040 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,9 @@ async fn main() -> Result<(), Box> { }; let config = fs::read_to_string(&config_file) - .expect(format!("Failed to read config file: {config_file}").as_str()); + .unwrap_or_else(|_| panic!("Failed to read config file: {config_file}")); let parsed_config: Vec = json5::from_str(&config) - .expect(format!("Failed to parse config file: {config_file}").as_str()); + .unwrap_or_else(|_| panic!("Failed to parse config file: {config_file}")); let shared_config = Arc::new(parsed_config); // Run ping proxy in loop @@ -76,12 +76,12 @@ async fn main() -> Result<(), Box> { let system_proxy_arc = Arc::new(system_proxy); // Set up signal handling - let mut signals = Signals::new(&[SIGINT, SIGTERM, SIGQUIT])?; + let mut signals = Signals::new([SIGINT, SIGTERM, SIGQUIT])?; let handle = signals.handle(); // Start signal handler in a separate task let signals_task = tokio::spawn(async move { - while let Some(signal) = signals.next().await { + if let Some(signal) = signals.next().await { match signal { SIGINT | SIGTERM | SIGQUIT => { system_proxy_arc.set_state(ProxyState::Off);