diff --git a/crates/shadowsocks-service/src/local/net/mod.rs b/crates/shadowsocks-service/src/local/net/mod.rs index 35cc82c0..ae2743b8 100644 --- a/crates/shadowsocks-service/src/local/net/mod.rs +++ b/crates/shadowsocks-service/src/local/net/mod.rs @@ -6,4 +6,4 @@ pub use self::{ }; mod tcp; -mod udp; +pub(crate) mod udp; diff --git a/crates/shadowsocks-service/src/local/net/udp/association.rs b/crates/shadowsocks-service/src/local/net/udp/association.rs index f679f041..61086655 100644 --- a/crates/shadowsocks-service/src/local/net/udp/association.rs +++ b/crates/shadowsocks-service/src/local/net/udp/association.rs @@ -187,10 +187,26 @@ struct ServerSessionContext { server_session_map: LruCache, } +// Server shouldn't be remembered too long. +// +// Server session will only changed if +// 1. Association expired +// 2. Server restarted +// +// Normally there should only be 1 unqiue server session. +pub const SERVER_SESSION_REMEMBER_DURATION: Duration = Duration::from_secs(60); + +// Remember 2 server sessions. When server restarts, +// some of the packet on wire may be received later then those new ones. +pub const SERVER_SESSION_REMEMBER_COUNT: usize = 2; + impl ServerSessionContext { fn new() -> ServerSessionContext { ServerSessionContext { - server_session_map: LruCache::with_expiry_duration_and_capacity(Duration::from_secs(30 * 60), 5), + server_session_map: LruCache::with_expiry_duration_and_capacity( + SERVER_SESSION_REMEMBER_DURATION, + SERVER_SESSION_REMEMBER_COUNT, + ), } } } diff --git a/crates/shadowsocks-service/src/local/tunnel/udprelay.rs b/crates/shadowsocks-service/src/local/tunnel/udprelay.rs index f22aa4a1..efacd696 100644 --- a/crates/shadowsocks-service/src/local/tunnel/udprelay.rs +++ b/crates/shadowsocks-service/src/local/tunnel/udprelay.rs @@ -25,7 +25,11 @@ use shadowsocks::{ use tokio::{net::UdpSocket, sync::mpsc, task::JoinHandle, time}; use crate::{ - local::{context::ServiceContext, loadbalancing::PingBalancer}, + local::{ + context::ServiceContext, + loadbalancing::PingBalancer, + net::udp::association::{SERVER_SESSION_REMEMBER_COUNT, SERVER_SESSION_REMEMBER_DURATION}, + }, net::{ packet_window::PacketWindowFilter, MonProxySocket, @@ -217,7 +221,10 @@ struct ServerSessionContext { impl ServerSessionContext { fn new() -> ServerSessionContext { ServerSessionContext { - server_session_map: LruCache::with_expiry_duration_and_capacity(Duration::from_secs(30 * 60), 5), + server_session_map: LruCache::with_expiry_duration_and_capacity( + SERVER_SESSION_REMEMBER_DURATION, + SERVER_SESSION_REMEMBER_COUNT, + ), } } } diff --git a/crates/shadowsocks-service/src/server/udprelay.rs b/crates/shadowsocks-service/src/server/udprelay.rs index b7af8dcd..e048fda1 100644 --- a/crates/shadowsocks-service/src/server/udprelay.rs +++ b/crates/shadowsocks-service/src/server/udprelay.rs @@ -324,9 +324,20 @@ struct ClientSessionContext { impl ClientSessionContext { fn new(client_session_id: u64) -> ClientSessionContext { + // Client shouldn't be remembered too long. + // If a client was switching between networks (like Wi-Fi and Cellular), + // when it switched back from another, the packet filter window will be too old. + const CLIENT_SESSION_REMEMBER_DURATION: Duration = Duration::from_secs(60); + + // Wi-Fi & Cellular network device, so it is 2 for most users + const CLIENT_SESSION_REMEMBER_COUNT: usize = 2; + ClientSessionContext { client_session_id, - client_context_map: LruCache::with_expiry_duration_and_capacity(Duration::from_secs(30 * 60), 10), + client_context_map: LruCache::with_expiry_duration_and_capacity( + CLIENT_SESSION_REMEMBER_DURATION, + CLIENT_SESSION_REMEMBER_COUNT, + ), } } }