This commit is contained in:
2025-09-17 13:06:18 -04:00
parent fcd377211a
commit bb8b7cc3fd
10 changed files with 201 additions and 101 deletions

View File

@@ -47,7 +47,7 @@ impl AppState {
pub fn config(&mut self, args: Arguments) {
// check for config file if it doesn't exist write default config
println!("Configuring..");
log::info!("Configuring..");
let cache_dir = get_cache_dir();
let config_dir = get_config_dir();
@@ -61,10 +61,10 @@ impl AppState {
if let Some(service_command) = args.service {
match service_command.as_str() {
"install" => {
println!("Installing..");
log::info!("Installing..");
setup_service();
if !Path::new(settings_file_path.as_str()).exists() {
println!("Settings.toml does not exist");
log::warn!("Settings.toml does not exist; writing default and exiting after service install");
crate::util::write_default_config(settings_file_path.clone(), webhook.clone());
exit(0);
}
@@ -78,26 +78,32 @@ impl AppState {
}
if !Path::new(settings_file_path.as_str()).exists() {
println!("Settings.toml does not exist");
log::warn!("Settings.toml does not exist; writing default");
write_default_config(settings_file_path.clone(), webhook);
}
let blacklist_file_path = format!("{}config/file_mon_blacklist", config_dir);
if !Path::new(blacklist_file_path.as_str()).exists() {
println!("file_mon_blacklist does not exist");
log::warn!("file_mon_blacklist does not exist; creating default");
write_default_blacklist(blacklist_file_path.clone());
}
let config = Config::builder();
let settings = config
let settings = match config
.add_source(config::File::with_name(settings_file_path.as_str()))
.build()
.expect("Configuration error: unable to parse settings");
{
Ok(s) => s,
Err(e) => {
log::error!("Configuration error: unable to parse settings: {}", e);
exit(2);
}
};
// initialize monitoring modules
let mut monitors: Vec<Monitors> = vec![];
if settings
.get::<String>("usb_mon_enabled")
.unwrap()
.unwrap_or("false".to_string())
.eq_ignore_ascii_case("true")
{
monitors.push(Monitors::USBMon(USBMon::new(settings.clone())));
@@ -111,14 +117,14 @@ impl AppState {
// }
if settings
.get::<String>("burn_file_mon_enabled")
.unwrap()
.unwrap_or("false".to_string())
.eq_ignore_ascii_case("true")
{
monitors.push(Monitors::SSHBurnMon(SSHBurnMon::new(settings.clone())));
}
if settings
.get::<String>("fs_mon_enabled")
.unwrap()
.unwrap_or("false".to_string())
.eq_ignore_ascii_case("true")
{
monitors.push(Monitors::FileChanges(FileChanges::new(
@@ -140,20 +146,18 @@ impl AppState {
}
pub async fn run(&mut self) {
let fs_check_tick = self
// Defaults if not configured or parse fails
let n_fs_check_tick: u64 = self
.settings_map
.get::<String>("fs_tick_delay_seconds")
.expect("tick_delay_seconds not found in Settings.toml");
let n_fs_check_tick = fs_check_tick
.parse::<u64>()
.expect("unable to parse fs_tick_delay_seconds");
let tick = self
.get::<String>("fs_tick_delay_seconds").ok()
.and_then(|v| match v.parse::<u64>() { Ok(n) => Some(n), Err(e) => { log::warn!("Invalid fs_tick_delay_seconds '{}': {}. Using default 300.", v, e); None } })
.unwrap_or(300);
let n_tick: u64 = self
.settings_map
.get::<String>("tick_delay_seconds")
.expect("tick_delay_seconds not found in Settings.toml");
let n_tick = tick
.parse::<u64>()
.expect("unable to parse tick_delay_seconds");
.get::<String>("tick_delay_seconds").ok()
.and_then(|v| match v.parse::<u64>() { Ok(n) => Some(n), Err(e) => { log::warn!("Invalid tick_delay_seconds '{}': {}. Using default 5.", v, e); None } })
.unwrap_or(5);
let mut last = SystemTime::now();
loop {
let mut binding = self.monitors.lock();
@@ -161,34 +165,33 @@ impl AppState {
for i in bind.iter_mut() {
match i {
Monitors::USBMon(e) => {
// println!("{:#?}", e);
e.check().await;
}
// Monitors::NetMon(e) => {
// // println!("{:#?}", e);
// e.check().await;
// }
Monitors::SSHBurnMon(e) => {
// println!("{:#?}", e);
e.check().await;
}
Monitors::FileChanges(e) => {
// println!("{:#?}", e);
let now = SystemTime::now();
let dur_since = now.duration_since(last).unwrap();
let dur_since = match now.duration_since(last) {
Ok(d) => d,
Err(e) => {
log::warn!("SystemTime went backwards: {}", e);
Duration::new(0, 0)
}
};
if dur_since.as_secs() > n_fs_check_tick {
// println!("fs_tick");
last = SystemTime::now();
e.check().await;
}
}
Monitors::NewClients(e) => {
// println!("{:#?}", e);
e.check().await;
}
}
tokio::time::sleep(Duration::new(n_tick, 0)).await;
// println!("tick");
}
}
}