Skip to content

Commit

Permalink
修复客户端一直显示为relay的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Apr 7, 2024
1 parent c64af86 commit 385ee5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/core/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct ClientInfo {
pub tcp_sender: Option<Sender<Vec<u8>>>,
pub client_status: Option<ClientStatusInfo>,
pub last_join_time: DateTime<Local>,
pub timestamp: i64,
}

impl Default for ClientInfo {
Expand All @@ -70,6 +71,7 @@ impl Default for ClientInfo {
tcp_sender: None,
client_status: None,
last_join_time: Local::now(),
timestamp: 0,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/service/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ impl ServerPacketHandler {
let mut virtual_ip = request.virtual_ip;
// 可分配的ip段
let ip_range = network + 1..gateway | (!netmask);
let timestamp = Local::now().timestamp();
{
let mut lock = v.write();
let mut insert = true;
Expand Down Expand Up @@ -472,6 +473,7 @@ impl ServerPacketHandler {
info.virtual_ip = virtual_ip;
info.tcp_sender = tcp_sender.clone();
info.last_join_time = Local::now();
info.timestamp = timestamp;
lock.epoch += 1;
response.virtual_ip = virtual_ip;
response.epoch = lock.epoch as u32;
Expand All @@ -482,7 +484,7 @@ impl ServerPacketHandler {
.insert_ip_session((group_id.clone(), virtual_ip), addr)
.await;
cache
.insert_addr_session(addr, (group_id, virtual_ip))
.insert_addr_session(addr, (group_id, virtual_ip, timestamp))
.await;
let bytes = response.write_to_bytes()?;
let rs = vec![0u8; 12 + bytes.len() + ENCRYPTION_RESERVED];
Expand Down
49 changes: 30 additions & 19 deletions src/core/store/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct AppCache {
// (group,ip) -> addr
pub ip_session: ExpireMap<(String, u32), SocketAddr>,
// addr -> (group,ip)
pub addr_session: ExpireMap<SocketAddr, (String, u32)>,
pub addr_session: ExpireMap<SocketAddr, (String, u32, i64)>,
pub cipher_session: ExpireMap<SocketAddr, Arc<Aes256GcmCipher>>,
pub auth_map: ExpireMap<String, ()>,
}
Expand Down Expand Up @@ -53,25 +53,35 @@ impl AppCache {
});
let virtual_network_ = virtual_network.clone();
// 20秒钟没有收到消息则判定为掉线
let addr_session = ExpireMap::new(move |addr: SocketAddr, (group, virtual_ip)| {
log::info!(
"addr_session eviction group={},virtual_ip={},addr={}",
group,
Ipv4Addr::from(virtual_ip),
addr
);
let addr_session = ExpireMap::new(
move |addr: SocketAddr, (group, virtual_ip, timestamp)| {
log::info!(
"addr_session eviction group={},virtual_ip={},addr={},timestamp={}",
group,
Ipv4Addr::from(virtual_ip),
addr,
timestamp
);

if let Some(v) = virtual_network_.get(&group) {
let mut lock = v.write();
if let Some(item) = lock.clients.get_mut(&virtual_ip) {
if item.address != addr {
return;
if let Some(v) = virtual_network_.get(&group) {
let mut lock = v.write();
if let Some(item) = lock.clients.get_mut(&virtual_ip) {
if item.address != addr || item.timestamp != timestamp {
log::info!(
"无效信息 addr_session eviction group={},virtual_ip={},addr={},timestamp={}",
group,
Ipv4Addr::from(virtual_ip),
addr,
timestamp
);
return;
}
item.online = false;
lock.epoch += 1;
}
item.online = false;
lock.epoch += 1;
}
}
});
},
);
let cipher_session = ExpireMap::new(|_k, _v| {});
let auth_map = ExpireMap::new(|_k, _v| {});
Self {
Expand All @@ -86,7 +96,8 @@ impl AppCache {

impl AppCache {
pub fn get_context(&self, addr: &SocketAddr) -> Option<Context> {
if let Some(k) = self.addr_session.get(addr) {
if let Some((group, virtual_ip, _)) = self.addr_session.get(addr) {
let k = (group, virtual_ip);
self.ip_session.get(&k)?;
let (group, virtual_ip) = k;
return self
Expand All @@ -111,7 +122,7 @@ impl AppCache {
.insert(key, value, Duration::from_secs(24 * 3600))
.await
}
pub async fn insert_addr_session(&self, key: SocketAddr, value: (String, u32)) {
pub async fn insert_addr_session(&self, key: SocketAddr, value: (String, u32, i64)) {
self.addr_session
.insert(key, value, Duration::from_secs(20))
.await
Expand Down

0 comments on commit 385ee5c

Please sign in to comment.