parse subnet

This commit is contained in:
asxalex 2024-02-18 21:19:10 +08:00
parent 9700539833
commit cb251fba8a
5 changed files with 55 additions and 12 deletions

View File

@ -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"] }

View File

@ -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::<Ipv4Addr>()
.unwrap()
.into();
let endip: u32 = config::DEFAULT_MAX_AUTO_IP_NET
.parse::<Ipv4Addr>()
.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<u32, String>,
pub edges: DashSet<String>,
pub ip_to_edge_id: DashMap<u32, String>,
}
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(),
}
}
}

View File

@ -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 {

View File

@ -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]

5
src/utils/myuuid.rs Normal file
View File

@ -0,0 +1,5 @@
use uuid::Uuid;
pub fn gen_uuid() -> String {
format!("{:x}", Uuid::new_v4().as_u128())
}