Skip to content

Commit

Permalink
feat(windows): IF index cache use HashMap directly
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Aug 12, 2023
1 parent e383129 commit dcc4743
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
9 changes: 7 additions & 2 deletions crates/shadowsocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ stream-cipher = ["shadowsocks-crypto/v1-stream"]
aead-cipher-extra = ["shadowsocks-crypto/v1-aead-extra"]

# Enable AEAD 2022
aead-cipher-2022 = ["shadowsocks-crypto/v2", "rand/small_rng", "aes"]
aead-cipher-2022 = [
"shadowsocks-crypto/v2",
"rand/small_rng",
"aes",
"lru_time_cache",
]
# Enable AEAD 2022 with extra ciphers
aead-cipher-2022-extra = ["aead-cipher-2022", "shadowsocks-crypto/v2-extra"]

Expand All @@ -52,7 +57,7 @@ pin-project = "1.1"
bloomfilter = { version = "1.0.8", optional = true }
thiserror = "1.0"
rand = { version = "0.8", optional = true }
lru_time_cache = "0.11"
lru_time_cache = { version = "0.11", optional = true }

serde = { version = "1.0", features = ["derive"] }
serde_urlencoded = "0.7"
Expand Down
42 changes: 10 additions & 32 deletions crates/shadowsocks/src/net/sys/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashMap,
ffi::{c_void, CStr, CString, OsString},
io::{self, ErrorKind},
mem,
Expand All @@ -8,15 +9,13 @@ use std::{
io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket},
},
pin::Pin,
ptr,
slice,
ptr, slice,
task::{self, Poll},
time::{Duration, Instant},
};

use bytes::BytesMut;
use log::{error, warn};
use lru_time_cache::LruCache;
use once_cell::sync::Lazy;
use pin_project::pin_project;
use socket2::{Domain, Protocol, SockAddr, Socket, TcpKeepalive, Type};
Expand All @@ -31,32 +30,13 @@ use windows_sys::{
Win32::{
Foundation::{BOOL, ERROR_BUFFER_OVERFLOW, ERROR_NO_DATA, ERROR_SUCCESS},
NetworkManagement::IpHelper::{
if_nametoindex,
GetAdaptersAddresses,
GAA_FLAG_SKIP_ANYCAST,
GAA_FLAG_SKIP_DNS_SERVER,
GAA_FLAG_SKIP_MULTICAST,
GAA_FLAG_SKIP_UNICAST,
IP_ADAPTER_ADDRESSES_LH,
if_nametoindex, GetAdaptersAddresses, GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER,
GAA_FLAG_SKIP_MULTICAST, GAA_FLAG_SKIP_UNICAST, IP_ADAPTER_ADDRESSES_LH,
},
Networking::WinSock::{
htonl,
setsockopt,
WSAGetLastError,
WSAIoctl,
AF_UNSPEC,
IPPROTO_IP,
IPPROTO_IPV6,
IPPROTO_TCP,
IPV6_MTU_DISCOVER,
IPV6_UNICAST_IF,
IP_MTU_DISCOVER,
IP_PMTUDISC_DO,
IP_UNICAST_IF,
SIO_UDP_CONNRESET,
SOCKET,
SOCKET_ERROR,
TCP_FASTOPEN,
htonl, setsockopt, WSAGetLastError, WSAIoctl, AF_UNSPEC, IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP,
IPV6_MTU_DISCOVER, IPV6_UNICAST_IF, IP_MTU_DISCOVER, IP_PMTUDISC_DO, IP_UNICAST_IF, SIO_UDP_CONNRESET,
SOCKET, SOCKET_ERROR, TCP_FASTOPEN,
},
},
};
Expand All @@ -67,9 +47,7 @@ const FALSE: BOOL = 0;
use crate::net::{
is_dual_stack_addr,
sys::{set_common_sockopt_for_connect, socket_bind_dual_stack},
AcceptOpts,
AddrFamily,
ConnectOpts,
AcceptOpts, AddrFamily, ConnectOpts,
};

/// A `TcpStream` that supports TFO (TCP Fast Open)
Expand Down Expand Up @@ -291,8 +269,8 @@ fn find_adapter_interface_index(addr: &SocketAddr, iface: &str) -> io::Result<Op
async fn find_interface_index_cached(addr: &SocketAddr, iface: &str) -> io::Result<u32> {
const INDEX_EXPIRE_DURATION: Duration = Duration::from_secs(5);

static INTERFACE_INDEX_CACHE: Lazy<Mutex<LruCache<String, (u32, Instant)>>> =
Lazy::new(|| Mutex::new(LruCache::with_expiry_duration(INDEX_EXPIRE_DURATION)));
static INTERFACE_INDEX_CACHE: Lazy<Mutex<HashMap<String, (u32, Instant)>>> =
Lazy::new(|| Mutex::new(HashMap::new()));

let mut cache = INTERFACE_INDEX_CACHE.lock().await;
if let Some((idx, insert_time)) = cache.get(iface) {
Expand Down

0 comments on commit dcc4743

Please sign in to comment.