Skip to content

Commit

Permalink
refactor: change fwmark to global static var
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Apr 9, 2024
1 parent cdf9d85 commit 2fb6ead
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions clash_lib/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::{
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
Expand Down Expand Up @@ -375,8 +376,6 @@ pub struct Session {
pub source: SocketAddr,
/// The proxy target address of a proxy connection.
pub destination: SocksAddr,
/// The packet mark SO_MARK
pub packet_mark: Option<u32>,
/// The bind interface
pub iface: Option<Interface>,
}
Expand Down Expand Up @@ -409,7 +408,6 @@ impl Default for Session {
typ: Type::Http,
source: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0),
destination: SocksAddr::any_ipv4(),
packet_mark: None,
iface: None,
}
}
Expand All @@ -431,7 +429,6 @@ impl Debug for Session {
.field("network", &self.network)
.field("source", &self.source)
.field("destination", &self.destination)
.field("packet_mark", &self.packet_mark)
.field("iface", &self.iface)
.finish()
}
Expand All @@ -444,7 +441,6 @@ impl Clone for Session {
typ: self.typ,
source: self.source,
destination: self.destination.clone(),
packet_mark: self.packet_mark,
iface: self.iface.as_ref().cloned(),
}
}
Expand All @@ -461,3 +457,15 @@ fn invalid_atyp() -> io::Error {
fn insuff_bytes() -> io::Error {
io::Error::new(io::ErrorKind::Other, "insufficient bytes")
}


static GLOBAL_MARK: AtomicU32 = AtomicU32::new(0);
static ENABLE_MARK: AtomicBool = AtomicBool::new(false);
/// get socket SO_MARK that outgoing socket should use
pub fn get_somark() -> Option<u32> {
if ENABLE_MARK.load(Ordering::Relaxed) {
Some(GLOBAL_MARK.load(Ordering::Relaxed))
} else {
None
}
}

0 comments on commit 2fb6ead

Please sign in to comment.