// Admin locks for the permission switches, following how managed policies are delivered: // - macOS: user defaults (`systemPreferences.getUserDefault(key, "string")`), which MDM profiles // can force; Linux: a JSON file in the home folder; Windows: the native managed-policy lookup. // - A missing or blank value = "unmanaged"; a valid value = "pinned"; anything else = "invalid", // which managed-policy readers refuse to use. Here an invalid value locks the switch off. use std::process::{Command, Stdio}; /// The macOS defaults domain AnyBot reads (`defaults write com.anybot.AnyBot AllowDirectMode false`). pub const DOMAIN: &str = "com.anybot.AnyBot"; pub const KEY_DIRECT: &str = "AllowDirectMode"; pub const KEY_LOCAL: &str = "AllowLocalExecution"; /// 「網路流量改走你的電腦」 (the original lets admins turn it off). pub const KEY_EGRESS: &str = "AllowBoxTrafficViaThisMac"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Policy { Unmanaged, Pinned(bool), Invalid, } impl Policy { /// May the person turn this on? pub fn allows(self) -> bool { match self { Policy::Unmanaged => true, Policy::Pinned(b) => b, Policy::Invalid => false, } } } /// Blank or missing → unmanaged (the original's `mZ`); true/false words → pinned; else invalid. pub fn parse(v: Option<&str>) -> Policy { match v.map(|s| s.trim().to_ascii_lowercase()) { None => Policy::Unmanaged, Some(s) if s.is_empty() => Policy::Unmanaged, Some(s) if ["true", "1", "yes"].contains(&s.as_str()) => Policy::Pinned(true), Some(s) if ["false", "0", "no"].contains(&s.as_str()) => Policy::Pinned(false), Some(_) => Policy::Invalid, } } fn read_raw(key: &str) -> Option { if cfg!(target_os = "macos") { let out = Command::new("defaults").args(["read", DOMAIN, key]).stderr(Stdio::null()).output().ok()?; out.status.success().then(|| String::from_utf8_lossy(&out.stdout).trim().to_string()) } else { let path = dirs::home_dir()?.join(".anybot").join("policy.json"); let text = std::fs::read_to_string(path).ok()?; let v: serde_json::Value = serde_json::from_str(&text).ok()?; v.get(key).map(|x| x.as_str().map(str::to_string).unwrap_or_else(|| x.to_string())) } } #[derive(Clone, Copy, Debug)] pub struct Managed { pub direct: Policy, pub local: Policy, pub egress: Policy, } impl Default for Managed { fn default() -> Self { Managed { direct: Policy::Unmanaged, local: Policy::Unmanaged, egress: Policy::Unmanaged } } } /// Just the traffic switch (checked when a command starts). pub fn read_egress() -> Policy { parse(read_raw(KEY_EGRESS).as_deref()) } pub fn read() -> Managed { Managed { direct: parse(read_raw(KEY_DIRECT).as_deref()), local: parse(read_raw(KEY_LOCAL).as_deref()), egress: parse(read_raw(KEY_EGRESS).as_deref()) } } #[cfg(test)] mod tests { use super::*; #[test] fn values_follow_the_original_kinds() { assert_eq!(parse(None), Policy::Unmanaged); assert_eq!(parse(Some(" ")), Policy::Unmanaged); assert_eq!(parse(Some("0")), Policy::Pinned(false)); assert_eq!(parse(Some("TRUE")), Policy::Pinned(true)); assert_eq!(parse(Some("maybe")), Policy::Invalid); assert!(Policy::Unmanaged.allows() && Policy::Pinned(true).allows()); assert!(!Policy::Pinned(false).allows() && !Policy::Invalid.allows()); } }