diff --git a/leaf/Cargo.toml b/leaf/Cargo.toml index 9efbc2f..8c8d0fa 100644 --- a/leaf/Cargo.toml +++ b/leaf/Cargo.toml @@ -121,13 +121,13 @@ outbound-failover = ["lru_time_cache"] outbound-static= [] outbound-tryall = [] outbound-chain = [] -outbound-vless = ["uuid", "hex"] +outbound-vless = ["hex"] outbound-reality = ["reality", "reality-rustls", "webpki-roots", "rustls-pemfile", "hex", "base64"] outbound-amux= ["tokio-util"] outbound-quic = ["rustls", "webpki-roots-old", "rustls-pemfile-old"] outbound-mptp = [] outbound-select = ["directories", "axum/query"] -outbound-vmess = ["lz_fnv", "cfb-mode", "hmac", "sha2", "aes", "aes-gcm", "sha3", "digest", "uuid", "md-5", "tokio-util", "byteorder", "crc32fast"] +outbound-vmess = ["lz_fnv", "cfb-mode", "hmac", "sha2", "aes", "aes-gcm", "sha3", "digest", "md-5", "tokio-util", "byteorder", "crc32fast"] # Inbounds inbound-trojan = ["sha2", "hex"] @@ -164,6 +164,7 @@ rand = "0.8" socket2 = "0.5" async-recursion = "1.1" parking_lot = "0.12" +uuid = { version = "1", features = ["v4"] } # DNS hickory-proto = { version = "0.24", default-features = false } @@ -245,7 +246,6 @@ aes = { version = "0.8", optional = true } aes-gcm = { version = "0.10", optional = true } sha3 = { version = "0.10", optional = true } digest = { version = "0.10", optional = true } -uuid = { version = "1", features = ["v4"], optional = true } byteorder = { version = "1", optional = true } crc32fast = { version = "1", optional = true } diff --git a/leaf/src/app/nat_manager.rs b/leaf/src/app/nat_manager.rs index 9676d0e..24c7e99 100644 --- a/leaf/src/app/nat_manager.rs +++ b/leaf/src/app/nat_manager.rs @@ -131,6 +131,7 @@ impl NatManager { let mut sess = sess.cloned().unwrap_or_else(|| Session { network: Network::Udp, source: dgram_src.address, + stream_id: dgram_src.stream_id, destination: pkt.dst_addr.clone(), inbound_tag: inbound_tag.to_string(), process_name: dgram_src.process_name.clone(), diff --git a/leaf/src/proxy/amux/inbound/stream.rs b/leaf/src/proxy/amux/inbound/stream.rs index d101fb6..96388b8 100644 --- a/leaf/src/proxy/amux/inbound/stream.rs +++ b/leaf/src/proxy/amux/inbound/stream.rs @@ -7,7 +7,7 @@ use futures::{ task::{Context, Poll}, }; -use crate::{proxy::*, session::Session}; +use crate::{proxy::*, session::Session, session::StreamId}; use super::MuxAcceptor; use super::MuxSession; @@ -33,7 +33,7 @@ impl Stream for Incoming { Poll::Ready( ready!(Pin::new(&mut self.acceptor).poll_next(cx)).map(|stream| { let mut sess = self.sess.clone(); - sess.stream_id = Some(stream.id().into()); + sess.stream_id = Some(StreamId::U64(stream.id().into())); AnyBaseInboundTransport::Stream(Box::new(stream), sess) }), ) diff --git a/leaf/src/proxy/amux/mod.rs b/leaf/src/proxy/amux/mod.rs index 7f0a6b8..bbdc77c 100644 --- a/leaf/src/proxy/amux/mod.rs +++ b/leaf/src/proxy/amux/mod.rs @@ -713,37 +713,20 @@ impl MuxConnector { if self.max_recv_bytes > 0 && self.recv_bytes_counter.load(Ordering::Relaxed) >= self.max_recv_bytes { - debug!( - "exceeding allowed received bytes ({}): {}", - self.session_id, self.max_recv_bytes - ); return None; } if self.max_lifetime > 0 && Instant::now().duration_since(self.started_at).as_secs() >= self.max_lifetime { - debug!( - "exceeding allowed lifetime ({}): {}s", - self.session_id, self.max_lifetime - ); return None; } if self.total_accepted >= self.max_accepts { if self.streams.lock().await.is_empty() { self.done.store(true, Ordering::Relaxed); } - debug!( - "exceeding allowed accpets ({}): {}", - self.session_id, self.max_accepts - ); return None; } if self.streams.lock().await.len() >= self.concurrency { - trace!( - "exceeding allowed concurrency ({}): {}", - self.session_id, - self.concurrency - ); return None; } let frame_write_tx = self.frame_write_tx.clone(); diff --git a/leaf/src/proxy/mptp/inbound/stream.rs b/leaf/src/proxy/mptp/inbound/stream.rs index bb0f2a1..9a16f22 100644 --- a/leaf/src/proxy/mptp/inbound/stream.rs +++ b/leaf/src/proxy/mptp/inbound/stream.rs @@ -16,7 +16,7 @@ use uuid::Uuid; use crate::{ proxy::{AnyInboundTransport, AnyStream, InboundStreamHandler, InboundTransport}, - session::{DatagramSource, Session, SocksAddr}, + session::{DatagramSource, Session, SocksAddr, StreamId}, }; struct PrefixedStream { @@ -207,9 +207,7 @@ impl InboundStreamHandler for Handler { } if req.cmd == CMD_UDP { - let mut stream_id_bytes = [0u8; 8]; - stream_id_bytes.copy_from_slice(&req.cid.as_bytes()[..8]); - let stream_id = u64::from_le_bytes(stream_id_bytes); + let stream_id = StreamId::Uuid(req.cid); let dgram_src = DatagramSource::new(sess.source, Some(stream_id)); let mptp_datagram = MptpDatagram::new_with_source(tracked_stream, dgram_src); diff --git a/leaf/src/proxy/quic/inbound/datagram.rs b/leaf/src/proxy/quic/inbound/datagram.rs index 678f6a6..7dbbc49 100644 --- a/leaf/src/proxy/quic/inbound/datagram.rs +++ b/leaf/src/proxy/quic/inbound/datagram.rs @@ -13,7 +13,7 @@ use rustls_pemfile::{certs, pkcs8_private_keys, rsa_private_keys}; use tokio::sync::mpsc::{channel, Receiver, Sender}; use tracing::{debug, trace, warn}; -use crate::{proxy::*, session::Session}; +use crate::{proxy::*, session::Session, session::StreamId}; use super::QuicProxyStream; @@ -31,7 +31,7 @@ impl Stream for Incoming { source, ..Default::default() }; - sess.stream_id = Some(send.id().index()); + sess.stream_id = Some(StreamId::U64(send.id().index())); Poll::Ready(Some(AnyBaseInboundTransport::Stream( Box::new(QuicProxyStream { recv, send }), sess, diff --git a/leaf/src/session.rs b/leaf/src/session.rs index a3a8e16..b686b8e 100644 --- a/leaf/src/session.rs +++ b/leaf/src/session.rs @@ -24,7 +24,20 @@ impl std::fmt::Display for Network { } } -pub type StreamId = u64; +#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] +pub enum StreamId { + U64(u64), + Uuid(uuid::Uuid), +} + +impl std::fmt::Display for StreamId { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::U64(id) => write!(f, "{}", id), + Self::Uuid(id) => write!(f, "{}", id), + } + } +} #[derive(PartialEq, Eq, Hash, Clone, Debug)] pub struct DatagramSource {