69 lines
1.9 KiB
Rust
69 lines
1.9 KiB
Rust
use std::{collections::HashMap, path::Path, sync::atomic::AtomicBool};
|
|
|
|
use sdlan_sn_rs::utils::{Mac, Result};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{ExitNodeInfo, generate_mac_address, get_base_dir};
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub struct ExitNodeConfiguration {
|
|
pub in_use: bool,
|
|
pub node_id: u32,
|
|
pub node_name: String,
|
|
pub gateway: String,
|
|
pub target_network: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct LocalConfigInfo {
|
|
pub allow_routing: Option<bool>,
|
|
pub mac: Option<[u8; 6]>,
|
|
pub exit_node: HashMap<u32, ExitNodeConfiguration>,
|
|
}
|
|
|
|
pub fn store_configuration(config: &LocalConfigInfo) -> Result<()> {
|
|
let path = format!("{}/.config", get_base_dir());
|
|
let data = serde_json::to_string(config)?;
|
|
std::fs::write(&path, data)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn load_or_create_new_mac() -> Mac {
|
|
let path = format!("{}/.mac", get_base_dir());
|
|
let mut mac = None;
|
|
if let Ok(content) = std::fs::read(&path) {
|
|
if content.len() == 6 {
|
|
let mut mac_slice = [0; 6];
|
|
mac_slice.copy_from_slice(&content);
|
|
mac = Some(mac_slice);
|
|
}
|
|
}
|
|
let _ = std::fs::remove_file(&path);
|
|
if let None = mac {
|
|
mac = Some(generate_mac_address());
|
|
}
|
|
mac.unwrap()
|
|
}
|
|
|
|
pub fn load_configuration() -> LocalConfigInfo {
|
|
let path = format!("{}/.config", get_base_dir());
|
|
if let Ok(content) = std::fs::read(&path) {
|
|
if let Ok(mut config) = serde_json::from_slice::<LocalConfigInfo>(&content) {
|
|
if config.mac.is_none() {
|
|
config.mac = Some(load_or_create_new_mac());
|
|
let _ = store_configuration(&config);
|
|
}
|
|
return config;
|
|
}
|
|
}
|
|
|
|
let mac = Some(load_or_create_new_mac());
|
|
let config = LocalConfigInfo {
|
|
allow_routing: Some(false),
|
|
mac,
|
|
exit_node: HashMap::new(),
|
|
};
|
|
let _ = store_configuration(&config);
|
|
config
|
|
}
|