Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/acl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ impl ParsingRules {
if let Some(tree_rule) = caps.get(1) {
if let Ok(tree_rule) = str::from_utf8(tree_rule.as_bytes()) {
let tree_rule = tree_rule.replace("\\.", ".");
if let Ok(..) = self.add_tree_rule_inner(&tree_rule) {
if self.add_tree_rule_inner(&tree_rule).is_ok() {
trace!("REGEX-RULE {} => TREE-RULE {}", rule, tree_rule);
return;
}
}
} else if let Some(set_rule) = caps.get(2) {
if let Ok(set_rule) = str::from_utf8(set_rule.as_bytes()) {
let set_rule = set_rule.replace("\\.", ".");
if let Ok(..) = self.add_set_rule_inner(&set_rule) {
if self.add_set_rule_inner(&set_rule).is_ok() {
trace!("REGEX-RULE {} => SET-RULE {}", rule, set_rule);
return;
}
Expand Down
27 changes: 6 additions & 21 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,22 +642,17 @@ impl FromStr for ManagerServerHost {
}

/// Mode of Manager's server
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum ManagerServerMode {
/// Run shadowsocks server in the same process of manager
#[default]
Builtin,

/// Run shadowsocks server in standalone (process) mode
#[cfg(unix)]
Standalone,
}

impl Default for ManagerServerMode {
fn default() -> ManagerServerMode {
ManagerServerMode::Builtin
}
}

/// Parsing ManagerServerMode error
#[derive(Debug, Clone, Copy)]
pub struct ManagerServerModeError;
Expand Down Expand Up @@ -741,8 +736,9 @@ impl ManagerConfig {
}

/// Protocol of local server
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub enum ProtocolType {
#[default]
Socks,
#[cfg(feature = "local-http")]
Http,
Expand All @@ -756,12 +752,6 @@ pub enum ProtocolType {
Tun,
}

impl Default for ProtocolType {
fn default() -> ProtocolType {
ProtocolType::Socks
}
}

impl ProtocolType {
/// As string representation
pub fn as_str(&self) -> &'static str {
Expand Down Expand Up @@ -1016,21 +1006,16 @@ impl LocalConfig {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub enum DnsConfig {
#[default]
System,
#[cfg(feature = "trust-dns")]
TrustDns(ResolverConfig),
#[cfg(feature = "local-dns")]
LocalDns(NameServerAddr),
}

impl Default for DnsConfig {
fn default() -> DnsConfig {
DnsConfig::System
}
}

/// Security Config
#[derive(Clone, Debug, Default)]
pub struct SecurityConfig {
Expand Down
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/local/http/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn clear_hop_headers(headers: &mut HeaderMap<HeaderValue>) {
extra_headers.extend(get_extra_headers(headers.get_all("Proxy-Connection")));

for header in extra_headers {
while let Some(..) = headers.remove(&header) {}
while headers.remove(&header).is_some() {}
}

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection
Expand All @@ -313,7 +313,7 @@ fn clear_hop_headers(headers: &mut HeaderMap<HeaderValue>) {
];

for header in &HOP_BY_HOP_HEADERS {
while let Some(..) = headers.remove(*header) {}
while headers.remove(*header).is_some() {}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/http/http_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl ProxyHttpStream {
Err(_) => {
return Err(io::Error::new(
ErrorKind::InvalidInput,
format!("invalid dnsname \"{}\"", domain),
format!("invalid dnsname \"{domain}\""),
));
}
};
Expand Down
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/local/net/udp/association.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ where
}

fn try_send(&self, data: (Address, Bytes)) -> io::Result<()> {
if let Err(..) = self.sender.try_send(data) {
if self.sender.try_send(data).is_err() {
let err = io::Error::new(ErrorKind::Other, "udp relay channel full");
return Err(err);
}
Expand Down Expand Up @@ -376,7 +376,7 @@ where

_ = keepalive_interval.tick() => {
if self.keepalive_flag {
if let Err(..) = self.keepalive_tx.try_send(self.peer_addr) {
if self.keepalive_tx.try_send(self.peer_addr).is_err() {
debug!("udp relay {} keep-alive failed, channel full or closed", self.peer_addr);
} else {
self.keepalive_flag = false;
Expand Down
10 changes: 5 additions & 5 deletions crates/shadowsocks-service/src/local/socks/socks4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl fmt::Display for ResultCode {
ResultCode::RequestRejectedDifferentUserId => {
f.write_str("request rejected because the client program and identd report different user-ids")
}
ResultCode::Other(code) => write!(f, "other result code {}", code),
ResultCode::Other(code) => write!(f, "other result code {code}"),
}
}
}
Expand All @@ -126,8 +126,8 @@ impl fmt::Debug for Address {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Address::SocketAddress(ref addr) => write!(f, "{}", addr),
Address::DomainNameAddress(ref addr, ref port) => write!(f, "{}:{}", addr, port),
Address::SocketAddress(ref addr) => write!(f, "{addr}"),
Address::DomainNameAddress(ref addr, ref port) => write!(f, "{addr}:{port}"),
}
}
}
Expand All @@ -136,8 +136,8 @@ impl fmt::Display for Address {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Address::SocketAddress(ref addr) => write!(f, "{}", addr),
Address::DomainNameAddress(ref addr, ref port) => write!(f, "{}:{}", addr, port),
Address::SocketAddress(ref addr) => write!(f, "{addr}"),
Address::DomainNameAddress(ref addr, ref port) => write!(f, "{addr}:{port}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tunnel/tcprelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ async fn handle_tcp_client(
);

let mut remote = AutoProxyClientStream::connect_proxied(context, &server, forward_addr).await?;
establish_tcp_tunnel(svr_cfg, &mut stream, &mut remote, peer_addr, &forward_addr).await
establish_tcp_tunnel(svr_cfg, &mut stream, &mut remote, peer_addr, forward_addr).await
}
12 changes: 6 additions & 6 deletions crates/shadowsocks-service/src/manager/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ impl Manager {

#[cfg(unix)]
fn server_pid_path(&self, port: u16) -> PathBuf {
let pid_file_name = format!("shadowsocks-server-{}.pid", port);
let pid_file_name = format!("shadowsocks-server-{port}.pid");
let mut pid_path = self.svr_cfg.server_working_directory.clone();
pid_path.push(&pid_file_name);
pid_path
}

#[cfg(unix)]
fn server_config_path(&self, port: u16) -> PathBuf {
let config_file_name = format!("shadowsocks-server-{}.json", port);
let config_file_name = format!("shadowsocks-server-{port}.json");
let mut config_file_path = self.svr_cfg.server_working_directory.clone();
config_file_path.push(&config_file_name);
config_file_path
Expand All @@ -355,7 +355,7 @@ impl Manager {
if pid_path.exists() {
if let Ok(mut pid_file) = File::open(&pid_path) {
let mut pid_content = String::new();
if let Ok(..) = pid_file.read_to_string(&mut pid_content) {
if pid_file.read_to_string(&mut pid_content).is_ok() {
let pid_content = pid_content.trim();

match pid_content.parse::<libc::pid_t>() {
Expand Down Expand Up @@ -415,7 +415,7 @@ impl Manager {

trace!("created standalone server with config {:?}", config);

let config_file_content = format!("{}", config);
let config_file_content = format!("{config}");

match OpenOptions::new().write(true).create(true).open(&config_file_path) {
Err(err) => {
Expand Down Expand Up @@ -484,7 +484,7 @@ impl Manager {
Err(..) => {
error!("unrecognized method \"{}\", req: {:?}", m, req);

let err = format!("unrecognized method \"{}\"", m);
let err = format!("unrecognized method \"{m}\"");
return Ok(AddResponse(err));
}
},
Expand Down Expand Up @@ -523,7 +523,7 @@ impl Manager {
Err(..) => {
error!("unrecognized mode \"{}\", req: {:?}", mode, req);

let err = format!("unrecognized mode \"{}\"", mode);
let err = format!("unrecognized mode \"{mode}\"");
return Ok(AddResponse(err));
}
},
Expand Down
8 changes: 4 additions & 4 deletions crates/shadowsocks-service/src/server/udprelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ impl UdpServer {
None => continue,
};

if let Err(..) = otx
if (otx
.send((peer_addr, target_addr, control, Bytes::copy_from_slice(&buffer[..n])))
.await
.await).is_err()
{
// If Result is error, the channel receiver is closed. We should exit the task.
break;
Expand Down Expand Up @@ -423,7 +423,7 @@ impl UdpAssociation {
}

fn try_send(&self, data: UdpAssociationSendMessage) -> io::Result<()> {
if let Err(..) = self.sender.try_send(data) {
if self.sender.try_send(data).is_err() {
let err = io::Error::new(ErrorKind::Other, "udp relay channel full");
return Err(err);
}
Expand Down Expand Up @@ -566,7 +566,7 @@ impl UdpAssociationContext {
Some(..) => unreachable!("client_session_id is not None but aead-cipher-2022 is not enabled"),
};

if let Err(..) = self.keepalive_tx.try_send(nat_key) {
if self.keepalive_tx.try_send(nat_key).is_err() {
debug!("udp relay {:?} keep-alive failed, channel full or closed", nat_key);
} else {
self.keepalive_flag = false;
Expand Down
9 changes: 2 additions & 7 deletions crates/shadowsocks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,13 +1090,14 @@ impl From<PathBuf> for ManagerAddr {
}

/// Policy for handling replay attack requests
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum ReplayAttackPolicy {
/// Default strategy based on protocol
///
/// SIP022 (AEAD-2022): Reject
/// SIP004 (AEAD): Ignore
/// Stream: Ignore
#[default]
Default,
/// Ignore it completely
Ignore,
Expand All @@ -1106,12 +1107,6 @@ pub enum ReplayAttackPolicy {
Reject,
}

impl Default for ReplayAttackPolicy {
fn default() -> ReplayAttackPolicy {
ReplayAttackPolicy::Default
}
}

impl Display for ReplayAttackPolicy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
6 changes: 2 additions & 4 deletions crates/shadowsocks/src/net/sys/unix/bsd/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ impl TcpStream {

stream.ready(Interest::WRITABLE).await?;

if let Err(err) = stream.take_error() {
return Err(err);
}
stream.take_error()?;

stream
} else {
Expand Down Expand Up @@ -338,7 +336,7 @@ pub async fn bind_outbound_udp_socket(bind_addr: &SocketAddr, config: &ConnectOp
UdpSocket::bind(bind_addr).await?
} else {
let socket = Socket::new(Domain::for_address(*bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;
socket_bind_dual_stack(&socket, bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub async fn bind_outbound_udp_socket(bind_addr: &SocketAddr, opts: &ConnectOpts
UdpSocket::bind(bind_addr).await?
} else {
let socket = Socket::new(Domain::for_address(*bind_addr), Type::DGRAM, Some(Protocol::UDP))?;
socket_bind_dual_stack(&socket, &bind_addr, false)?;
socket_bind_dual_stack(&socket, bind_addr, false)?;

// UdpSocket::from_std requires socket to be non-blocked
socket.set_nonblocking(true)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl Drop for Plugin {
}

if !terminated {
if let Ok(..) = self.process.start_kill() {
if self.process.start_kill().is_ok() {
debug!("killed plugin process {:?}", self.process.id());
}
}
Expand Down
17 changes: 3 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
str::FromStr,
};

use cfg_if::cfg_if;
use clap::ArgMatches;
use directories::ProjectDirs;
use serde::Deserialize;
Expand Down Expand Up @@ -221,27 +220,17 @@ pub struct LogFormatConfig {
}

/// Runtime mode (Tokio)
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub enum RuntimeMode {
/// Single-Thread Runtime
#[cfg_attr(not(feature = "multi-threaded"), default)]
SingleThread,
/// Multi-Thread Runtime
#[cfg(feature = "multi-threaded")]
#[cfg_attr(feature = "multi-threaded", default)]
MultiThread,
}

impl Default for RuntimeMode {
fn default() -> RuntimeMode {
cfg_if! {
if #[cfg(feature = "multi-threaded")] {
RuntimeMode::MultiThread
} else {
RuntimeMode::SingleThread
}
}
}
}

/// Parse `RuntimeMode` from string error
#[derive(Debug)]
pub struct RuntimeModeError;
Expand Down