Skip to content

Commit f73f907

Browse files
committed
refactor: replace Lazy with LazyLock for improved thread safety and simplify connect method signatures
1 parent 0774efc commit f73f907

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

crates/shadowsocks-service/src/local/net/tcp/auto_proxy_stream.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ use {
3737
bytes::{BufMut, BytesMut},
3838
httparse::{Response, Status},
3939
log::warn,
40-
once_cell::sync::Lazy,
4140
rustls_native_certs::CertificateResult,
4241
std::io::ErrorKind,
42+
std::sync::LazyLock,
4343
tokio::io::{AsyncReadExt, AsyncWriteExt},
4444
tokio_rustls::{
45-
rustls::{pki_types::ServerName, ClientConfig, RootCertStore},
4645
TlsConnector,
46+
rustls::{ClientConfig, RootCertStore, pki_types::ServerName},
4747
},
4848
};
4949
#[cfg(feature = "https-tunnel")]
@@ -88,11 +88,7 @@ impl AutoProxyClientStream {
8888
}
8989
}
9090
/// Connect to target `addr` via shadowsocks' server configured by `svr_cfg`
91-
pub async fn connect<A>(
92-
context: Arc<ServiceContext>,
93-
server: &ServerIdent,
94-
addr: A,
95-
) -> io::Result<Self>
91+
pub async fn connect<A>(context: Arc<ServiceContext>, server: &ServerIdent, addr: A) -> io::Result<Self>
9692
where
9793
A: Into<Address>,
9894
{
@@ -139,7 +135,7 @@ impl AutoProxyClientStream {
139135
)
140136
.await?;
141137

142-
static TLS_CONFIG: Lazy<Arc<ClientConfig>> = Lazy::new(|| {
138+
static TLS_CONFIG: LazyLock<Arc<ClientConfig>> = LazyLock::new(|| {
143139
let mut config = ClientConfig::builder()
144140
.with_root_certificates({
145141
// Load WebPKI roots (Mozilla's root certificates)
@@ -182,6 +178,7 @@ impl AutoProxyClientStream {
182178
let tls_stream = connector
183179
.connect(host.to_owned(), MonProxyStream::from_stream(stream, flow_stat))
184180
.await?;
181+
185182
use base64::Engine;
186183
let base64 = base64::engine::general_purpose::STANDARD.encode(server.server_config().password());
187184
Ok(AutoProxyClientStream::HttpTunnel(HttpTunnelStream {
@@ -220,16 +217,11 @@ impl AutoProxyClientStream {
220217
}
221218

222219
/// Connect to target `addr` via shadowsocks' server configured by `svr_cfg`
223-
pub async fn connect_proxied<A>(
224-
context: Arc<ServiceContext>,
225-
server: &ServerIdent,
226-
addr: A,
227-
) -> io::Result<Self>
220+
pub async fn connect_proxied<A>(context: Arc<ServiceContext>, server: &ServerIdent, addr: A) -> io::Result<Self>
228221
where
229222
A: Into<Address>,
230223
{
231-
Self::connect_proxied_with_opts(context.clone(), server, addr, context.connect_opts_ref())
232-
.await
224+
Self::connect_proxied_with_opts(context.clone(), server, addr, context.connect_opts_ref()).await
233225
}
234226

235227
/// Connect to target `addr` via shadowsocks' server configured by `svr_cfg`

0 commit comments

Comments
 (0)