Skip to content

Commit

Permalink
make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 18, 2023
1 parent e2785a4 commit a359a84
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 36 deletions.
29 changes: 18 additions & 11 deletions crates/shadowsocks-service/src/dns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use shadowsocks::{dns_resolver::DnsResolver, net::ConnectOpts};
use crate::config::DnsConfig;

#[allow(unused_variables, dead_code)]
pub async fn build_dns_resolver(dns: DnsConfig, ipv6_first: bool, dns_cache_size: Option<usize>, connect_opts: &ConnectOpts) -> Option<DnsResolver> {
pub async fn build_dns_resolver(
dns: DnsConfig,
ipv6_first: bool,
dns_cache_size: Option<usize>,
connect_opts: &ConnectOpts,
) -> Option<DnsResolver> {
match dns {
DnsConfig::System => {
#[cfg(feature = "trust-dns")]
Expand Down Expand Up @@ -41,18 +46,20 @@ pub async fn build_dns_resolver(dns: DnsConfig, ipv6_first: bool, dns_cache_size
None
}
#[cfg(feature = "trust-dns")]
DnsConfig::TrustDns(dns) => match DnsResolver::trust_dns_resolver(dns, dns_cache_size, connect_opts.clone()).await {
Ok(r) => Some(r),
Err(err) => {
use log::warn;
DnsConfig::TrustDns(dns) => {
match DnsResolver::trust_dns_resolver(dns, dns_cache_size, connect_opts.clone()).await {
Ok(r) => Some(r),
Err(err) => {
use log::warn;

warn!(
"initialize trust-dns DNS resolver failed, fallback to default system resolver, error: {}",
err
);
None
warn!(
"initialize trust-dns DNS resolver failed, fallback to default system resolver, error: {}",
err
);
None
}
}
},
}
#[cfg(feature = "local-dns")]
DnsConfig::LocalDns(ns) => {
use crate::local::dns::dns_resolver::DnsResolver as LocalDnsResolver;
Expand Down
9 changes: 8 additions & 1 deletion crates/shadowsocks-service/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ impl Server {
accept_opts.tcp.mptcp = config.mptcp;
context.set_accept_opts(accept_opts);

if let Some(resolver) = build_dns_resolver(config.dns, config.ipv6_first, config.dns_cache_size, context.connect_opts_ref()).await {
if let Some(resolver) = build_dns_resolver(
config.dns,
config.ipv6_first,
config.dns_cache_size,
context.connect_opts_ref(),
)
.await
{
context.set_dns_resolver(Arc::new(resolver));
}

Expand Down
1 change: 1 addition & 0 deletions crates/shadowsocks-service/src/local/socks/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use self::socks5::Socks5UdpServer;

use super::config::Socks5AuthConfig;

#[allow(clippy::module_inception)]
mod server;
#[cfg(feature = "local-socks4")]
mod socks4;
Expand Down
5 changes: 3 additions & 2 deletions crates/shadowsocks-service/src/local/tun/virt_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct VirtTunDevice {
}

impl VirtTunDevice {
#[allow(clippy::type_complexity)]
pub fn new(
capabilities: DeviceCapabilities,
) -> (
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Device for VirtTunDevice {
type RxToken<'a> = VirtRxToken<'a>;
type TxToken<'a> = VirtTxToken<'a>;

fn receive<'a>(&'a mut self, _timestamp: Instant) -> Option<(Self::RxToken<'a>, Self::TxToken<'a>)> {
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
if let Ok(buffer) = self.in_buf.try_recv() {
let rx = Self::RxToken {
buffer,
Expand All @@ -70,7 +71,7 @@ impl Device for VirtTunDevice {
None
}

fn transmit<'a>(&'a mut self, _timestamp: Instant) -> Option<Self::TxToken<'a>> {
fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
return Some(VirtTxToken(self));
}

Expand Down
4 changes: 3 additions & 1 deletion crates/shadowsocks-service/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ pub async fn run(config: Config) -> io::Result<()> {
accept_opts.tcp.keepalive = config.keep_alive.or(Some(SERVER_DEFAULT_KEEPALIVE_TIMEOUT));
accept_opts.tcp.mptcp = config.mptcp;

if let Some(resolver) = build_dns_resolver(config.dns, config.ipv6_first, config.dns_cache_size, &connect_opts).await {
if let Some(resolver) =
build_dns_resolver(config.dns, config.ipv6_first, config.dns_cache_size, &connect_opts).await
{
manager_builder.set_dns_resolver(Arc::new(resolver));
}
manager_builder.set_ipv6_first(config.ipv6_first);
Expand Down
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/manager/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ impl Manager {

let server_config_path = self.server_config_path(port);

let _ = fs::remove_file(&pid_path);
let _ = fs::remove_file(&server_config_path);
let _ = fs::remove_file(pid_path);
let _ = fs::remove_file(server_config_path);
}

#[cfg(unix)]
Expand Down
3 changes: 2 additions & 1 deletion crates/shadowsocks-service/src/server/udprelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ impl UdpServer {

if (otx
.send((peer_addr, target_addr, control, Bytes::copy_from_slice(&buffer[..n])))
.await).is_err()
.await)
.is_err()
{
// If Result is error, the channel receiver is closed. We should exit the task.
break;
Expand Down
15 changes: 12 additions & 3 deletions crates/shadowsocks/src/dns_resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ impl DnsResolver {
///
/// On *nix system, it will try to read configurations from `/etc/resolv.conf`.
#[cfg(feature = "trust-dns")]
pub async fn trust_dns_system_resolver(dns_cache_size: Option<usize>, connect_opts: ConnectOpts) -> io::Result<DnsResolver> {
pub async fn trust_dns_system_resolver(
dns_cache_size: Option<usize>,
connect_opts: ConnectOpts,
) -> io::Result<DnsResolver> {
use super::trust_dns_resolver::create_resolver;

let resolver = create_resolver(None, dns_cache_size, connect_opts.clone()).await?;
Expand Down Expand Up @@ -258,9 +261,15 @@ impl DnsResolver {

/// Use trust-dns DNS resolver (with DNS cache)
#[cfg(feature = "trust-dns")]
pub async fn trust_dns_resolver(dns: ResolverConfig, dns_cache_size: Option<usize>, connect_opts: ConnectOpts) -> io::Result<DnsResolver> {
pub async fn trust_dns_resolver(
dns: ResolverConfig,
dns_cache_size: Option<usize>,
connect_opts: ConnectOpts,
) -> io::Result<DnsResolver> {
use super::trust_dns_resolver::create_resolver;
Ok(DnsResolver::TrustDns(create_resolver(Some(dns), dns_cache_size, connect_opts).await?))
Ok(DnsResolver::TrustDns(
create_resolver(Some(dns), dns_cache_size, connect_opts).await?,
))
}

/// Custom DNS resolver
Expand Down
10 changes: 8 additions & 2 deletions crates/shadowsocks/src/dns_resolver/trust_dns_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ pub type ShadowDnsConnectionProvider = GenericConnector<ShadowDnsRuntimeProvider
pub type DnsResolver = AsyncResolver<ShadowDnsConnectionProvider>;

/// Create a `trust-dns` asynchronous DNS resolver
pub async fn create_resolver(dns: Option<ResolverConfig>, dns_cache_size: Option<usize>, connect_opts: ConnectOpts) -> ResolveResult<DnsResolver> {
pub async fn create_resolver(
dns: Option<ResolverConfig>,
dns_cache_size: Option<usize>,
connect_opts: ConnectOpts,
) -> ResolveResult<DnsResolver> {
// Customized dns resolution
match dns {
Some(conf) => {
Expand All @@ -108,7 +112,9 @@ pub async fn create_resolver(dns: Option<ResolverConfig>, dns_cache_size: Option
// Since we want to use Happy Eyeballs to connect to both IPv4 and IPv6 addresses, we need both A and AAAA records.
resolver_opts.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;

if let Some(size) = dns_cache_size { resolver_opts.cache_size = size }
if let Some(size) = dns_cache_size {
resolver_opts.cache_size = size
}

trace!(
"initializing DNS resolver with config {:?} opts {:?}",
Expand Down
7 changes: 5 additions & 2 deletions crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io, mem,
io,
mem,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
os::unix::io::{AsRawFd, RawFd},
pin::Pin,
Expand All @@ -20,7 +21,9 @@ use tokio_tfo::TfoStream;
use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
udp::{BatchRecvMessage, BatchSendMessage},
AcceptOpts, AddrFamily, ConnectOpts,
AcceptOpts,
AddrFamily,
ConnectOpts,
};

/// A `TcpStream` that supports TFO (TCP Fast Open)
Expand Down
4 changes: 3 additions & 1 deletion crates/shadowsocks/src/net/sys/unix/bsd/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use tokio_tfo::TfoStream;
use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
udp::{BatchRecvMessage, BatchSendMessage},
AcceptOpts, AddrFamily, ConnectOpts,
AcceptOpts,
AddrFamily,
ConnectOpts,
};

/// A `TcpStream` that supports TFO (TCP Fast Open)
Expand Down
7 changes: 5 additions & 2 deletions crates/shadowsocks/src/net/sys/unix/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io, mem,
io,
mem,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
pin::Pin,
Expand All @@ -21,7 +22,9 @@ use tokio_tfo::TfoStream;
use crate::net::{
sys::{set_common_sockopt_after_connect, set_common_sockopt_for_connect, socket_bind_dual_stack},
udp::{BatchRecvMessage, BatchSendMessage},
AcceptOpts, AddrFamily, ConnectOpts,
AcceptOpts,
AddrFamily,
ConnectOpts,
};

/// A `TcpStream` that supports TFO (TCP Fast Open)
Expand Down
6 changes: 2 additions & 4 deletions crates/shadowsocks/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,8 @@ impl Drop for Plugin {
}
}

if !terminated {
if self.process.start_kill().is_ok() {
debug!("killed plugin process {:?}", self.process.id());
}
if !terminated && self.process.start_kill().is_ok() {
debug!("killed plugin process {:?}", self.process.id());
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/shadowsocks/src/relay/udprelay/proxy_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl ProxySocket {
/// This function will use `recv_buf` to store intermediate data, so it has to be big enough to store the whole shadowsocks' packet
///
/// It is recommended to allocate a buffer to have at least 65536 bytes.
#[allow(clippy::type_complexity)]
pub async fn recv_from(&self, recv_buf: &mut [u8]) -> ProxySocketResult<(usize, SocketAddr, Address, usize)> {
self.recv_from_with_ctrl(recv_buf)
.await
Expand All @@ -469,6 +470,7 @@ impl ProxySocket {
/// This function will use `recv_buf` to store intermediate data, so it has to be big enough to store the whole shadowsocks' packet
///
/// It is recommended to allocate a buffer to have at least 65536 bytes.
#[allow(clippy::type_complexity)]
pub async fn recv_from_with_ctrl(
&self,
recv_buf: &mut [u8],
Expand Down Expand Up @@ -502,6 +504,7 @@ impl ProxySocket {

/// poll family functions.
/// the recv_timeout is ignored.
#[allow(clippy::type_complexity)]
pub fn poll_recv(
&self,
cx: &mut Context<'_>,
Expand All @@ -512,6 +515,7 @@ impl ProxySocket {
}

/// poll family functions
#[allow(clippy::type_complexity)]
pub fn poll_recv_with_ctrl(
&self,
cx: &mut Context<'_>,
Expand All @@ -527,6 +531,8 @@ impl ProxySocket {
}
}

/// poll family functions
#[allow(clippy::type_complexity)]
pub fn poll_recv_from(
&self,
cx: &mut Context<'_>,
Expand All @@ -536,6 +542,8 @@ impl ProxySocket {
.map(|r| r.map(|(n, sa, a, rn, _)| (n, sa, a, rn)))
}

/// poll family functions
#[allow(clippy::type_complexity)]
pub fn poll_recv_from_with_ctrl(
&self,
cx: &mut Context<'_>,
Expand All @@ -550,6 +558,7 @@ impl ProxySocket {
}
}

/// poll family functions
pub fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<ProxySocketResult<()>> {
self.socket.poll_recv_ready(cx).map_err(|x| x.into())
}
Expand Down
10 changes: 8 additions & 2 deletions src/service/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ use shadowsocks_service::shadowsocks::relay::socks5::Address;
use shadowsocks_service::{
acl::AccessControl,
config::{
read_variable_field_value, Config, ConfigType, LocalConfig, LocalInstanceConfig, ProtocolType,
read_variable_field_value,
Config,
ConfigType,
LocalConfig,
LocalInstanceConfig,
ProtocolType,
ServerInstanceConfig,
},
local::{loadbalancing::PingBalancer, Server},
Expand All @@ -29,7 +34,8 @@ use shadowsocks_service::{
use crate::logging;
use crate::{
config::{Config as ServiceConfig, RuntimeMode},
monitor, vparser,
monitor,
vparser,
};

#[cfg(feature = "local-dns")]
Expand Down
3 changes: 2 additions & 1 deletion src/service/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use shadowsocks_service::{
use crate::logging;
use crate::{
config::{Config as ServiceConfig, RuntimeMode},
monitor, vparser,
monitor,
vparser,
};

/// Defines command line options
Expand Down
3 changes: 2 additions & 1 deletion src/service/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use shadowsocks_service::{
use crate::logging;
use crate::{
config::{Config as ServiceConfig, RuntimeMode},
monitor, vparser,
monitor,
vparser,
};

/// Defines command line options
Expand Down

0 comments on commit a359a84

Please sign in to comment.