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

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,15 +104,31 @@ 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:
// We can't skip uploading sysinfo when the username is empty, because the username may
// always be empty before login. We also need to upload the other sysinfo info.
//
// https://github.com/rustdesk/rustdesk/discussions/8031
// We still need to check the username after uploading sysinfo, because
// 1. The username may be empty when logining in, and it can be fetched after a while.
// In this case, we need to upload sysinfo again.
// 2. The username may be changed after uploading sysinfo, and we need to upload sysinfo again.
//
// The Windows session will switch to the last user session before the restart,
// so it may be able to get the username before login.
// But strangely, sometimes we can get the username before login,
// we may not be able to get the username before login after the next restart.
let mut v = crate::get_sysinfo(); let mut v = crate::get_sysinfo();
// username is empty in login screen of windows, but here we only upload sysinfo once, causing let sys_username = v["username"].as_str().unwrap_or_default().to_string();
// real user name not uploaded after login screen. https://github.com/rustdesk/rustdesk/discussions/8031 // Though the username comparison is only necessary on Windows,
if !cfg!(windows) || !v["username"].as_str().unwrap_or_default().is_empty() { // we still keep the comparison on other platforms for consistency.
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);
if need_upload {
v["version"] = json!(crate::VERSION); v["version"] = json!(crate::VERSION);
v["id"] = json!(id); v["id"] = json!(id);
v["uuid"] = json!(crate::encode64(hbb_common::get_uuid())); v["uuid"] = json!(crate::encode64(hbb_common::get_uuid()));
@@ -130,7 +177,7 @@ async fn start_hbbs_sync_async() {
} }
}; };
if samever { if samever {
info_uploaded = (true, url.clone(), None, id.clone()); info_uploaded = InfoUploaded::uploaded(url.clone(), id.clone(), sys_username);
log::info!("sysinfo not changed, skip upload"); log::info!("sysinfo not changed, skip upload");
continue; continue;
} }
@@ -139,7 +186,7 @@ async fn start_hbbs_sync_async() {
match crate::post_request(url.replace("heartbeat", "sysinfo"), v, "").await { match crate::post_request(url.replace("heartbeat", "sysinfo"), v, "").await {
Ok(x) => { Ok(x) => {
if x == "SYSINFO_UPDATED" { if x == "SYSINFO_UPDATED" {
info_uploaded = (true, url.clone(), None, id.clone()); info_uploaded = InfoUploaded::uploaded(url.clone(), id.clone(), sys_username);
log::info!("sysinfo updated"); log::info!("sysinfo updated");
if !hash.is_empty() { if !hash.is_empty() {
config::Status::set("sysinfo_hash", hash); config::Status::set("sysinfo_hash", hash);
@@ -147,14 +194,13 @@ async fn start_hbbs_sync_async() {
} }
*PRO.lock().unwrap() = true; *PRO.lock().unwrap() = true;
} else if x == "ID_NOT_FOUND" { } else if x == "ID_NOT_FOUND" {
info_uploaded.2 = None; // next heartbeat will upload sysinfo again info_uploaded.last_uploaded = None; // next heartbeat will upload sysinfo again
} else { } else {
info_uploaded.2 = Some(Instant::now()); info_uploaded.last_uploaded = Some(Instant::now());
} }
} }
_ => { _ => {
info_uploaded.2 = Some(Instant::now()); info_uploaded.last_uploaded = Some(Instant::now());
}
} }
} }
} }
@@ -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");
} }