From 82b09d74ae36ec271b3a4fa0d2d6b6ce261bd5ee Mon Sep 17 00:00:00 2001 From: postscriptum Date: Mon, 23 Mar 2026 04:28:36 +0200 Subject: [PATCH] minor logic optimization --- README.md | 2 +- src/list.rs | 11 ++++------- src/main.rs | 13 ++++++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9abdd48..41e3ce6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Experimental async SOCKS5 (TCP/UDP) proxy server based on [fast-socks5](https:// ## Goals -* Ad/tracking protection +* Ad/tracking protection (before sending a DNS request) * Reduce CPU usage by filtering extra SSL traffic on background ## Roadmap diff --git a/src/list.rs b/src/list.rs index ed7eabd..330e1f1 100644 --- a/src/list.rs +++ b/src/list.rs @@ -51,13 +51,10 @@ impl List { cache, }) } - pub async fn any(&self, values: &[&str]) -> bool { - let guard = self.index.read().await; - values.iter().any(|&value| { - guard.iter().any(|item| match item { - Item::Exact(v) => v == value, - Item::Ending(v) => value.ends_with(v), - }) + pub async fn any(&self, value: &str) -> bool { + self.index.read().await.iter().any(|item| match item { + Item::Exact(v) => v == value, + Item::Ending(v) => value.ends_with(v), }) } pub async fn entries(&self) -> u64 { diff --git a/src/main.rs b/src/main.rs index 77ea8ab..aed36ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,6 @@ async fn serve_socks5( totals: Arc, ) -> Result<(), SocksError> { totals.increase_request(); - let request = match &opt.auth { AuthMode::NoAuth if opt.skip_auth => { Socks5ServerProtocol::skip_auth_this_is_not_rfc_compliant(socket) @@ -137,16 +136,20 @@ async fn serve_socks5( .read_command() .await?; - let (host, _) = request.2.clone().into_string_and_port(); // @TODO ref - let (proto, cmd, addr) = request.resolve_dns().await?; + let (host, _) = request.2.clone().into_string_and_port(); - if !list.any(&[&host, &addr.to_string()]).await { + if !list.any(&host).await { totals.increase_blocked(); info!("Blocked connection attempt to: {host}"); - proto.reply_error(&ReplyError::ConnectionNotAllowed).await?; + request + .0 + .reply_error(&ReplyError::ConnectionNotAllowed) + .await?; return Err(ReplyError::ConnectionNotAllowed.into()); } + let (proto, cmd, addr) = request.resolve_dns().await?; + match cmd { Socks5Command::TCPConnect => { run_tcp_proxy(proto, &addr, opt.request_timeout, false).await?;