Minor changes

This commit is contained in:
eric committed 2020-11-10 19:48:16 +08:00
1 parent e6fe417713
commit 31dea268a6
3 files changed
+38 -32

No files matched your search

+11 -10
View File
@@ -397,16 +397,17 @@ impl Dispatcher {
where
T: 'static + AsyncRead + AsyncWrite + Unpin + Send + Sync,
{
let lhs: Box<dyn ProxyStream> = 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<dyn ProxyStream> =
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) => {
+14 -8
View File
@@ -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<u8>,
@@ -32,8 +35,6 @@ pub struct NatManager {
impl NatManager {
pub fn new(dispatcher: Arc<Dispatcher>) -> 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();
}
}
}
}
+13 -14
View File
@@ -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<io::Result<usize>> {
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",
)))
}
}
}