From 040ac0ffe335607c65ec1825f7b50c9c2c7f50bc Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 18 Sep 2024 11:09:24 +0000 Subject: [PATCH] refactor: do not wrap shadowsocks::ProxyClientStream Updated `shadowsocks` implements `Debug` for the type, so there is no need to wrap it. --- Cargo.lock | 5 ++-- Cargo.toml | 3 +-- src/net/proxy.rs | 64 ++-------------------------------------------- src/net/session.rs | 5 ++-- 4 files changed, 7 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 285b673d82..77a94e7642 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1315,7 +1315,6 @@ dependencies = [ "parking_lot", "percent-encoding", "pgp", - "pin-project", "pretty_assertions", "proptest", "qrcodegen", @@ -5385,9 +5384,9 @@ dependencies = [ [[package]] name = "shadowsocks" -version = "1.20.2" +version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06b6af20f0f009894644c9fb149ce6244c69b0a264ffcf7a53cbb3dd4883e4a3" +checksum = "5ecb3780dfbc654de9383758015b9bb95c6e32fecace36ebded09d67e854d130" dependencies = [ "aes", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index d4807b3fe2..bd54f23510 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,6 @@ once_cell = { workspace = true } parking_lot = "0.12" percent-encoding = "2.3" pgp = { version = "0.13.2", default-features = false } -pin-project = "1" qrcodegen = "1.7.0" quick-xml = "0.36" quoted_printable = "0.5" @@ -90,7 +89,7 @@ serde_json = { workspace = true } serde_urlencoded = "0.7.1" serde = { workspace = true, features = ["derive"] } sha-1 = "0.10" -shadowsocks = { version = "1.20.2", default-features = false, features = ["aead-cipher-2022"] } +shadowsocks = { version = "1.21.0", default-features = false, features = ["aead-cipher-2022"] } smallvec = "1.13.2" strum = "0.26" strum_macros = "0.26" diff --git a/src/net/proxy.rs b/src/net/proxy.rs index 1f5a56c244..caaebcf33c 100644 --- a/src/net/proxy.rs +++ b/src/net/proxy.rs @@ -13,8 +13,7 @@ use fast_socks5::util::target_addr::ToTargetAddr; use fast_socks5::AuthenticationMethod; use fast_socks5::Socks5Command; use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; -use pin_project::pin_project; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; use tokio_io_timeout::TimeoutStream; use url::Url; @@ -41,62 +40,6 @@ impl PartialEq for ShadowsocksConfig { impl Eq for ShadowsocksConfig {} -/// Wrapper for Shadowsocks stream implementing -/// `Debug` and `SessionStream`. -/// -/// Passes `AsyncRead` and `AsyncWrite` traits through. -#[pin_project] -pub(crate) struct ShadowsocksStream { - #[pin] - pub(crate) stream: shadowsocks::ProxyClientStream, -} - -impl std::fmt::Debug for ShadowsocksStream { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "ShadowsocksStream") - } -} - -impl AsyncRead for ShadowsocksStream -where - S: AsyncRead + AsyncWrite + Unpin, -{ - fn poll_read( - self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - buf: &mut tokio::io::ReadBuf<'_>, - ) -> std::task::Poll> { - self.project().stream.poll_read(cx, buf) - } -} - -impl AsyncWrite for ShadowsocksStream -where - S: AsyncRead + AsyncWrite + Unpin, -{ - fn poll_write( - self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - buf: &[u8], - ) -> std::task::Poll> { - self.project().stream.poll_write(cx, buf) - } - - fn poll_flush( - self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll> { - self.project().stream.poll_flush(cx) - } - - fn poll_shutdown( - self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll> { - self.project().stream.poll_shutdown(cx) - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct HttpConfig { /// HTTP proxy host. @@ -469,15 +412,12 @@ impl ProxyConfig { .context("Failed to connect to Shadowsocks proxy")? }; - let proxy_client_stream = shadowsocks::ProxyClientStream::from_stream( + let shadowsocks_stream = shadowsocks::ProxyClientStream::from_stream( shadowsocks_context, tcp_stream, server_config, (target_host.to_string(), target_port), ); - let shadowsocks_stream = ShadowsocksStream { - stream: proxy_client_stream, - }; Ok(Box::new(shadowsocks_stream)) } diff --git a/src/net/session.rs b/src/net/session.rs index 4dc8fbc338..18a758e065 100644 --- a/src/net/session.rs +++ b/src/net/session.rs @@ -1,4 +1,3 @@ -use crate::net::proxy::ShadowsocksStream; use async_native_tls::TlsStream; use fast_socks5::client::Socks5Stream; use std::pin::Pin; @@ -45,9 +44,9 @@ impl SessionStream for Socks5Stream { self.get_socket_mut().set_read_timeout(timeout) } } -impl SessionStream for ShadowsocksStream { +impl SessionStream for shadowsocks::ProxyClientStream { fn set_read_timeout(&mut self, timeout: Option) { - self.stream.get_mut().set_read_timeout(timeout) + self.get_mut().set_read_timeout(timeout) } }