diff --git a/leaf/src/app/dispatcher.rs b/leaf/src/app/dispatcher.rs index 3ad7011..910b9bf 100644 --- a/leaf/src/app/dispatcher.rs +++ b/leaf/src/app/dispatcher.rs @@ -397,16 +397,17 @@ impl Dispatcher { where T: 'static + AsyncRead + AsyncWrite + Unpin + Send + Sync, { - let lhs: Box = if sess.destination.is_domain() { - Box::new(SimpleStream(lhs)) - } else { - let mut lhs = SniffingStream::new(lhs); - if let Some(domain) = lhs.sniff().await? { - debug!("sniffed domain {}", &domain); - sess.destination = SocksAddr::from((domain, sess.destination.port())); - } - Box::new(SimpleStream(lhs)) - }; + let lhs: Box = + if sess.destination.is_domain() && sess.destination.port() == 443 { + Box::new(SimpleStream(lhs)) + } else { + let mut lhs = SniffingStream::new(lhs); + if let Some(domain) = lhs.sniff().await? { + debug!("sniffed domain {}", &domain); + sess.destination = SocksAddr::from((domain, sess.destination.port())); + } + Box::new(SimpleStream(lhs)) + }; let outbound = match self.router.pick_route(&sess) { Ok(tag) => { diff --git a/leaf/src/app/nat_manager.rs b/leaf/src/app/nat_manager.rs index b9ed8be..8ed7050 100644 --- a/leaf/src/app/nat_manager.rs +++ b/leaf/src/app/nat_manager.rs @@ -15,6 +15,9 @@ use tokio::sync::{ use crate::app::dispatcher::Dispatcher; use crate::session::{Session, SocksAddr}; +static UDP_SESSION_TIMEOUT: u64 = 30; +static UDP_SESSION_TIMEOUT_CHECK_INTERVAL: u64 = 10; + #[derive(Debug)] pub struct UdpPacket { pub data: Vec, @@ -32,8 +35,6 @@ pub struct NatManager { impl NatManager { pub fn new(dispatcher: Arc) -> Self { - let timeout: u64 = 30; - let check_interval: u64 = 10; let sessions: SessionMap = Arc::new(TokioMutex::new(HashMap::new())); let sessions2 = sessions.clone(); @@ -44,7 +45,7 @@ impl NatManager { let n_total = sessions.len(); let now = Instant::now(); sessions.retain(|key, sess| { - if now.duration_since(sess.2).as_secs() >= timeout { + if now.duration_since(sess.2).as_secs() >= UDP_SESSION_TIMEOUT { // Abort downlink task, uplink task will end automatically // when we drop the channel's tx side upon session removal. sess.1.abort(); @@ -64,7 +65,8 @@ impl NatManager { n_remaining ); } - tokio::time::delay_for(Duration::from_secs(check_interval)).await; + tokio::time::delay_for(Duration::from_secs(UDP_SESSION_TIMEOUT_CHECK_INTERVAL)) + .await; } }); @@ -150,9 +152,6 @@ impl NatManager { } if addr.port() == 53 { - // If the destination port is 53, we assume it's a - // DNS query and remove the session at once after - // receiving the response. sessions.lock().await.remove(&raddr); break; } @@ -161,7 +160,14 @@ impl NatManager { { let mut sessions = sessions.lock().await; if let Some(sess) = sessions.get_mut(&raddr) { - sess.2 = Instant::now(); + if addr.port() == 53 { + // If the destination port is 53, we assume it's a + // DNS query and set a negative timeout so it will + // be removed on next check. + sess.2.checked_sub(Duration::from_secs(UDP_SESSION_TIMEOUT)); + } else { + sess.2 = Instant::now(); + } } } } diff --git a/leaf/src/proxy/tun/netstack/stack_impl.rs b/leaf/src/proxy/tun/netstack/stack_impl.rs index cc1f95b..2046830 100644 --- a/leaf/src/proxy/tun/netstack/stack_impl.rs +++ b/leaf/src/proxy/tun/netstack/stack_impl.rs @@ -309,6 +309,8 @@ impl AsyncRead for NetStackImpl { impl AsyncWrite for NetStackImpl { fn poll_write(self: Pin<&mut Self>, _cx: &mut Context, buf: &[u8]) -> Poll> { unsafe { + let _g = self.lwip_lock.lock(); + let pbuf = pbuf_alloc(pbuf_layer_PBUF_RAW, buf.len() as u16_t, pbuf_type_PBUF_RAM); if pbuf.is_null() { warn!("alloc null pbuf"); @@ -316,26 +318,23 @@ impl AsyncWrite for NetStackImpl { } pbuf_take(pbuf, buf.as_ptr() as *const raw::c_void, buf.len() as u16_t); - { - let _g = self.lwip_lock.lock(); - if let Some(input_fn) = (*netif_list).input { - let err = input_fn(pbuf, netif_list); - if err == err_enum_t_ERR_OK as err_t { - Poll::Ready(Ok(buf.len())) - } else { - pbuf_free(pbuf); - Poll::Ready(Err(io::Error::new( - io::ErrorKind::Interrupted, - "input failed", - ))) - } + if let Some(input_fn) = (*netif_list).input { + let err = input_fn(pbuf, netif_list); + if err == err_enum_t_ERR_OK as err_t { + Poll::Ready(Ok(buf.len())) } else { pbuf_free(pbuf); Poll::Ready(Err(io::Error::new( io::ErrorKind::Interrupted, - "none input fn", + "input failed", ))) } + } else { + pbuf_free(pbuf); + Poll::Ready(Err(io::Error::new( + io::ErrorKind::Interrupted, + "none input fn", + ))) } } }