|
| 1 | +use std::{error::Error, fs, path::PathBuf}; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use spdlog::warn; |
| 5 | + |
| 6 | +#[derive(Debug, Serialize, Deserialize)] |
| 7 | +#[serde(default)] |
| 8 | +pub struct ListenForegroundEvents { |
| 9 | + pub enabled: bool, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Serialize, Deserialize, Default)] |
| 13 | +#[serde(rename_all = "snake_case")] |
| 14 | +pub enum ListenNewProcessMode { |
| 15 | + #[default] |
| 16 | + Normal, |
| 17 | + BlacklistOnly, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Serialize, Deserialize)] |
| 21 | +#[serde(default)] |
| 22 | +pub struct ListenNewProcess { |
| 23 | + pub enabled: bool, |
| 24 | + pub mode: ListenNewProcessMode, |
| 25 | + pub blacklist: Vec<String>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Serialize, Deserialize)] |
| 29 | +#[serde(default)] |
| 30 | +pub struct Config { |
| 31 | + pub listen_new_process: ListenNewProcess, |
| 32 | + pub listen_foreground_events: ListenForegroundEvents, |
| 33 | + pub throttle_all_startup: bool, |
| 34 | + pub system_process: bool, |
| 35 | + pub whitelist: Vec<String>, |
| 36 | +} |
| 37 | + |
| 38 | +impl Config { |
| 39 | + pub fn from_profile() -> Result<Self, Box<dyn Error + Send + Sync>> { |
| 40 | + let config_dir = directories::ProjectDirs::from("io", "RustyStarX", "RustyStar") |
| 41 | + .map(|d| d.config_dir().to_path_buf()) |
| 42 | + .unwrap_or(PathBuf::from(".")); |
| 43 | + fs::create_dir_all(&config_dir)?; |
| 44 | + |
| 45 | + let config_path = config_dir.join("config.toml"); |
| 46 | + if config_path.exists() { |
| 47 | + Ok(toml::from_str(&fs::read_to_string(&config_path)?)?) |
| 48 | + } else { |
| 49 | + warn!("config not existing! falling back to default..."); |
| 50 | + let config = Self::default(); |
| 51 | + let serialized = toml::to_string_pretty(&config)?; |
| 52 | + _ = fs::write(config_path, serialized).inspect_err(|e| { |
| 53 | + warn!("failed to write default config: {e}"); |
| 54 | + }); |
| 55 | + Ok(config) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl Default for Config { |
| 61 | + fn default() -> Self { |
| 62 | + Self { |
| 63 | + listen_new_process: ListenNewProcess::default(), |
| 64 | + listen_foreground_events: ListenForegroundEvents::default(), |
| 65 | + throttle_all_startup: true, |
| 66 | + system_process: true, |
| 67 | + whitelist: [ |
| 68 | + // ourself |
| 69 | + "RustyStar.exe", |
| 70 | + // System processes |
| 71 | + "explorer.exe", |
| 72 | + // Windows Manager of Windows |
| 73 | + "dwm.exe", |
| 74 | + // CSRSS core process |
| 75 | + "csrss.exe", |
| 76 | + // Windows services process |
| 77 | + "svchost.exe", |
| 78 | + // Task Manager |
| 79 | + "Taskmgr.exe", |
| 80 | + // Session Manager Subsystem |
| 81 | + "smss.exe", |
| 82 | + // Chinese input method |
| 83 | + "ChsIME.exe", |
| 84 | + // Speech-To-Text, Screen keyboard, handwrite input, e.g. |
| 85 | + "ctfmon.exe", |
| 86 | + // Windows User Mode Driver Framework |
| 87 | + "WUDFRd.exe", |
| 88 | + "WUDFHost.exe", |
| 89 | + // Edge is energy aware |
| 90 | + "msedge.exe", |
| 91 | + // UWP special handle |
| 92 | + "ApplicationFrameHost.exe", |
| 93 | + // system itself |
| 94 | + "[System Process]", |
| 95 | + "System", |
| 96 | + "Registry", |
| 97 | + // parent of "services.exe" |
| 98 | + "wininit.exe", |
| 99 | + // parent of "svchost.exe", "wudfhost.exe", e.g. |
| 100 | + "services.exe", |
| 101 | + // Local Security Authority Subsystem Service |
| 102 | + "lsass.exe", |
| 103 | + // part of the Windows Security Center, |
| 104 | + // responsible for monitoring and reporting the security status of your system |
| 105 | + "SecurityHealthService.exe", |
| 106 | + ] |
| 107 | + .map(str::to_string) |
| 108 | + .to_vec(), |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Default for ListenForegroundEvents { |
| 114 | + fn default() -> Self { |
| 115 | + Self { enabled: true } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl Default for ListenNewProcess { |
| 120 | + fn default() -> Self { |
| 121 | + Self { |
| 122 | + enabled: true, |
| 123 | + mode: ListenNewProcessMode::default(), |
| 124 | + blacklist: vec![], |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments