fix: win, upload sysinfo (#11849)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou 2025-05-23 16:46:50 +08:00 committed by GitHub
parent 48da2709d7
commit 6ff679c6b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -49,6 +49,38 @@ pub struct StrategyOptions {
pub extra: HashMap<String, String>, pub extra: HashMap<String, String>,
} }
struct InfoUploaded {
uploaded: bool,
url: String,
last_uploaded: Option<Instant>,
id: String,
username: Option<String>,
}
impl Default for InfoUploaded {
fn default() -> Self {
Self {
uploaded: false,
url: "".to_owned(),
last_uploaded: None,
id: "".to_owned(),
username: None,
}
}
}
impl InfoUploaded {
fn uploaded(url: String, id: String, username: String) -> Self {
Self {
uploaded: true,
url,
last_uploaded: None,
id,
username: Some(username),
}
}
}
#[cfg(not(any(target_os = "ios")))] #[cfg(not(any(target_os = "ios")))]
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn start_hbbs_sync_async() { async fn start_hbbs_sync_async() {
@ -57,8 +89,7 @@ async fn start_hbbs_sync_async() {
TIME_CONN, TIME_CONN,
)); ));
let mut last_sent: Option<Instant> = None; let mut last_sent: Option<Instant> = None;
let mut info_uploaded: (bool, String, Option<Instant>, String) = let mut info_uploaded = InfoUploaded::default();
(false, "".to_owned(), None, "".to_owned());
let mut sysinfo_ver = "".to_owned(); let mut sysinfo_ver = "".to_owned();
loop { loop {
tokio::select! { tokio::select! {
@ -73,89 +104,104 @@ async fn start_hbbs_sync_async() {
continue; continue;
} }
let conns = Connection::alive_conns(); let conns = Connection::alive_conns();
if info_uploaded.0 && (url != info_uploaded.1 || id != info_uploaded.3) { if info_uploaded.uploaded && (url != info_uploaded.url || id != info_uploaded.id) {
info_uploaded.0 = false; info_uploaded.uploaded = false;
*PRO.lock().unwrap() = false; *PRO.lock().unwrap() = false;
} }
if !info_uploaded.0 && info_uploaded.2.map(|x| x.elapsed() >= UPLOAD_SYSINFO_TIMEOUT).unwrap_or(true) { // For Windows:
let mut v = crate::get_sysinfo(); // We can't skip uploading sysinfo when the username is empty, because the username may
// username is empty in login screen of windows, but here we only upload sysinfo once, causing // always be empty before login. We also need to upload the other sysinfo info.
// real user name not uploaded after login screen. https://github.com/rustdesk/rustdesk/discussions/8031 //
if !cfg!(windows) || !v["username"].as_str().unwrap_or_default().is_empty() { // https://github.com/rustdesk/rustdesk/discussions/8031
v["version"] = json!(crate::VERSION); // We still need to check the username after uploading sysinfo, because
v["id"] = json!(id); // 1. The username may be empty when logining in, and it can be fetched after a while.
v["uuid"] = json!(crate::encode64(hbb_common::get_uuid())); // In this case, we need to upload sysinfo again.
let ab_name = Config::get_option(keys::OPTION_PRESET_ADDRESS_BOOK_NAME); // 2. The username may be changed after uploading sysinfo, and we need to upload sysinfo again.
if !ab_name.is_empty() { //
v[keys::OPTION_PRESET_ADDRESS_BOOK_NAME] = json!(ab_name); // The Windows session will switch to the last user session before the restart,
} // so it may be able to get the username before login.
let ab_tag = Config::get_option(keys::OPTION_PRESET_ADDRESS_BOOK_TAG); // But strangely, sometimes we can get the username before login,
if !ab_tag.is_empty() { // we may not be able to get the username before login after the next restart.
v[keys::OPTION_PRESET_ADDRESS_BOOK_TAG] = json!(ab_tag); let mut v = crate::get_sysinfo();
} let sys_username = v["username"].as_str().unwrap_or_default().to_string();
let username = get_builtin_option(keys::OPTION_PRESET_USERNAME); // Though the username comparison is only necessary on Windows,
if !username.is_empty() { // we still keep the comparison on other platforms for consistency.
v[keys::OPTION_PRESET_USERNAME] = json!(username); let need_upload = (!info_uploaded.uploaded || info_uploaded.username.as_ref() != Some(&sys_username)) &&
} info_uploaded.last_uploaded.map(|x| x.elapsed() >= UPLOAD_SYSINFO_TIMEOUT).unwrap_or(true);
let strategy_name = get_builtin_option(keys::OPTION_PRESET_STRATEGY_NAME); if need_upload {
if !strategy_name.is_empty() { v["version"] = json!(crate::VERSION);
v[keys::OPTION_PRESET_STRATEGY_NAME] = json!(strategy_name); v["id"] = json!(id);
} v["uuid"] = json!(crate::encode64(hbb_common::get_uuid()));
let device_group_name = get_builtin_option(keys::OPTION_PRESET_DEVICE_GROUP_NAME); let ab_name = Config::get_option(keys::OPTION_PRESET_ADDRESS_BOOK_NAME);
if !device_group_name.is_empty() { if !ab_name.is_empty() {
v[keys::OPTION_PRESET_DEVICE_GROUP_NAME] = json!(device_group_name); v[keys::OPTION_PRESET_ADDRESS_BOOK_NAME] = json!(ab_name);
} }
let v = v.to_string(); let ab_tag = Config::get_option(keys::OPTION_PRESET_ADDRESS_BOOK_TAG);
let mut hash = "".to_owned(); if !ab_tag.is_empty() {
if crate::is_public(&url) { v[keys::OPTION_PRESET_ADDRESS_BOOK_TAG] = json!(ab_tag);
use sha2::{Digest, Sha256}; }
let mut hasher = Sha256::new(); let username = get_builtin_option(keys::OPTION_PRESET_USERNAME);
hasher.update(url.as_bytes()); if !username.is_empty() {
hasher.update(&v.as_bytes()); v[keys::OPTION_PRESET_USERNAME] = json!(username);
let res = hasher.finalize(); }
hash = hbb_common::base64::encode(&res[..]); let strategy_name = get_builtin_option(keys::OPTION_PRESET_STRATEGY_NAME);
let old_hash = config::Status::get("sysinfo_hash"); if !strategy_name.is_empty() {
let ver = config::Status::get("sysinfo_ver"); // sysinfo_ver is the version of sysinfo on server's side v[keys::OPTION_PRESET_STRATEGY_NAME] = json!(strategy_name);
if hash == old_hash { }
// When the api doesn't exist, Ok("") will be returned in test. let device_group_name = get_builtin_option(keys::OPTION_PRESET_DEVICE_GROUP_NAME);
let samever = match crate::post_request(url.replace("heartbeat", "sysinfo_ver"), "".to_owned(), "").await { if !device_group_name.is_empty() {
Ok(x) => { v[keys::OPTION_PRESET_DEVICE_GROUP_NAME] = json!(device_group_name);
sysinfo_ver = x.clone(); }
*PRO.lock().unwrap() = true; let v = v.to_string();
x == ver let mut hash = "".to_owned();
} if crate::is_public(&url) {
_ => { use sha2::{Digest, Sha256};
false // to make sure Pro can be assigned in below post for old let mut hasher = Sha256::new();
// hbbs pro not supporting sysinfo_ver, use false for ensuring hasher.update(url.as_bytes());
} hasher.update(&v.as_bytes());
}; let res = hasher.finalize();
if samever { hash = hbb_common::base64::encode(&res[..]);
info_uploaded = (true, url.clone(), None, id.clone()); let old_hash = config::Status::get("sysinfo_hash");
log::info!("sysinfo not changed, skip upload"); let ver = config::Status::get("sysinfo_ver"); // sysinfo_ver is the version of sysinfo on server's side
continue; if hash == old_hash {
} // When the api doesn't exist, Ok("") will be returned in test.
} let samever = match crate::post_request(url.replace("heartbeat", "sysinfo_ver"), "".to_owned(), "").await {
} Ok(x) => {
match crate::post_request(url.replace("heartbeat", "sysinfo"), v, "").await { sysinfo_ver = x.clone();
Ok(x) => {
if x == "SYSINFO_UPDATED" {
info_uploaded = (true, url.clone(), None, id.clone());
log::info!("sysinfo updated");
if !hash.is_empty() {
config::Status::set("sysinfo_hash", hash);
config::Status::set("sysinfo_ver", sysinfo_ver.clone());
}
*PRO.lock().unwrap() = true; *PRO.lock().unwrap() = true;
} else if x == "ID_NOT_FOUND" { x == ver
info_uploaded.2 = None; // next heartbeat will upload sysinfo again
} else {
info_uploaded.2 = Some(Instant::now());
} }
_ => {
false // to make sure Pro can be assigned in below post for old
// hbbs pro not supporting sysinfo_ver, use false for ensuring
}
};
if samever {
info_uploaded = InfoUploaded::uploaded(url.clone(), id.clone(), sys_username);
log::info!("sysinfo not changed, skip upload");
continue;
} }
_ => { }
info_uploaded.2 = Some(Instant::now()); }
match crate::post_request(url.replace("heartbeat", "sysinfo"), v, "").await {
Ok(x) => {
if x == "SYSINFO_UPDATED" {
info_uploaded = InfoUploaded::uploaded(url.clone(), id.clone(), sys_username);
log::info!("sysinfo updated");
if !hash.is_empty() {
config::Status::set("sysinfo_hash", hash);
config::Status::set("sysinfo_ver", sysinfo_ver.clone());
}
*PRO.lock().unwrap() = true;
} else if x == "ID_NOT_FOUND" {
info_uploaded.last_uploaded = None; // next heartbeat will upload sysinfo again
} else {
info_uploaded.last_uploaded = Some(Instant::now());
} }
} }
_ => {
info_uploaded.last_uploaded = Some(Instant::now());
}
} }
} }
if conns.is_empty() && last_sent.map(|x| x.elapsed() < TIME_HEARTBEAT).unwrap_or(false) { if conns.is_empty() && last_sent.map(|x| x.elapsed() < TIME_HEARTBEAT).unwrap_or(false) {
@ -174,7 +220,7 @@ async fn start_hbbs_sync_async() {
if let Ok(s) = crate::post_request(url.clone(), v.to_string(), "").await { if let Ok(s) = crate::post_request(url.clone(), v.to_string(), "").await {
if let Ok(mut rsp) = serde_json::from_str::<HashMap::<&str, Value>>(&s) { if let Ok(mut rsp) = serde_json::from_str::<HashMap::<&str, Value>>(&s) {
if rsp.remove("sysinfo").is_some() { if rsp.remove("sysinfo").is_some() {
info_uploaded.0 = false; info_uploaded.uploaded = false;
config::Status::set("sysinfo_hash", "".to_owned()); config::Status::set("sysinfo_hash", "".to_owned());
log::info!("sysinfo required to forcely update"); log::info!("sysinfo required to forcely update");
} }