From 85ac4dfb2aa3d57fdadf3dde42df28e53152132e Mon Sep 17 00:00:00 2001 From: iHsin Date: Tue, 9 Apr 2024 21:56:14 +0800 Subject: [PATCH] feat: add StdSocketExt --- clash_lib/src/proxy/direct/mod.rs | 4 ++-- clash_lib/src/proxy/utils/socket_helpers.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/clash_lib/src/proxy/direct/mod.rs b/clash_lib/src/proxy/direct/mod.rs index 34e339f1e..2daa2a024 100644 --- a/clash_lib/src/proxy/direct/mod.rs +++ b/clash_lib/src/proxy/direct/mod.rs @@ -65,8 +65,8 @@ impl OutboundHandler for Handler { async fn proxy_stream( &self, s: AnyStream, - #[allow(unused_variables)] sess: &Session, - #[allow(unused_variables)] _resolver: ThreadSafeDNSResolver, + _sess: &Session, + _resolver: ThreadSafeDNSResolver, ) -> std::io::Result { Ok(s) } diff --git a/clash_lib/src/proxy/utils/socket_helpers.rs b/clash_lib/src/proxy/utils/socket_helpers.rs index e03d012c3..319b68e08 100644 --- a/clash_lib/src/proxy/utils/socket_helpers.rs +++ b/clash_lib/src/proxy/utils/socket_helpers.rs @@ -157,6 +157,27 @@ pub async fn new_udp_socket( UdpSocket::from_std(socket.into()) } +/// An extension to std::net::{UdpSocket, TcpStream} +pub trait StdSocketExt { + fn set_mark(&self, mark: u32) -> io::Result<()>; +} +impl StdSocketExt for std::net::UdpSocket { + fn set_mark(&self, mark: u32) -> io::Result<()> { + set_mark(socket2::SockRef::from(self), mark) + } +} +impl StdSocketExt for std::net::TcpStream { + fn set_mark(&self, mark: u32) -> io::Result<()> { + set_mark(socket2::SockRef::from(self), mark) + } +} + +fn set_mark<'a>(socket: socket2::SockRef<'a>, mark: u32) -> io::Result<()> { + #[cfg(target_os = "linux")] + return socket.set_mark(mark); + #[cfg(not(target_os = "linux"))] + return Ok(()); +} #[cfg(test)] mod tests {