Skip to content

Commit

Permalink
chore: make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 1, 2024
1 parent 4006cde commit 609de80
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 59 deletions.
2 changes: 1 addition & 1 deletion bin/ssurl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ fn main() -> ExitCode {
}

println!("Use -h for more detail");
return ExitCode::FAILURE;
ExitCode::FAILURE
}
3 changes: 2 additions & 1 deletion crates/shadowsocks-service/src/local/http/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum HttpClientError {

#[derive(Clone)]
pub struct HttpClient {
#[allow(clippy::type_complexity)]
cache_conn: Arc<Mutex<LruCache<Address, VecDeque<(HttpConnection, Instant)>>>>,
}

Expand Down Expand Up @@ -96,7 +97,7 @@ impl HttpClient {
}

async fn get_cached_connection(&self, host: &Address) -> Option<HttpConnection> {
if let Some(q) = self.cache_conn.lock().await.get_mut(&host) {
if let Some(q) = self.cache_conn.lock().await.get_mut(host) {
while let Some((c, inst)) = q.pop_front() {
let now = Instant::now();
if now - inst >= CONNECTION_EXPIRE_DURATION {
Expand Down
72 changes: 21 additions & 51 deletions crates/shadowsocks-service/src/local/redir/sys/unix/bsd_pf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl PacketFilter {
match proto {
Protocol::TCP => self.tcp_natlook(bind_addr, peer_addr, proto),
Protocol::UDP => self.udp_natlook(bind_addr, peer_addr, proto),
_ => return Err(io::ErrorKind::InvalidInput.into()),
_ => Err(io::ErrorKind::InvalidInput.into()),
}
}

Expand All @@ -81,12 +81,7 @@ impl PacketFilter {
let addr: *const in_addr = ptr::addr_of!((*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,
ptr::addr_of_mut!(pnl.daddr.pfa) as *mut _,
mem::size_of::<in_addr>(),
);
ptr::write_unaligned::<in_addr>(ptr::addr_of_mut!(pnl.daddr.pfa) as *mut _, *addr);

cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
Expand All @@ -105,12 +100,7 @@ impl PacketFilter {
let addr: *const in6_addr = ptr::addr_of!((*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,
ptr::addr_of_mut!(pnl.daddr.pfa) as *mut _,
mem::size_of::<in6_addr>(),
);
ptr::write_unaligned::<in6_addr>(ptr::addr_of_mut!(pnl.daddr.pfa) as *mut _, *addr);

cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
Expand All @@ -134,12 +124,7 @@ impl PacketFilter {
let addr: *const in_addr = ptr::addr_of!((*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,
ptr::addr_of_mut!(pnl.saddr.pfa) as *mut _,
mem::size_of::<in_addr>(),
);
ptr::write_unaligned::<in_addr>(ptr::addr_of_mut!(pnl.saddr.pfa) as *mut _, *addr);

cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
Expand All @@ -160,12 +145,7 @@ impl PacketFilter {
let addr: *const in6_addr = ptr::addr_of!((*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,
ptr::addr_of_mut!(pnl.saddr.pfa) as *mut _,
mem::size_of::<in6_addr>(),
);
ptr::write_unaligned::<in6_addr>(ptr::addr_of_mut!(pnl.saddr.pfa) as *mut _, *addr);

cfg_if! {
if #[cfg(any(target_os = "macos", target_os = "ios"))] {
Expand Down Expand Up @@ -197,11 +177,9 @@ impl PacketFilter {
}
}

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(
ptr::addr_of!(pnl.rdaddr.pfa) as *const _,
ptr::write_unaligned::<in_addr>(
ptr::addr_of_mut!(dst_addr.sin_addr),
mem::size_of_val(&dst_addr.sin_addr),
ptr::read::<in_addr>(ptr::addr_of!(pnl.rdaddr.pfa) as *const _),
);
*addr_len = mem::size_of_val(&dst_addr.sin_addr) as libc::socklen_t;
} else if pnl.af == libc::AF_INET6 as libc::sa_family_t {
Expand All @@ -216,11 +194,9 @@ impl PacketFilter {
}
}

#[allow(clippy::size_of_in_element_count)]
ptr::copy_nonoverlapping(
ptr::addr_of!(pnl.rdaddr.pfa) as *const _,
ptr::write_unaligned::<in6_addr>(
ptr::addr_of_mut!(dst_addr.sin6_addr),
mem::size_of_val(&dst_addr.sin6_addr),
ptr::read::<in6_addr>(ptr::addr_of!(pnl.rdaddr.pfa) as *const _),
);
*addr_len = mem::size_of_val(&dst_addr.sin6_addr) as libc::socklen_t;
} else {
Expand Down Expand Up @@ -270,18 +246,16 @@ impl PacketFilter {
match bind_addr_sockaddr.family() as libc::c_int {
libc::AF_INET => {
let sockaddr: *const sockaddr_in = bind_addr_sockaddr.as_ptr() as *const _;
ptr::copy_nonoverlapping(
ptr::addr_of!((&*sockaddr).sin_addr),
ptr::write_unaligned::<in_addr>(
ptr::addr_of_mut!(bind_addr_pfaddr.pfa) as *mut _,
mem::size_of::<in_addr>(),
(*sockaddr).sin_addr,
);
}
libc::AF_INET6 => {
let sockaddr: *const sockaddr_in6 = bind_addr_sockaddr.as_ptr() as *const _;
ptr::copy_nonoverlapping(
ptr::addr_of!((&*sockaddr).sin6_addr),
ptr::write_unaligned::<in6_addr>(
ptr::addr_of_mut!(bind_addr_pfaddr.pfa) as *mut _,
mem::size_of::<in6_addr>(),
(*sockaddr).sin6_addr,
);
}
_ => unreachable!("bind_addr family = {}", bind_addr_sockaddr.family()),
Expand All @@ -290,18 +264,16 @@ impl PacketFilter {
match peer_addr_sockaddr.family() as libc::c_int {
libc::AF_INET => {
let sockaddr: *const sockaddr_in = peer_addr_sockaddr.as_ptr() as *const _;
ptr::copy_nonoverlapping(
ptr::addr_of!((&*sockaddr).sin_addr),
ptr::write_unaligned::<in_addr>(
ptr::addr_of_mut!(peer_addr_pfaddr.pfa) as *mut _,
mem::size_of::<in_addr>(),
(*sockaddr).sin_addr,
);
}
libc::AF_INET6 => {
let sockaddr: *const sockaddr_in6 = peer_addr_sockaddr.as_ptr() as *const _;
ptr::copy_nonoverlapping(
ptr::addr_of!((&*sockaddr).sin6_addr),
ptr::write_unaligned::<in6_addr>(
ptr::addr_of_mut!(peer_addr_pfaddr.pfa) as *mut _,
mem::size_of::<in6_addr>(),
(*sockaddr).sin6_addr,
);
}
_ => unreachable!("peer_addr family = {}", peer_addr_sockaddr.family()),
Expand Down Expand Up @@ -341,10 +313,9 @@ impl PacketFilter {
let (_, actual_dst_addr) = SockAddr::try_init(|sockaddr, len| {
let addr = &mut *(sockaddr as *mut sockaddr_in);
addr.sin_family = libc::AF_INET as libc::sa_family_t;
ptr::copy_nonoverlapping(
ptr::addr_of!(state.gwy.addr.pfa) as *const _,
ptr::write_unaligned::<in_addr>(
ptr::addr_of_mut!(addr.sin_addr),
mem::size_of::<in_addr>(),
ptr::read_unaligned::<in_addr>(ptr::addr_of!(state.gwy.addr.pfa) as *const _),
);
addr.sin_port = actual_dst_port as libc::in_port_t;

Expand All @@ -359,10 +330,9 @@ impl PacketFilter {
let (_, actual_dst_addr) = SockAddr::try_init(|sockaddr, len| {
let addr = &mut *(sockaddr as *mut sockaddr_in6);
addr.sin6_family = libc::AF_INET6 as libc::sa_family_t;
ptr::copy_nonoverlapping(
ptr::addr_of!(state.gwy.addr.pfa) as *const _,
ptr::write_unaligned::<in6_addr>(
ptr::addr_of_mut!(addr.sin6_addr),
mem::size_of::<in6_addr>(),
ptr::read_unaligned::<in6_addr>(ptr::addr_of!(state.gwy.addr.pfa) as *const _),
);
addr.sin6_port = actual_dst_port as libc::in_port_t;

Expand Down
2 changes: 2 additions & 0 deletions crates/shadowsocks-service/src/local/redir/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ cfg_if! {
target_os = "ios"))] {
#[path = "pfvar_bindgen_macos.rs"]
#[allow(dead_code, non_upper_case_globals, non_snake_case, non_camel_case_types)]
#[allow(clippy::useless_transmute)]
mod pfvar;
} else if #[cfg(target_os = "freebsd")] {
#[path = "pfvar_bindgen_freebsd.rs"]
#[allow(dead_code, non_upper_case_globals, non_snake_case, non_camel_case_types)]
#[allow(clippy::useless_transmute)]
mod pfvar;
}
}
7 changes: 6 additions & 1 deletion crates/shadowsocks-service/src/manager/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ impl Manager {

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

match OpenOptions::new().write(true).create(true).open(&config_file_path) {
match OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&config_file_path)
{
Err(err) => {
error!(
"failed to open {} for writing, error: {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/sys/unix/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn get_launch_activate_socket(name: &str) -> io::Result<RawFd> {
} else if cnt > 1 {
for idx in 0..cnt {
unsafe {
let fd = *(fds.offset(idx as isize));
let fd = *(fds.add(idx));
let _ = libc::close(fd);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl ServerConfig {
if #[cfg(feature = "aead-cipher-2022")] {
let user_info = if !self.method().is_aead_2022() {
let user_info = format!("{}:{}", self.method(), self.password());
URL_PASSWORD_BASE64_ENGINE.encode(&user_info)
URL_PASSWORD_BASE64_ENGINE.encode(user_info)
} else {
format!("{}:{}", self.method(), percent_encoding::utf8_percent_encode(self.password(), percent_encoding::NON_ALPHANUMERIC))
};
Expand Down
4 changes: 2 additions & 2 deletions crates/shadowsocks/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl UdpSocket {
// Check MTU
if let Some(mtu) = self.mtu {
if buf.len() > mtu {
return Err(make_mtu_error(buf.len(), mtu)).into();
return Err(make_mtu_error(buf.len(), mtu));
}
}

Expand All @@ -229,7 +229,7 @@ impl UdpSocket {
// Check MTU
if let Some(mtu) = self.mtu {
if buf.len() > mtu {
return Err(make_mtu_error(buf.len(), mtu)).into();
return Err(make_mtu_error(buf.len(), mtu));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn init_with_config(bin_name: &str, config: &LogConfig) {
};

let config = match debug_level {
0 | 1 | 2 | 3 => logging_builder
0..=3 => logging_builder
.logger(Logger::builder().build(bin_name, l1))
.logger(Logger::builder().build("shadowsocks_rust", l1))
.logger(Logger::builder().build("shadowsocks", l1))
Expand Down

0 comments on commit 609de80

Please sign in to comment.