| 1 | use std::{fs, io::Write, path::PathBuf}; |
| 2 | |
| 3 | use common::config::Config; |
| 4 | use tracing::warn; |
| 5 | |
| 6 | pub mod accounts; |
| 7 | pub mod autostart; |
| 8 | pub mod avatar; |
| 9 | pub mod embed; |
| 10 | pub mod emoji; |
| 11 | pub mod gui; |
| 12 | pub mod instance; |
| 13 | pub mod lang; |
| 14 | pub mod logo; |
| 15 | pub mod marks; |
| 16 | pub mod media; |
| 17 | pub mod net; |
| 18 | pub mod release; |
| 19 | pub mod settings; |
| 20 | pub mod share; |
| 21 | pub mod tray; |
| 22 | pub mod update; |
| 23 | pub use usage; |
| 24 | pub mod voice; |
| 25 | |
| 26 | pub const NAME: &str = "Yumush"; |
| 27 | pub const VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 28 | pub const COMMIT: &str = env!("YUMUSH_COMMIT"); |
| 29 | pub const TARGET: &str = env!("YUMUSH_TARGET"); |
| 30 | |
| 31 | static EXE: std::sync::LazyLock<Option<PathBuf>> = |
| 32 | std::sync::LazyLock::new(|| std::env::current_exe().ok()); |
| 33 | |
| 34 | pub fn exe() -> Option<&'static std::path::Path> { |
| 35 | EXE.as_deref() |
| 36 | } |
| 37 | |
| 38 | pub fn config_dir() -> Option<PathBuf> { |
| 39 | std::env::var_os("YUMUSH_CONFIG_DIR") |
| 40 | .map(PathBuf::from) |
| 41 | .or_else(|| dirs::config_dir().map(|dir| dir.join("yumush"))) |
| 42 | } |
| 43 | |
| 44 | fn config_file(name: &str) -> Option<PathBuf> { |
| 45 | config_dir().map(|dir| dir.join(name)) |
| 46 | } |
| 47 | |
| 48 | pub fn read_config(name: &str) -> Option<String> { |
| 49 | fs::read_to_string(config_file(name)?).ok() |
| 50 | } |
| 51 | |
| 52 | pub fn read_config_bytes(name: &str) -> Option<Vec<u8>> { |
| 53 | fs::read(config_file(name)?).ok() |
| 54 | } |
| 55 | |
| 56 | pub fn remove_config(name: &str) { |
| 57 | if let Some(path) = config_file(name) { |
| 58 | let _ = fs::remove_file(path); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn write_config(name: &str, content: impl AsRef<[u8]>) { |
| 63 | let Some(path) = config_file(name) else { |
| 64 | return; |
| 65 | }; |
| 66 | let result = path |
| 67 | .parent() |
| 68 | .map_or(Ok(()), fs::create_dir_all) |
| 69 | .and_then(|()| { |
| 70 | let mut options = fs::OpenOptions::new(); |
| 71 | options.write(true).create(true).truncate(true); |
| 72 | #[cfg(unix)] |
| 73 | std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); |
| 74 | options.open(&path)?.write_all(content.as_ref()) |
| 75 | }); |
| 76 | |
| 77 | if let Err(error) = result { |
| 78 | warn!("save {name} failed: {error}"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const CLIENT_CONFIG_FILE_CONTENT: &str = include_str!(concat!( |
| 83 | env!("CARGO_MANIFEST_DIR"), |
| 84 | "/../../configs/client_config.toml" |
| 85 | )); |
| 86 | |
| 87 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 88 | pub struct Server { |
| 89 | pub address: String, |
| 90 | pub any_cert: bool, |
| 91 | } |
| 92 | |
| 93 | impl Server { |
| 94 | pub fn load() -> Self { |
| 95 | let saved = read_config("server"); |
| 96 | let mut lines = saved.as_deref().unwrap_or_default().lines().map(str::trim); |
| 97 | let saved_address = lines.next(); |
| 98 | let address = std::env::var("YUMUSH_SERVER_ADDRESS") |
| 99 | .ok() |
| 100 | .or_else(|| saved_address.map(str::to_string)) |
| 101 | .filter(|address| !address.is_empty()) |
| 102 | .unwrap_or_else(|| { |
| 103 | Config::parse(CLIENT_CONFIG_FILE_CONTENT) |
| 104 | .and_then(|config| config.get("server_address")) |
| 105 | .expect("client config server_address") |
| 106 | }); |
| 107 | let any_cert = |
| 108 | std::env::var_os("YUMUSH_ANY_CERT").is_some() || lines.next() == Some("any_cert"); |
| 109 | Self { address, any_cert } |
| 110 | } |
| 111 | |
| 112 | pub fn save(&self) { |
| 113 | let flag = if self.any_cert { "any_cert" } else { "verify" }; |
| 114 | write_config("server", format!("{}\n{flag}\n", self.address)); |
| 115 | } |
| 116 | |
| 117 | pub fn host(&self) -> &str { |
| 118 | match self.address.rsplit_once(':') { |
| 119 | Some((host, port)) if !host.ends_with(':') && port.parse::<u16>().is_ok() => { |
| 120 | host.trim_start_matches('[').trim_end_matches(']') |
| 121 | } |
| 122 | _ => &self.address, |
| 123 | } |
| 124 | } |
| 125 | } |