From cb251fba8ad6509a64fa497540e8afab99b30c0c Mon Sep 17 00:00:00 2001 From: asxalex Date: Sun, 18 Feb 2024 21:19:10 +0800 Subject: [PATCH] parse subnet --- Cargo.toml | 1 + src/bin/sdlan-sn/utils/sn.rs | 51 +++++++++++++++++++++++++++++------- src/utils/mod.rs | 6 ++++- src/utils/myrsa.rs | 4 +-- src/utils/myuuid.rs | 5 ++++ 5 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 src/utils/myuuid.rs diff --git a/Cargo.toml b/Cargo.toml index 2524347..a3187f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ serde_repr = "0.1.18" tokio = { version = "1.36.0", features = ["full"] } tracing = "0.1.40" tracing-appender = "0.2.3" +uuid = { version = "1.7.0", features = ["v4"] } diff --git a/src/bin/sdlan-sn/utils/sn.rs b/src/bin/sdlan-sn/utils/sn.rs index 9c93616..642414f 100644 --- a/src/bin/sdlan-sn/utils/sn.rs +++ b/src/bin/sdlan-sn/utils/sn.rs @@ -1,8 +1,12 @@ -use dashmap::DashMap; +use dashmap::{DashMap, DashSet}; use sdlan_sn_rs::peer::IpSubnet; +use sdlan_sn_rs::utils::gen_uuid; use sdlan_sn_rs::utils::MyDashMap; use tokio::net::UdpSocket; +use crate::config; +use std::net::Ipv4Addr; + use std::{ sync::atomic::{AtomicU32, AtomicU8}, time::{SystemTime, UNIX_EPOCH}, @@ -65,21 +69,29 @@ impl SuperNode { .expect("time went backwards") .as_secs(); + let startip: u32 = config::DEFAULT_MIN_AUTO_IP_NET + .parse::() + .unwrap() + .into(); + let endip: u32 = config::DEFAULT_MAX_AUTO_IP_NET + .parse::() + .unwrap() + .into(); Self { start_time, daemon: false, local_port: 7655, networks: MyDashMap::new(), - fedration: Network::new("*fedration"), - pending: Network::new("*pending"), + fedration: Network::new("*fedration", gen_uuid(), true, 0, 0), + pending: Network::new("*pending", gen_uuid(), false, 0, 0), ip_range: AutoIpAssign { start_ip: IpSubnet { - net_addr: AtomicU32::new(0), - net_bit_len: AtomicU8::new(0), + net_addr: AtomicU32::new(startip), + net_bit_len: AtomicU8::new(config::DEFAULT_IP_NET_BIT_LEN), }, end_ip: IpSubnet { - net_addr: AtomicU32::new(0), - net_bit_len: AtomicU8::new(0), + net_addr: AtomicU32::new(endip), + net_bit_len: AtomicU8::new(config::DEFAULT_IP_NET_BIT_LEN), }, }, sock, @@ -94,13 +106,34 @@ pub struct AutoIpAssign { } pub struct Network { - name: String, + pub id: String, + pub name: String, + pub is_fedration: bool, + pub enabled: bool, + pub network_pass: String, + pub header_pass: String, + pub auto_ip_net: IpSubnet, + // 这个网络下面的节点,ip到peer uuid + // pub edges: DashMap, + pub edges: DashSet, + pub ip_to_edge_id: DashMap, } impl Network { - fn new(name: &str) -> Self { + fn new(name: &str, id: String, is_fedration: bool, ipnet: u32, ipnet_bitlen: u8) -> Self { Self { name: name.to_string(), + id, + is_fedration, + enabled: true, + network_pass: gen_uuid(), + header_pass: gen_uuid(), + auto_ip_net: IpSubnet { + net_addr: AtomicU32::new(ipnet), + net_bit_len: AtomicU8::new(ipnet_bitlen), + }, + edges: DashSet::new(), + ip_to_edge_id: DashMap::new(), } } } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index eaa223e..381bf20 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,12 +3,16 @@ mod error; mod helper; mod myaes; mod myrsa; +mod myuuid; pub use encode_decode::*; pub use error::*; pub use helper::*; pub use myaes::{aes_decrypt, aes_encrypt}; -pub use myrsa::{gen_keys, load_private_key_file, load_public_key_file, rsa_decrypt, rsa_encrypt}; +pub use myrsa::{ + gen_rsa_keys, load_private_key_file, load_public_key_file, rsa_decrypt, rsa_encrypt, +}; +pub use myuuid::*; #[cfg(test)] pub mod test_utils { diff --git a/src/utils/myrsa.rs b/src/utils/myrsa.rs index d351016..511153a 100644 --- a/src/utils/myrsa.rs +++ b/src/utils/myrsa.rs @@ -9,7 +9,7 @@ use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey}; use std::io::Read; use std::sync::Arc; -pub fn gen_keys() { +pub fn gen_rsa_keys() { let mut rng = rand::thread_rng(); let bits = 2048; let priv_key = RsaPrivateKey::new(&mut rng, bits).unwrap(); @@ -103,7 +103,7 @@ mod tests { if std::fs::File::open(".data/id_rsa").is_ok() { return; } - gen_keys(); + gen_rsa_keys(); } #[test] diff --git a/src/utils/myuuid.rs b/src/utils/myuuid.rs new file mode 100644 index 0000000..a3e5e2f --- /dev/null +++ b/src/utils/myuuid.rs @@ -0,0 +1,5 @@ +use uuid::Uuid; + +pub fn gen_uuid() -> String { + format!("{:x}", Uuid::new_v4().as_u128()) +}