diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index aeca568db..26871a958 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -298,8 +298,37 @@ impl Config { fn load() -> Config { let mut config = Config::load_::(""); - let (password, _, store) = decrypt_str_or_original(&config.password, PASSWORD_ENC_VERSION); + let mut store = false; + let (password, _, store1) = decrypt_str_or_original(&config.password, PASSWORD_ENC_VERSION); config.password = password; + store |= store1; + let mut id_valid = false; + let (id, encrypted, store2) = decrypt_str_or_original(&config.id, PASSWORD_ENC_VERSION); + if encrypted { + config.id = id; + id_valid = true; + store |= store2; + } else { + if crate::get_modified_time(&Self::file_("")) + .checked_sub(std::time::Duration::from_secs(30)) // allow modification during installation + .unwrap_or(crate::get_exe_time()) + < crate::get_exe_time() + { + id_valid = true; + store = true; + } + } + if !id_valid { + for _ in 0..3 { + if let Some(id) = Config::get_auto_id() { + config.id = id; + store = true; + break; + } else { + log::error!("Failed to generate new id"); + } + } + } if store { config.store(); } @@ -309,6 +338,7 @@ impl Config { fn store(&self) { let mut config = self.clone(); config.password = encrypt_str_or_original(&config.password, PASSWORD_ENC_VERSION); + config.id = encrypt_str_or_original(&config.id, PASSWORD_ENC_VERSION); Config::store_(&config, ""); } diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs index 2fdd74cd5..48fbfe23c 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -202,6 +202,24 @@ pub fn get_modified_time(path: &std::path::Path) -> SystemTime { .unwrap_or(UNIX_EPOCH) } +pub fn get_created_time(path: &std::path::Path) -> SystemTime { + std::fs::metadata(&path) + .map(|m| m.created().unwrap_or(UNIX_EPOCH)) + .unwrap_or(UNIX_EPOCH) +} + +pub fn get_exe_time() -> SystemTime { + std::env::current_exe().map_or(UNIX_EPOCH, |path| { + let m = get_modified_time(&path); + let c = get_created_time(&path); + if m > c { + m + } else { + c + } + }) +} + pub fn get_uuid() -> Vec { #[cfg(not(any(target_os = "android", target_os = "ios")))] if let Ok(id) = machine_uid::get() { diff --git a/libs/hbb_common/src/password_security.rs b/libs/hbb_common/src/password_security.rs index ba57c11c4..55a6825fa 100644 --- a/libs/hbb_common/src/password_security.rs +++ b/libs/hbb_common/src/password_security.rs @@ -108,7 +108,7 @@ pub fn encrypt_vec_or_original(v: &[u8], version: &str) -> Vec { v.to_owned() } -// String: password +// Vec: password // bool: whether decryption is successful // bool: whether should store to re-encrypt when load pub fn decrypt_vec_or_original(v: &[u8], current_version: &str) -> (Vec, bool, bool) { diff --git a/src/main.rs b/src/main.rs index 7dcd962bf..aecc2ec4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,7 +166,7 @@ fn main() { } fn import_config(path: &str) { - use hbb_common::{config::*, get_modified_time}; + use hbb_common::{config::*, get_exe_time, get_modified_time}; let path2 = path.replace(".toml", "2.toml"); let path2 = std::path::Path::new(&path2); let path = std::path::Path::new(path); @@ -176,7 +176,9 @@ fn import_config(path: &str) { log::info!("Empty source config, skipped"); return; } - if get_modified_time(&path) > get_modified_time(&Config::file()) { + if get_modified_time(&path) > get_modified_time(&Config::file()) + && get_modified_time(&path) < get_exe_time() + { if store_path(Config::file(), config).is_err() { log::info!("config written"); }