diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index 3767b1b83..6697c3320 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -584,15 +584,16 @@ impl Config { config.store(); } - pub fn get_sk_uuid() -> Vec { - // for uuid, avoid deadlock + // * Manually make sure no gen_keypair more than once + // for uuid, avoid deadlock + pub fn get_key_pair_without_lock() -> (Vec, Vec) { let mut config = Config::load_::(""); if config.key_pair.0.is_empty() { let (pk, sk) = sign::gen_keypair(); config.key_pair = (sk.0.to_vec(), pk.0.into()); Config::store_(&config, ""); } - config.key_pair.1 + config.key_pair.clone() } pub fn get_key_pair() -> (Vec, Vec) { diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs index 3ae95c83e..a9f25d31b 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -209,9 +209,16 @@ pub fn get_modified_time(path: &std::path::Path) -> SystemTime { fn gen_uuid() -> Vec { #[cfg(not(any(target_os = "android", target_os = "ios")))] if let Ok(id) = machine_uid::get() { - return id.into(); + id.into() + } else { + Config::get_key_pair().1 } - Config::get_sk_uuid() + #[cfg(any(target_os = "android", target_os = "ios"))] + Config::get_key_pair_without_lock().1 +} + +pub fn init_uuid() { + let _ = *UUID; } pub fn get_uuid() -> Vec { diff --git a/src/mobile_ffi.rs b/src/mobile_ffi.rs index 34eb9bdb8..cf3405a06 100644 --- a/src/mobile_ffi.rs +++ b/src/mobile_ffi.rs @@ -3,7 +3,7 @@ use crate::mobile::connection_manager::{self, get_clients_length, get_clients_st use crate::mobile::{self, Session}; use crate::common::{make_fd_to_json}; use flutter_rust_bridge::{StreamSink, ZeroCopyBuffer}; -use hbb_common::ResultType; +use hbb_common::{ResultType, init_uuid}; use hbb_common::{ config::{self, Config, LocalConfig, PeerConfig, ONLINE}, fs, log, @@ -16,7 +16,6 @@ use std::{ }; fn initialize(app_dir: &str) { - *config::APP_DIR.write().unwrap() = app_dir.to_owned(); #[cfg(target_os = "android")] { android_logger::init_once( @@ -30,6 +29,8 @@ fn initialize(app_dir: &str) { use hbb_common::env_logger::*; init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "debug")); } + *config::APP_DIR.write().unwrap() = app_dir.to_owned(); + init_uuid(); crate::common::test_rendezvous_server(); crate::common::test_nat_type(); #[cfg(target_os = "android")]