Skip to content

Commit

Permalink
feat: add StdSocketExt
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Apr 9, 2024
1 parent 9d0de09 commit 6872792
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clash_lib/src/proxy/direct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyStream> {
Ok(s)
}
Expand Down
22 changes: 22 additions & 0 deletions clash_lib/src/proxy/utils/socket_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ 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)
}
}

#[allow(unused_variables)]
fn set_mark(socket: socket2::SockRef<'_>, mark: u32) -> io::Result<()> {
#[cfg(target_os = "linux")]
return socket.set_mark(mark);
#[cfg(not(target_os = "linux"))]
return Ok(());
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 6872792

Please sign in to comment.