Skip to content

Commit

Permalink
clippy --fix style & potential bugs
Browse files Browse the repository at this point in the history
- DNS /etc/resolv.conf auto reloader exit directly if file not exists
- socks5 PasswdAuthResponse::read_from didn't await
  • Loading branch information
zonyitoo committed Dec 21, 2022
1 parent 85c99b9 commit 857da6d
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 111 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 @@ -269,7 +269,7 @@ impl ParsingRules {
.size_limit(REGEX_SIZE_LIMIT)
.unicode(false)
.build()
.map_err(|err| Error::new(ErrorKind::Other, format!("{} regex error: {}", name, err)))
.map_err(|err| Error::new(ErrorKind::Other, format!("{name} regex error: {err}")))
}

fn into_rules(self) -> io::Result<Rules> {
Expand Down
18 changes: 9 additions & 9 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ impl Config {
let err = Error::new(
ErrorKind::Malformed,
"`protocol` invalid",
Some(format!("unrecognized protocol {}", p)),
Some(format!("unrecognized protocol {p}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1390,7 +1390,7 @@ impl Config {
let err = Error::new(
ErrorKind::Malformed,
"`protocol` invalid",
Some(format!("unrecognized protocol {}", p)),
Some(format!("unrecognized protocol {p}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1549,7 +1549,7 @@ impl Config {
let err = Error::new(
ErrorKind::Invalid,
"acl loading failed",
Some(format!("file {}, error: {}", acl_path, err)),
Some(format!("file {acl_path}, error: {err}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1587,7 +1587,7 @@ impl Config {
let err = Error::new(
ErrorKind::Invalid,
"unsupported method",
Some(format!("`{}` is not a supported method", m)),
Some(format!("`{m}` is not a supported method")),
);
return Err(err);
}
Expand All @@ -1603,7 +1603,7 @@ impl Config {
let err = Error::new(
ErrorKind::MissingField,
"`password` is required",
Some(format!("`password` is required for method {}", method)),
Some(format!("`password` is required for method {method}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1692,7 +1692,7 @@ impl Config {
let err = Error::new(
ErrorKind::MissingField,
"`password` is required",
Some(format!("`password` is required for method {}", method)),
Some(format!("`password` is required for method {method}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1794,7 +1794,7 @@ impl Config {
let err = Error::new(
ErrorKind::Invalid,
"acl loading failed",
Some(format!("file {}, error: {}", acl_path, err)),
Some(format!("file {acl_path}, error: {err}")),
);
return Err(err);
}
Expand Down Expand Up @@ -1849,7 +1849,7 @@ impl Config {
let err = Error::new(
ErrorKind::Invalid,
"unsupported method",
Some(format!("`{}` is not a supported method", m)),
Some(format!("`{m}` is not a supported method")),
);
return Err(err);
}
Expand Down Expand Up @@ -1954,7 +1954,7 @@ impl Config {
let err = Error::new(
ErrorKind::Invalid,
"acl loading failed",
Some(format!("file {}, error: {}", acl_path, err)),
Some(format!("file {acl_path}, error: {err}")),
);
return Err(err);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/http/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl HttpDispatcher {
method, self.client_addr, host, err
);

let mut resp = Response::new(Body::from(format!("relay failed to {}", host)));
let mut resp = Response::new(Body::from(format!("relay failed to {host}")));
*resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
return Ok(resp);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/shadowsocks-service/src/local/http/http_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ProxyHttpStream {
let cx = match TlsConnector::builder().request_alpns(&["h2", "http/1.1"]).build() {
Ok(c) => c,
Err(err) => {
return Err(io::Error::new(ErrorKind::Other, format!("tls build: {}", err)));
return Err(io::Error::new(ErrorKind::Other, format!("tls build: {err}")));
}
};
let cx = tokio_native_tls::TlsConnector::from(cx);
Expand All @@ -45,15 +45,15 @@ impl ProxyHttpStream {
Ok(Some(alpn)) => alpn == b"h2",
Ok(None) => false,
Err(err) => {
let ierr = io::Error::new(ErrorKind::Other, format!("tls alpn negotiate: {}", err));
let ierr = io::Error::new(ErrorKind::Other, format!("tls alpn negotiate: {err}"));
return Err(ierr);
}
};

Ok(ProxyHttpStream::Https(s, negotiated_h2))
}
Err(err) => {
let ierr = io::Error::new(ErrorKind::Other, format!("tls connect: {}", err));
let ierr = io::Error::new(ErrorKind::Other, format!("tls connect: {err}"));
Err(ierr)
}
}
Expand Down
44 changes: 20 additions & 24 deletions crates/shadowsocks-service/src/local/loadbalancing/ping_balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,17 @@ impl PingBalancerContext {
"chose best TCP server {}",
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else if best_idx != old_best_idx {
info!(
"switched best TCP server from {} to {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config()),
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else {
if best_idx != old_best_idx {
info!(
"switched best TCP server from {} to {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config()),
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else {
debug!(
"kept best TCP server {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config())
);
}
debug!(
"kept best TCP server {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config())
);
}
}

Expand All @@ -492,19 +490,17 @@ impl PingBalancerContext {
"chose best UDP server {}",
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else if best_idx != old_best_idx {
info!(
"switched best UDP server from {} to {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config()),
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else {
if best_idx != old_best_idx {
info!(
"switched best UDP server from {} to {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config()),
ServerConfigFormatter::new(servers[best_idx].server_config())
);
} else {
debug!(
"kept best UDP server {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config())
);
}
debug!(
"kept best UDP server {}",
ServerConfigFormatter::new(servers[old_best_idx].server_config())
);
}
}
}
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 @@ -443,7 +443,7 @@ async fn flow_report_task(stat_addr: LocalFlowStatAddress, flow_stat: Arc<FlowSt
let tx = flow_stat.tx();
let rx = flow_stat.rx();

let buf: [u64; 2] = [tx as u64, rx as u64];
let buf: [u64; 2] = [tx, rx];
let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *const _, 16) };

match stat_addr {
Expand Down
18 changes: 8 additions & 10 deletions crates/shadowsocks-service/src/local/net/udp/association.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,14 @@ where
err
);
}
} else {
if let Err(err) = self.dispatch_received_proxied_packet(target_addr, data).await {
error!(
"udp relay {} -> {} (proxied) with {} bytes, error: {}",
self.peer_addr,
target_addr,
data.len(),
err
);
}
} else if let Err(err) = self.dispatch_received_proxied_packet(target_addr, data).await {
error!(
"udp relay {} -> {} (proxied) with {} bytes, error: {}",
self.peer_addr,
target_addr,
data.len(),
err
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,6 @@ pub static PF: Lazy<PacketFilter> = Lazy::new(|| match PacketFilter::open() {
panic!("open /dev/pf permission denied, consider restart with root user");
}
Err(err) => {
panic!("open /dev/pf {}", err);
panic!("open /dev/pf {err}");
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Socks5TcpHandler {

return Err(Error::new(
ErrorKind::Other,
format!("Username/Password Authentication Initial request failed: {}", err),
format!("Username/Password Authentication Initial request failed: {err}"),
));
}
};
Expand Down Expand Up @@ -174,8 +174,7 @@ impl Socks5TcpHandler {
Err(Error::new(
ErrorKind::Other,
format!(
"Username/Password Authentication failed, user: {}, password: {}",
user_name, password
"Username/Password Authentication failed, user: {user_name}, password: {password}"
),
))
}
Expand Down
8 changes: 4 additions & 4 deletions crates/shadowsocks-service/src/local/tun/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl TcpTun {
let mut sockets_to_remove = Vec::new();

for (socket_handle, control) in sockets.iter() {
let socket_handle = socket_handle.clone();
let socket_handle = *socket_handle;
let socket = iface.get_socket::<TcpSocket>(socket_handle);
let mut control = control.lock();

Expand All @@ -308,7 +308,7 @@ impl TcpTun {

if !socket.is_open() || socket.state() == TcpState::Closed {
sockets_to_remove.push(socket_handle);
close_socket_control(&mut *control);
close_socket_control(&mut control);
continue;
}

Expand All @@ -335,7 +335,7 @@ impl TcpTun {
Err(err) => {
error!("socket recv error: {}", err);
sockets_to_remove.push(socket_handle);
close_socket_control(&mut *control);
close_socket_control(&mut control);
break;
}
}
Expand All @@ -362,7 +362,7 @@ impl TcpTun {
Err(err) => {
error!("socket send error: {}", err);
sockets_to_remove.push(socket_handle);
close_socket_control(&mut *control);
close_socket_control(&mut control);
break;
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/shadowsocks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl ServerUserManager {

/// Iterate users
pub fn users_iter(&self) -> impl Iterator<Item = &ServerUser> {
self.users.iter().map(|(_, v)| v.as_ref())
self.users.values().map(|v| v.as_ref())
}
}

Expand Down Expand Up @@ -316,7 +316,7 @@ fn make_derived_key(method: CipherKind, password: &str, enc_key: &mut [u8]) {
enc_key.copy_from_slice(&v);
}
Err(err) => {
panic!("{} password {} is not base64 encoded, error: {}", method, password, err);
panic!("{method} password {password} is not base64 encoded, error: {err}");
}
}
} else {
Expand Down Expand Up @@ -368,7 +368,7 @@ where
identity_keys.push(Bytes::from(v));
}
Err(err) => {
panic!("iPSK {} is not base64 encoded, error: {}", ipsk, err);
panic!("iPSK {ipsk} is not base64 encoded, error: {err}");
}
}
}
Expand Down Expand Up @@ -564,7 +564,7 @@ impl ServerConfig {
/// ```
pub fn to_qrcode_url(&self) -> String {
let param = format!("{}:{}@{}", self.method(), self.password(), self.addr());
format!("ss://{}", encode_config(&param, URL_SAFE_NO_PAD))
format!("ss://{}", encode_config(param, URL_SAFE_NO_PAD))
}

/// Get [SIP002](https://github.com/shadowsocks/shadowsocks-org/issues/27) URL
Expand Down Expand Up @@ -717,7 +717,7 @@ impl ServerConfig {
};

let port = parsed.port().unwrap_or(8388);
let addr = format!("{}:{}", host, port);
let addr = format!("{host}:{port}");

let addr = match addr.parse::<ServerAddr>() {
Ok(a) => a,
Expand Down Expand Up @@ -891,8 +891,8 @@ impl FromStr for ServerAddr {
impl Display for ServerAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ServerAddr::SocketAddr(ref a) => write!(f, "{}", a),
ServerAddr::DomainName(ref d, port) => write!(f, "{}:{}", d, port),
ServerAddr::SocketAddr(ref a) => write!(f, "{a}"),
ServerAddr::DomainName(ref d, port) => write!(f, "{d}:{port}"),
}
}
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ impl Display for ManagerAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ManagerAddr::SocketAddr(ref saddr) => fmt::Display::fmt(saddr, f),
ManagerAddr::DomainName(ref dname, port) => write!(f, "{}:{}", dname, port),
ManagerAddr::DomainName(ref dname, port) => write!(f, "{dname}:{port}"),
#[cfg(unix)]
ManagerAddr::UnixSocketAddr(ref path) => fmt::Display::fmt(&path.display(), f),
}
Expand Down
Loading

0 comments on commit 857da6d

Please sign in to comment.