From ecf99847ebd3e313732423fdcf9da2fd9cf682e4 Mon Sep 17 00:00:00 2001 From: asxalex Date: Wed, 21 Feb 2024 10:50:07 +0800 Subject: [PATCH] changed peer's uuid from struct to outer --- src/config.rs | 7 ++++++ src/peer.rs | 56 ++++++++++++++++++++++++++++----------------- src/utils/helper.rs | 32 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4ee5927..02999fe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,3 +9,10 @@ pub const SDLAN_FLAGS_OPTIONS: u16 = 0x0080; pub const IPV4_SIZE: u8 = 4; pub const IPV6_SIZE: u8 = 16; + +// add to LIST +pub enum SDLanSNOPER { + SDLanSNAdd, + SDLanSNAddSkip, + SDLanSNAdded, +} diff --git a/src/peer.rs b/src/peer.rs index 13dcff3..d11ce7d 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -1,4 +1,5 @@ #![allow(unused)] +use super::utils::Result; use dashmap::DashMap; use serde::{Deserialize, Serialize}; use sqlx::prelude::FromRow; @@ -8,12 +9,13 @@ use std::sync::Mutex; #[derive(Debug)] pub struct Peer { - pub id: String, + // pub id: Mutex, // 对端阶段的udp信息,包括ipv4地址和子网掩码位数 pub dev_addr: IpSubnet, // 对端对外开放的ip和端口信息 pub sock: Mutex, pub pub_key: Mutex>, + pub timeout: isize, // 最近一次遇见 @@ -22,15 +24,17 @@ pub struct Peer { pub last_p2p: AtomicU64, // 最近一次发送query pub last_send_query: AtomicU64, + // 最近一次合法时间 + pub last_valid_timestamp: AtomicU64, // 该节点锁属的网络 pub network_id: Mutex, } impl Peer { - pub fn new(id: &str) -> Self { + pub fn new() -> Self { Self { - id: id.to_string(), + // id: Mutex::new(id.to_string()), dev_addr: IpSubnet::new(0, 0), sock: Mutex::new(SdlanSock { family: 0, @@ -45,6 +49,7 @@ impl Peer { last_seen: AtomicU64::new(0), last_p2p: AtomicU64::new(0), last_send_query: AtomicU64::new(0), + last_valid_timestamp: AtomicU64::new(0), network_id: Mutex::new(String::new()), } } @@ -132,39 +137,48 @@ pub struct SdlanSock { use std::borrow::Cow; use std::sync::Arc; + +use crate::utils::SDLanError; pub struct PeerMap { pub peers: DashMap>, // ip to peer's uuid, the ip is the logical ip // of tun - pub peer_ip_map: DashMap, - /// peer's read only sdlansock - /// if a peer's sock changes, just remove - /// and re-insert it. - pub peer_sock: DashMap, + // pub peer_ip_map: DashMap, + + // peer's read only sdlansock + // if a peer's sock changes, just remove + // and re-insert it. + // pub peer_sock: DashMap, } impl PeerMap { pub fn new() -> Self { Self { peers: DashMap::new(), - peer_ip_map: DashMap::new(), - peer_sock: DashMap::new(), + // peer_ip_map: DashMap::new(), + // peer_sock: DashMap::new(), } } - pub fn insert_peer(&self, data: Arc) { - let ip = data.dev_addr.net_addr.load(Ordering::Relaxed); - if ip != 0 { - let id = data.id.clone(); - self.peer_ip_map.insert(ip, id); - } - self.peers.insert(data.id.clone(), data); + pub fn insert_peer(&self, id: &str, data: Arc) { + self.peers.insert(id.to_string(), data); } - pub fn peer_match) -> bool>(&self, id: &str, f: F) -> Option> { + pub fn peer_id_change(&self, oldid: &str, newid: &str) { + if let Some((_, v)) = self.peers.remove(oldid) { + // *v.id.lock().unwrap() = newid.to_string(); + self.peers.insert(newid.to_string(), v); + } + } + + pub fn peer_match) -> bool>( + &self, + id: &str, + f: F, + ) -> Option<(String, Arc)> { if let Some(v) = self.peers.get(id) { if f(&v) { - return Some(v.clone()); + return Some((v.key().to_owned(), v.clone())); } } None @@ -202,14 +216,14 @@ mod test { fn test_peer_map() { let id = gen_uuid(); let pm = PeerMap::new(); - let mut p = Peer::new(&id); + let mut p = Peer::new(); { let mut sl = p.sock.lock().unwrap(); sl.family = 0; sl.v4 = [1; 4]; sl.port = 20; } - pm.insert_peer(Arc::new(p)); + pm.insert_peer(&id, Arc::new(p)); if let Some(p2) = pm.find_peer_by_id(&id) { let pl = p2.sock.lock().unwrap(); assert_eq!(pl.family, 0); diff --git a/src/utils/helper.rs b/src/utils/helper.rs index a1424bb..cc664f8 100644 --- a/src/utils/helper.rs +++ b/src/utils/helper.rs @@ -1,5 +1,7 @@ use dashmap::DashMap; +use std::path::Path; use std::sync::Arc; +use tokio::sync::mpsc::UnboundedReceiver; pub struct MyDashMap(DashMap>); @@ -27,3 +29,33 @@ impl MyDashMap { self.0.clear() } } + +use std::time::{SystemTime, UNIX_EPOCH}; +pub fn get_current_timestamp() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() +} + +use super::{gen_uuid, Result}; +use std::fs::{File, OpenOptions}; +use std::io::{Read, Write}; + +pub fn create_or_load_uuid() -> Result { + let filepath = Path::new("./.id"); + if filepath.exists() { + let mut result = String::new(); + File::open(filepath)?.read_to_string(&mut result)?; + let result = result.trim().to_string(); + return Ok(result); + } else { + let uuid = gen_uuid(); + OpenOptions::new() + .create(true) + .write(true) + .open(filepath)? + .write_all(uuid.as_bytes())?; + return Ok(uuid); + } +}