adding infra to load settings from file
This commit is contained in:
106
client/protector/Cargo.lock
generated
106
client/protector/Cargo.lock
generated
@@ -2,6 +2,112 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.21.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.103"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "protector"
|
name = "protector"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.41"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.145"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.108"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
once_cell = "1.19"
|
||||||
|
serde = "1.0"
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
@@ -1,3 +1,28 @@
|
|||||||
|
//protector client
|
||||||
|
|
||||||
|
#![allow(unused_parens)] // do not warn about unused parentheses
|
||||||
|
|
||||||
|
mod settings;
|
||||||
|
use settings::settings as set;
|
||||||
|
|
||||||
|
//const SETTINGS_FILE: &str = "/etc/protector/conf/protector.conf";
|
||||||
|
const DEBUG: bool = true;
|
||||||
|
//only debug
|
||||||
|
const SETTINGS_FILE: &str = "protector.conf";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Starting Protector Client");
|
||||||
|
println!("Loading settings from conf file");
|
||||||
|
if(!set::load_settings(SETTINGS_FILE)){
|
||||||
|
println!("error reading config file\nexiting...");
|
||||||
|
std::process::exit(3);
|
||||||
|
}
|
||||||
|
if(DEBUG){
|
||||||
|
let b = set::get_settings_value_str("server_ip");
|
||||||
|
let c = set::get_settings_value_int("server_port");
|
||||||
|
let d = set::get_settings_value_str("server_secret");
|
||||||
|
let e = set::get_settings_value_str("client_secret");
|
||||||
|
println!("Server ip is {} and port is {}.\nAnd server_secret is {} and client_secret is {}",b,c,d,e);
|
||||||
|
}
|
||||||
|
println!("Finished, exiting...");
|
||||||
}
|
}
|
||||||
|
|||||||
1
client/protector/src/settings/mod.rs
Normal file
1
client/protector/src/settings/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod settings;
|
||||||
76
client/protector/src/settings/settings.rs
Normal file
76
client/protector/src/settings/settings.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::sync::RwLock;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
pub struct Settings {
|
||||||
|
pub server_ip: String,
|
||||||
|
pub server_port: i64,
|
||||||
|
pub client_secret: String,
|
||||||
|
pub server_secret: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub static SETTINGS: Lazy<RwLock<Settings>> = Lazy::new(|| {
|
||||||
|
RwLock::new(Settings {
|
||||||
|
server_ip: String::new(),
|
||||||
|
server_port: 0,
|
||||||
|
client_secret: String::new(),
|
||||||
|
server_secret: String::new(),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn load_settings(path: &str) -> bool {
|
||||||
|
let contents = match fs::read_to_string(path) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(_) => return false, //file error
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut settings = SETTINGS.write().unwrap();
|
||||||
|
|
||||||
|
for line in contents.lines() {
|
||||||
|
let line = line.trim();
|
||||||
|
if (line.is_empty() || line.starts_with('#') || line.starts_with("//") || line.starts_with("[")) {
|
||||||
|
continue; //skip comments
|
||||||
|
}
|
||||||
|
|
||||||
|
let (key, value) = match line.split_once(':') {
|
||||||
|
Some(kv) => kv,
|
||||||
|
None => return false, // malformed line, should be name: value
|
||||||
|
};
|
||||||
|
|
||||||
|
let key = key.trim();
|
||||||
|
let value = value.trim();
|
||||||
|
|
||||||
|
match key { //can be used with a lot more settings key&values
|
||||||
|
"server_ip" => settings.server_ip = value.to_string(),
|
||||||
|
"client_secret" => settings.client_secret = value.to_string(),
|
||||||
|
"server_secret" => settings.server_secret = value.to_string(),
|
||||||
|
"server_port" => {
|
||||||
|
match value.parse::<i64>() {
|
||||||
|
Ok(port) => settings.server_port = port,
|
||||||
|
Err(_) => return false, // invalid port number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => continue, //return false, // unknown key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings_value_int(name: &str) -> i64 {
|
||||||
|
let settings = SETTINGS.read().unwrap();
|
||||||
|
match name{
|
||||||
|
"server_port" => return settings.server_port,
|
||||||
|
_ => return 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings_value_str(name: &str) -> String {
|
||||||
|
let settings = SETTINGS.read().unwrap();
|
||||||
|
match name{
|
||||||
|
"server_ip" => return settings.server_ip.clone(),
|
||||||
|
"server_secret" => return settings.server_secret.clone(),
|
||||||
|
"client_secret" => return settings.client_secret.clone(),
|
||||||
|
_ => return "err_not_found".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user