diff --git a/crates/shadowsocks-service/src/config.rs b/crates/shadowsocks-service/src/config.rs index 3bc451e2..7a97860e 100644 --- a/crates/shadowsocks-service/src/config.rs +++ b/crates/shadowsocks-service/src/config.rs @@ -1338,30 +1338,24 @@ impl LocalConfig { match self.protocol { #[cfg(feature = "local-dns")] - ProtocolType::Dns => { - if self.local_dns_addr.is_none() || self.remote_dns_addr.is_none() { - let err = Error::new( - ErrorKind::MissingField, - "missing `local_dns_addr` or `remote_dns_addr` in configuration", - None, - ); - return Err(err); - } + ProtocolType::Dns if self.local_dns_addr.is_none() || self.remote_dns_addr.is_none() => { + let err = Error::new( + ErrorKind::MissingField, + "missing `local_dns_addr` or `remote_dns_addr` in configuration", + None, + ); + return Err(err); } #[cfg(feature = "local-tunnel")] - ProtocolType::Tunnel => { - if self.forward_addr.is_none() { - let err = Error::new(ErrorKind::MissingField, "missing `forward_addr` in configuration", None); - return Err(err); - } + ProtocolType::Tunnel if self.forward_addr.is_none() => { + let err = Error::new(ErrorKind::MissingField, "missing `forward_addr` in configuration", None); + return Err(err); } #[cfg(feature = "local-http")] - ProtocolType::Http => { - if !self.mode.enable_tcp() { - let err = Error::new(ErrorKind::Invalid, "TCP mode have to be enabled for http", None); - return Err(err); - } + ProtocolType::Http if !self.mode.enable_tcp() => { + let err = Error::new(ErrorKind::Invalid, "TCP mode have to be enabled for http", None); + return Err(err); } _ => {} diff --git a/crates/shadowsocks-service/src/local/dns/dns_resolver.rs b/crates/shadowsocks-service/src/local/dns/dns_resolver.rs index 20d12750..48e5a373 100644 --- a/crates/shadowsocks-service/src/local/dns/dns_resolver.rs +++ b/crates/shadowsocks-service/src/local/dns/dns_resolver.rs @@ -76,7 +76,7 @@ impl DnsResolver { { Ok(msg) => return Ok(msg), Err(err) => { - last_err = io::Error::new(io::ErrorKind::Other, err); + last_err = io::Error::other(err); } } } @@ -85,7 +85,7 @@ impl DnsResolver { match self.client_cache.lookup_local(ns, msg, &self.connect_opts, false).await { Ok(msg) => return Ok(msg), Err(err) => { - last_err = io::Error::new(io::ErrorKind::Other, err); + last_err = io::Error::other(err); } } } @@ -98,7 +98,7 @@ impl DnsResolver { .client_cache .lookup_unix_stream(path, msg) .await - .map_err(|err| io::Error::new(io::ErrorKind::Other, err)), + .map_err(io::Error::other), } } } diff --git a/crates/shadowsocks-service/src/local/dns/server.rs b/crates/shadowsocks-service/src/local/dns/server.rs index 11f66cda..eb932da6 100644 --- a/crates/shadowsocks-service/src/local/dns/server.rs +++ b/crates/shadowsocks-service/src/local/dns/server.rs @@ -321,7 +321,7 @@ impl DnsTcpServer { Ok(m) => m, Err(err) => { error!("dns tcp {} parse message failed, error: {}", peer_addr, err); - return Err(io::Error::new(io::ErrorKind::Other, err)); + return Err(io::Error::other(err)); } }; @@ -329,13 +329,13 @@ impl DnsTcpServer { Ok(m) => m, Err(err) => { error!("dns tcp {} lookup error: {}", peer_addr, err); - return Err(io::Error::new(io::ErrorKind::Other, err)); + return Err(io::Error::other(err)); } }; let mut buf = match respond_message.to_vec() { Ok(buf) => buf, - Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err)), + Err(err) => return Err(io::Error::other(err)), }; let length = buf.len(); buf.resize(length + 2, 0); @@ -485,7 +485,7 @@ impl DnsUdpServer { let buf = match respond_message.to_vec() { Ok(buf) => buf, - Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err)), + Err(err) => return Err(io::Error::other(err)), }; listener.send_to(&buf, peer_addr).await?; @@ -877,14 +877,14 @@ impl DnsClient { self.client_cache .lookup_remote(&self.context, server.server_config(), remote_addr, message, false) .await - .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) + .map_err(io::Error::other) } Mode::UdpOnly => { let server = self.balancer.best_udp_server(); self.client_cache .lookup_remote(&self.context, server.server_config(), remote_addr, message, true) .await - .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) + .map_err(io::Error::other) } Mode::TcpAndUdp => { // Query TCP & UDP simultaneously @@ -916,11 +916,11 @@ impl DnsClient { match future::select(tcp_fut, udp_fut).await { Either::Left((res, next)) => match res { Ok(o) => Ok(o), - Err(..) => next.await.map_err(|err| io::Error::new(io::ErrorKind::Other, err)), + Err(..) => next.await.map_err(io::Error::other), }, Either::Right((res, next)) => match res { Ok(o) => Ok(o), - Err(..) => next.await.map_err(|err| io::Error::new(io::ErrorKind::Other, err)), + Err(..) => next.await.map_err(io::Error::other), }, } } @@ -968,13 +968,9 @@ impl DnsClient { match future::select(udp_query, tcp_query).await { Either::Left((Ok(m), ..)) => Ok(m), - Either::Left((Err(..), next)) => { - next.await.map_err(|err| io::Error::new(io::ErrorKind::Other, err)) - } + Either::Left((Err(..), next)) => next.await.map_err(io::Error::other), Either::Right((Ok(m), ..)) => Ok(m), - Either::Right((Err(..), next)) => { - next.await.map_err(|err| io::Error::new(io::ErrorKind::Other, err)) - } + Either::Right((Err(..), next)) => next.await.map_err(io::Error::other), } } #[cfg(unix)] @@ -982,7 +978,7 @@ impl DnsClient { .client_cache .lookup_unix_stream(path, message) .await - .map_err(|err| io::Error::new(io::ErrorKind::Other, err)), + .map_err(io::Error::other), } } } diff --git a/crates/shadowsocks-service/src/server/tcprelay.rs b/crates/shadowsocks-service/src/server/tcprelay.rs index ee542530..fbb29fbe 100644 --- a/crates/shadowsocks-service/src/server/tcprelay.rs +++ b/crates/shadowsocks-service/src/server/tcprelay.rs @@ -40,6 +40,8 @@ impl<'a> TcpDialer for ServerTcpDialer<'a> { } /// Unified outbound stream: either direct or through the outbound proxy chain. +// Not boxed: one instance per connection, boxing would add an allocation on the hot path +#[allow(clippy::large_enum_variant)] enum RemoteStream { Direct(OutboundTcpStream), Proxied(OutboundProxyStream), diff --git a/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs b/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs index be7a04fe..2f8852e4 100644 --- a/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs +++ b/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs @@ -161,7 +161,7 @@ pub async fn create_resolver( Ok(resolver) => Ok(resolver), Err(err) => { error!("initialize DNS resolver with config failed, error: {}", err); - Err(io::Error::new(io::ErrorKind::Other, err)) + Err(io::Error::other(err)) } } } @@ -186,13 +186,13 @@ pub async fn create_resolver( Ok(resolver) => Ok(resolver), Err(err) => { error!("initialize DNS resolver with config failed, error: {}", err); - Err(io::Error::new(io::ErrorKind::Other, err)) + Err(io::Error::other(err)) } } } Err(err) => { error!("initialize DNS resolver with system-config failed, error: {}", err); - Err(io::Error::new(io::ErrorKind::Other, err)) + Err(io::Error::other(err)) } } }