Skip to content

Commit

Permalink
make clippy happier
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Apr 15, 2021
1 parent 1d3e6b7 commit bbf8fb3
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 112 deletions.
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/acl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Rules {

/// Check if there are no rules for domain names
fn is_host_empty(&self) -> bool {
self.rule.len() == 0
self.rule.is_empty()
}
}

Expand Down
25 changes: 9 additions & 16 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,11 +1332,10 @@ impl Config {
#[cfg(any(feature = "trust-dns", feature = "local-dns"))]
fn parse_dns_nameservers(nameservers: &str) -> Result<DnsConfig, Error> {
#[cfg(all(unix, feature = "local-dns"))]
if nameservers.starts_with("unix://") {
if let Some(nameservers) = nameservers.strip_prefix("unix://") {
// A special DNS server only for shadowsocks-android
// It serves like a TCP DNS server but using unix domain sockets

let nameservers = &nameservers[7..];
return Ok(DnsConfig::LocalDns(NameServerAddr::UnixSocketAddr(PathBuf::from(
nameservers,
))));
Expand Down Expand Up @@ -1581,20 +1580,14 @@ impl fmt::Display for Config {
ServerAddr::SocketAddr(ref sa) => sa.port(),
ServerAddr::DomainName(.., port) => port,
},
local_udp_address: match local.udp_addr {
None => None,
Some(ref udp_addr) => Some(match udp_addr {
ServerAddr::SocketAddr(ref sa) => sa.ip().to_string(),
ServerAddr::DomainName(ref dm, ..) => dm.to_string(),
}),
},
local_udp_port: match local.udp_addr {
None => None,
Some(ref udp_addr) => Some(match udp_addr {
ServerAddr::SocketAddr(ref sa) => sa.port(),
ServerAddr::DomainName(.., port) => *port,
}),
},
local_udp_address: local.udp_addr.as_ref().map(|udp_addr| match udp_addr {
ServerAddr::SocketAddr(sa) => sa.ip().to_string(),
ServerAddr::DomainName(dm, ..) => dm.to_string(),
}),
local_udp_port: local.udp_addr.as_ref().map(|udp_addr| match udp_addr {
ServerAddr::SocketAddr(sa) => sa.port(),
ServerAddr::DomainName(.., port) => *port,
}),
mode: Some(local.mode.to_string()),
protocol: match local.protocol {
ProtocolType::Socks => None,
Expand Down
6 changes: 6 additions & 0 deletions crates/shadowsocks-service/src/local/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub struct ServiceContext {
reverse_lookup_cache: Mutex<LruCache<IpAddr, bool>>,
}

impl Default for ServiceContext {
fn default() -> Self {
ServiceContext::new()
}
}

impl ServiceContext {
/// Create a new `ServiceContext`
pub fn new() -> ServiceContext {
Expand Down
140 changes: 71 additions & 69 deletions crates/shadowsocks-service/src/local/dns/dns_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,93 +126,95 @@ impl DnsResolve for DnsResolver {
msgv6.set_recursion_desired(true);
msgv6.add_query(queryv6);

let (res_v4, res_v6) = future::join(self.lookup(msgv4), self.lookup(msgv6)).await;

if res_v4.is_err() && res_v6.is_err() {
return if self.ipv6_first {
Err(res_v6.unwrap_err())
} else {
Err(res_v4.unwrap_err())
};
}
match future::join(self.lookup(msgv4), self.lookup(msgv6)).await {
(Err(res_v4), Err(res_v6)) => {
if self.ipv6_first {
Err(res_v6)
} else {
Err(res_v4)
}
}

let mut vaddr = Vec::new();

if self.ipv6_first {
match res_v6 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
(res_v4, res_v6) => {
let mut vaddr = Vec::new();

if self.ipv6_first {
match res_v6 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
}
}
}
}
Err(err) => {
debug!("failed to resolve AAAA records, error: {}", err);
}
}
}
Err(err) => {
debug!("failed to resolve AAAA records, error: {}", err);
}
}

match res_v4 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
match res_v4 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
}
}
}
}
Err(err) => {
debug!("failed to resolve A records, error: {}", err);
}
}
}
Err(err) => {
debug!("failed to resolve A records, error: {}", err);
}
}
} else {
match res_v4 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
} else {
match res_v4 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
}
}
}
}
Err(err) => {
debug!("failed to resolve A records, error: {}", err);
}
}
}
Err(err) => {
debug!("failed to resolve A records, error: {}", err);
}
}

match res_v6 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
match res_v6 {
Ok(res) => {
for record in res.answers() {
match *record.rdata() {
RData::A(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
RData::AAAA(addr) => vaddr.push(SocketAddr::new(addr.into(), port)),
ref rdata => {
trace!("skipped rdata {:?}", rdata);
}
}
}
}
Err(err) => {
debug!("failed to resolve AAAA records, error: {}", err);
}
}
}
Err(err) => {
debug!("failed to resolve AAAA records, error: {}", err);

if vaddr.is_empty() {
let err = io::Error::new(ErrorKind::InvalidData, "resolve empty");
return Err(err);
}
}
}

if vaddr.is_empty() {
let err = io::Error::new(ErrorKind::InvalidData, "resolve empty");
return Err(err);
Ok(vaddr)
}
}

Ok(vaddr)
}
}
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/dns/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl Dns {
Ok(m) => m,
Err(err) => {
error!("dns udp {} lookup failed, error: {}", peer_addr, err);
return Err(err.into());
return Err(err);
}
};

Expand Down
1 change: 1 addition & 0 deletions crates/shadowsocks-service/src/local/dns/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use trust_dns_resolver::proto::{
use crate::net::{FlowStat, MonProxySocket, MonProxyStream};

/// Collection of various DNS connections
#[allow(clippy::large_enum_variant)]
pub enum DnsClient {
TcpLocal {
stream: TcpStream,
Expand Down
1 change: 1 addition & 0 deletions crates/shadowsocks-service/src/local/http/http_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

use crate::local::net::AutoProxyClientStream;

#[allow(clippy::large_enum_variant)]
#[pin_project(project = ProxyHttpStreamProj)]
pub enum ProxyHttpStream {
Http(#[pin] AutoProxyClientStream),
Expand Down
6 changes: 6 additions & 0 deletions crates/shadowsocks-service/src/local/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pub struct Http {
proxy_client_cache: Arc<ProxyClientCache>,
}

impl Default for Http {
fn default() -> Self {
Http::new()
}
}

impl Http {
/// Create a new HTTP Local server
pub fn new() -> Http {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ pub struct ServerScore {
score: AtomicU32,
}

impl ServerScore {
/// Create a `ServerScore`
pub fn new() -> ServerScore {
impl Default for ServerScore {
fn default() -> Self {
ServerScore {
stat_data: Mutex::new(ServerStat::new()),
score: AtomicU32::new(0),
}
}
}

impl ServerScore {
/// Create a `ServerScore`
pub fn new() -> ServerScore {
ServerScore::default()
}

/// Get server's current statistic scores
pub fn score(&self) -> u32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ fn max_latency_stdev() -> f64 {
(diff1 + diff2).sqrt()
}

impl ServerStat {
pub fn new() -> ServerStat {
impl Default for ServerStat {
fn default() -> Self {
ServerStat {
rtt: MAX_SERVER_RTT,
fail_rate: 1.0,
Expand All @@ -56,6 +56,12 @@ impl ServerStat {
latency_mean: 0.0,
}
}
}

impl ServerStat {
pub fn new() -> ServerStat {
ServerStat::default()
}

fn score(&self) -> u32 {
// Normalize rtt
Expand Down Expand Up @@ -109,7 +115,7 @@ impl ServerStat {
self.fail_rate = cerr as f64 / self.latency_queue.len() as f64;

if !vlat.is_empty() {
vlat.sort();
vlat.sort_unstable();

// Find median of latency
let mid = vlat.len() / 2;
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub mod utils;
/// Starts a shadowsocks local server
pub async fn run(mut config: Config) -> io::Result<()> {
assert!(config.config_type == ConfigType::Local && !config.local.is_empty());
assert!(config.server.len() > 0);
assert!(!config.server.is_empty());

trace!("{:?}", config);

Expand Down
6 changes: 6 additions & 0 deletions crates/shadowsocks-service/src/local/redir/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub struct Redir {
udp_redir: RedirType,
}

impl Default for Redir {
fn default() -> Self {
Redir::new()
}
}

impl Redir {
/// Create a new transparent proxy server with default configuration
pub fn new() -> Redir {
Expand Down
6 changes: 6 additions & 0 deletions crates/shadowsocks-service/src/local/redir/sys/unix/bsd_pf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl PacketFilter {
let addr: *const libc::in_addr = &((*sockaddr).sin_addr) as *const _;
let port: libc::in_port_t = (*sockaddr).sin_port;

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(addr, &mut pnl.daddr.pfa.v4, mem::size_of_val(&pnl.daddr.pfa.v4));
pnl.set_dport(port);
}
Expand All @@ -177,6 +178,7 @@ impl PacketFilter {
let addr: *const libc::in6_addr = &((*sockaddr).sin6_addr) as *const _;
let port: libc::in_port_t = (*sockaddr).sin6_port;

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(addr, &mut pnl.daddr.pfa.v6, mem::size_of_val(&pnl.daddr.pfa.v6));
pnl.set_dport(port);
}
Expand All @@ -194,6 +196,7 @@ impl PacketFilter {
let addr: *const libc::in_addr = &((*sockaddr).sin_addr) as *const _;
let port: libc::in_port_t = (*sockaddr).sin_port;

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(addr, &mut pnl.saddr.pfa.v4, mem::size_of_val(&pnl.saddr.pfa.v4));
pnl.set_sport(port);
}
Expand All @@ -208,6 +211,7 @@ impl PacketFilter {
let addr: *const libc::in6_addr = &((*sockaddr).sin6_addr) as *const _;
let port: libc::in_port_t = (*sockaddr).sin6_port;

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(addr, &mut pnl.saddr.pfa.v6, mem::size_of_val(&pnl.saddr.pfa.v6));
pnl.set_sport(port);
}
Expand All @@ -229,6 +233,7 @@ impl PacketFilter {
let dst_addr: &mut libc::sockaddr_in = &mut *(dst_addr as *mut _);
dst_addr.sin_family = pnl.af;
dst_addr.sin_port = pnl.rdport();
#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(
&pnl.rdaddr.pfa.v4,
&mut dst_addr.sin_addr,
Expand All @@ -239,6 +244,7 @@ impl PacketFilter {
let dst_addr: &mut libc::sockaddr_in6 = &mut *(dst_addr as *mut _);
dst_addr.sin6_family = pnl.af;
dst_addr.sin6_port = pnl.rdport();
#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(
&pnl.rdaddr.pfa.v6,
&mut dst_addr.sin6_addr,
Expand Down
Loading

0 comments on commit bbf8fb3

Please sign in to comment.