changed peer's uuid from struct to outer

This commit is contained in:
asxalex 2024-02-21 10:50:07 +08:00
parent 7b7ae4b5f5
commit ecf99847eb
3 changed files with 74 additions and 21 deletions

View File

@ -9,3 +9,10 @@ pub const SDLAN_FLAGS_OPTIONS: u16 = 0x0080;
pub const IPV4_SIZE: u8 = 4; pub const IPV4_SIZE: u8 = 4;
pub const IPV6_SIZE: u8 = 16; pub const IPV6_SIZE: u8 = 16;
// add to LIST
pub enum SDLanSNOPER {
SDLanSNAdd,
SDLanSNAddSkip,
SDLanSNAdded,
}

View File

@ -1,4 +1,5 @@
#![allow(unused)] #![allow(unused)]
use super::utils::Result;
use dashmap::DashMap; use dashmap::DashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
@ -8,12 +9,13 @@ use std::sync::Mutex;
#[derive(Debug)] #[derive(Debug)]
pub struct Peer { pub struct Peer {
pub id: String, // pub id: Mutex<String>,
// 对端阶段的udp信息包括ipv4地址和子网掩码位数 // 对端阶段的udp信息包括ipv4地址和子网掩码位数
pub dev_addr: IpSubnet, pub dev_addr: IpSubnet,
// 对端对外开放的ip和端口信息 // 对端对外开放的ip和端口信息
pub sock: Mutex<SdlanSock>, pub sock: Mutex<SdlanSock>,
pub pub_key: Mutex<Vec<u8>>, pub pub_key: Mutex<Vec<u8>>,
pub timeout: isize, pub timeout: isize,
// 最近一次遇见 // 最近一次遇见
@ -22,15 +24,17 @@ pub struct Peer {
pub last_p2p: AtomicU64, pub last_p2p: AtomicU64,
// 最近一次发送query // 最近一次发送query
pub last_send_query: AtomicU64, pub last_send_query: AtomicU64,
// 最近一次合法时间
pub last_valid_timestamp: AtomicU64,
// 该节点锁属的网络 // 该节点锁属的网络
pub network_id: Mutex<String>, pub network_id: Mutex<String>,
} }
impl Peer { impl Peer {
pub fn new(id: &str) -> Self { pub fn new() -> Self {
Self { Self {
id: id.to_string(), // id: Mutex::new(id.to_string()),
dev_addr: IpSubnet::new(0, 0), dev_addr: IpSubnet::new(0, 0),
sock: Mutex::new(SdlanSock { sock: Mutex::new(SdlanSock {
family: 0, family: 0,
@ -45,6 +49,7 @@ impl Peer {
last_seen: AtomicU64::new(0), last_seen: AtomicU64::new(0),
last_p2p: AtomicU64::new(0), last_p2p: AtomicU64::new(0),
last_send_query: AtomicU64::new(0), last_send_query: AtomicU64::new(0),
last_valid_timestamp: AtomicU64::new(0),
network_id: Mutex::new(String::new()), network_id: Mutex::new(String::new()),
} }
} }
@ -132,39 +137,48 @@ pub struct SdlanSock {
use std::borrow::Cow; use std::borrow::Cow;
use std::sync::Arc; use std::sync::Arc;
use crate::utils::SDLanError;
pub struct PeerMap { pub struct PeerMap {
pub peers: DashMap<String, Arc<Peer>>, pub peers: DashMap<String, Arc<Peer>>,
// ip to peer's uuid, the ip is the logical ip // ip to peer's uuid, the ip is the logical ip
// of tun // of tun
pub peer_ip_map: DashMap<u32, String>, // pub peer_ip_map: DashMap<u32, String>,
/// peer's read only sdlansock
/// if a peer's sock changes, just remove // peer's read only sdlansock
/// and re-insert it. // if a peer's sock changes, just remove
pub peer_sock: DashMap<String, SdlanSock>, // and re-insert it.
// pub peer_sock: DashMap<String, SdlanSock>,
} }
impl PeerMap { impl PeerMap {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
peers: DashMap::new(), peers: DashMap::new(),
peer_ip_map: DashMap::new(), // peer_ip_map: DashMap::new(),
peer_sock: DashMap::new(), // peer_sock: DashMap::new(),
} }
} }
pub fn insert_peer(&self, data: Arc<Peer>) { pub fn insert_peer(&self, id: &str, data: Arc<Peer>) {
let ip = data.dev_addr.net_addr.load(Ordering::Relaxed); self.peers.insert(id.to_string(), data);
if ip != 0 {
let id = data.id.clone();
self.peer_ip_map.insert(ip, id);
}
self.peers.insert(data.id.clone(), data);
} }
pub fn peer_match<F: Fn(&Arc<Peer>) -> bool>(&self, id: &str, f: F) -> Option<Arc<Peer>> { 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<F: Fn(&Arc<Peer>) -> bool>(
&self,
id: &str,
f: F,
) -> Option<(String, Arc<Peer>)> {
if let Some(v) = self.peers.get(id) { if let Some(v) = self.peers.get(id) {
if f(&v) { if f(&v) {
return Some(v.clone()); return Some((v.key().to_owned(), v.clone()));
} }
} }
None None
@ -202,14 +216,14 @@ mod test {
fn test_peer_map() { fn test_peer_map() {
let id = gen_uuid(); let id = gen_uuid();
let pm = PeerMap::new(); let pm = PeerMap::new();
let mut p = Peer::new(&id); let mut p = Peer::new();
{ {
let mut sl = p.sock.lock().unwrap(); let mut sl = p.sock.lock().unwrap();
sl.family = 0; sl.family = 0;
sl.v4 = [1; 4]; sl.v4 = [1; 4];
sl.port = 20; 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) { if let Some(p2) = pm.find_peer_by_id(&id) {
let pl = p2.sock.lock().unwrap(); let pl = p2.sock.lock().unwrap();
assert_eq!(pl.family, 0); assert_eq!(pl.family, 0);

View File

@ -1,5 +1,7 @@
use dashmap::DashMap; use dashmap::DashMap;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::mpsc::UnboundedReceiver;
pub struct MyDashMap<T>(DashMap<String, Arc<T>>); pub struct MyDashMap<T>(DashMap<String, Arc<T>>);
@ -27,3 +29,33 @@ impl<T> MyDashMap<T> {
self.0.clear() 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<String> {
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);
}
}