Skip to content

Commit

Permalink
refactor: instead of delivery mark everywhere, use global mark
Browse files Browse the repository at this point in the history
  • Loading branch information
iHsin committed Apr 9, 2024
1 parent 2fb6ead commit 4ee0b15
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 45 deletions.
4 changes: 1 addition & 3 deletions clash_lib/src/app/dns/dhcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ async fn listen_dhcp_client(iface: &str) -> io::Result<UdpSocket> {

new_udp_socket(
Some(&listen_addr.parse().expect("must parse")),
Some(&Interface::Name(iface.to_string())),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
Some(&Interface::Name(iface.to_string()))
)
.await
}
Expand Down
4 changes: 1 addition & 3 deletions clash_lib/src/common/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ impl Service<Uri> for LocalConnector {
_ => panic!("invalid url: {}", remote),
},
}),
None,
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
None
)
.await
})
Expand Down
8 changes: 2 additions & 6 deletions clash_lib/src/proxy/direct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl OutboundHandler for Handler {
resolver,
sess.destination.host().as_str(),
sess.destination.port(),
None,
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
None
)
.await?;

Expand All @@ -79,9 +77,7 @@ impl OutboundHandler for Handler {
) -> std::io::Result<BoxedChainedDatagram> {
let d = new_udp_socket(
None,
sess.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
sess.iface.as_ref()
)
.await
.map(|x| OutboundDatagramImpl::new(x, resolver))?;
Expand Down
4 changes: 1 addition & 3 deletions clash_lib/src/proxy/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ impl OutboundHandler for Handler {
resolver.clone(),
remote_addr.host().as_str(),
remote_addr.port(),
None,
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
None
)
.await?;

Expand Down
8 changes: 2 additions & 6 deletions clash_lib/src/proxy/shadowsocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ impl OutboundHandler for Handler {
resolver.clone(),
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.map_err(|x| {
io::Error::new(
Expand Down Expand Up @@ -310,9 +308,7 @@ impl OutboundHandler for Handler {
);
let socket = new_udp_socket(
None,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.await?;
let socket = ProxySocket::from_socket(UdpSocketType::Client, ctx, &cfg, socket);
Expand Down
5 changes: 1 addition & 4 deletions clash_lib/src/proxy/socks/inbound/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ pub async fn handle_tcp<'a>(
let udp_addr = SocketAddr::new(s.local_addr()?.ip(), 0);
let udp_inbound = new_udp_socket(
Some(&udp_addr),
None,
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
None
)
.await?;

Expand All @@ -166,7 +164,6 @@ pub async fn handle_tcp<'a>(
let sess = Session {
network: Network::Udp,
typ: Type::Socks5,
packet_mark: None,
iface: None,
..Default::default()
};
Expand Down
8 changes: 2 additions & 6 deletions clash_lib/src/proxy/trojan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ impl OutboundHandler for Handler {
resolver.clone(),
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.map_err(|x| {
io::Error::new(
Expand Down Expand Up @@ -195,9 +193,7 @@ impl OutboundHandler for Handler {
resolver.clone(),
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.map_err(|x| {
io::Error::new(
Expand Down
9 changes: 4 additions & 5 deletions clash_lib/src/proxy/utils/socket_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::{
net::{TcpSocket, TcpStream, UdpSocket},
time::timeout,
};
use crate::session::get_somark;

#[cfg(target_os = "windows")]
use tracing::warn;
Expand Down Expand Up @@ -72,7 +73,6 @@ pub async fn new_tcp_stream<'a>(
address: &'a str,
port: u16,
iface: Option<&'a Interface>,
#[cfg(any(target_os = "linux", target_os = "android"))] packet_mark: Option<u32>,
) -> io::Result<AnyStream> {
let dial_addr = resolver
.resolve(address, false)
Expand Down Expand Up @@ -103,8 +103,8 @@ pub async fn new_tcp_stream<'a>(
must_bind_socket_on_interface(&socket, iface)?;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(packet_mark) = packet_mark {
#[cfg(target_os = "linux")]
if let Some(packet_mark) = get_somark() {
socket.set_mark(packet_mark)?;
}

Expand All @@ -124,7 +124,6 @@ pub async fn new_tcp_stream<'a>(
pub async fn new_udp_socket(
src: Option<&SocketAddr>,
iface: Option<&Interface>,
#[cfg(any(target_os = "linux", target_os = "android"))] packet_mark: Option<u32>,
) -> io::Result<UdpSocket> {
let socket = match src {
Some(src) => {
Expand All @@ -146,7 +145,7 @@ pub async fn new_udp_socket(
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(packet_mark) = packet_mark {
if let Some(packet_mark) = get_somark() {
socket.set_mark(packet_mark)?;
}

Expand Down
8 changes: 2 additions & 6 deletions clash_lib/src/proxy/vmess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ impl OutboundHandler for Handler {
resolver,
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.map_err(|x| {
io::Error::new(
Expand Down Expand Up @@ -214,9 +212,7 @@ impl OutboundHandler for Handler {
resolver.clone(),
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
self.opts.common_opts.iface.as_ref()
)
.map_err(|x| {
io::Error::new(
Expand Down
4 changes: 1 addition & 3 deletions clash_lib/src/proxy/wg/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ impl WireguardTunnel {

let udp = new_udp_socket(
None,
None,
#[cfg(any(target_os = "linux", target_os = "android"))]
None,
None
)
.await?;

Expand Down

0 comments on commit 4ee0b15

Please sign in to comment.