From 07137ac5669caaffb53ce8824eca6c969250a8ac Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sat, 22 Jul 2023 14:30:47 +0800 Subject: [PATCH] sys info --- Cargo.lock | 4 +- libs/hbb_common/Cargo.toml | 2 +- src/common.rs | 33 +++++++++++- src/core_main.rs | 6 ++- src/hbbs_http/sync.rs | 104 +++++++++++++++++++++++-------------- src/rendezvous_mediator.rs | 4 +- 6 files changed, 108 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42d0590bab6..43de5e23c74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5841,9 +5841,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.29.0" +version = "0.29.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02f1dc6930a439cc5d154221b5387d153f8183529b07c19aca24ea31e0a167e1" +checksum = "c7cb97a5a85a136d84e75d5c3cf89655090602efb1be0d8d5337b7e386af2908" dependencies = [ "cfg-if 1.0.0", "core-foundation-sys 0.8.4", diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index 74a2d40cc22..4d008a86695 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -37,11 +37,11 @@ libc = "0.2" dlopen = "0.1" toml = "0.7" uuid = { version = "1.3", features = ["v4"] } +sysinfo = "0.29" [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] mac_address = "1.1" machine-uid = { git = "https://github.com/21pages/machine-uid" } -sysinfo = "0.29" [features] quic = [] diff --git a/src/common.rs b/src/common.rs index 8f84509aee9..74e8dc59181 100644 --- a/src/common.rs +++ b/src/common.rs @@ -625,6 +625,12 @@ pub async fn get_rendezvous_server(ms_timeout: u64) -> (String, Vec, boo let (mut a, mut b) = get_rendezvous_server_(ms_timeout); #[cfg(not(any(target_os = "android", target_os = "ios")))] let (mut a, mut b) = get_rendezvous_server_(ms_timeout).await; + #[cfg(windows)] + if let Ok(lic) = crate::platform::get_license_from_exe_name() { + if !lic.host.is_empty() { + a = lic.host; + } + } let mut b: Vec = b .drain(..) .map(|x| socket_client::check_port(x, config::RENDEZVOUS_PORT)) @@ -743,6 +749,29 @@ pub fn hostname() -> String { return DEVICE_NAME.lock().unwrap().clone(); } +#[inline] +pub fn get_sysinfo() -> serde_json::Value { + use hbb_common::sysinfo::{CpuExt, System, SystemExt}; + let system = System::new_all(); + let memory = system.total_memory(); + let memory = (memory as f64 / 1024. / 1024. / 1024. * 100 as f64).round() / 100.; + let cpus = system.cpus(); + let cpu_name = cpus.first().map(|x| x.brand()).unwrap_or_default(); + let cpu_name = cpu_name.trim_end(); + let cpu_freq = cpus.first().map(|x| x.frequency()).unwrap_or_default(); + let num_cpus = num_cpus::get(); + let num_pcpus = num_cpus::get_physical(); + let os = system.name().unwrap_or(system.distribution_id()); + let os = format!("{} {}", os, system.os_version().unwrap_or_default()); + let hostname = system.host_name(); + serde_json::json!({ + "cpu": format!("{cpu_name}, {cpu_freq}MHz, {num_cpus}/{num_pcpus} cores"), + "memory": format!("{memory}GB"), + "os": os, + "hostname": hostname, + }) +} + #[inline] pub fn check_port(host: T, port: i32) -> String { hbb_common::socket_client::check_port(host, port) @@ -984,7 +1013,9 @@ pub fn decode64>(input: T) -> Result, base64::DecodeError pub async fn get_key(sync: bool) -> String { #[cfg(windows)] if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { - return lic.key; + if !lic.key.is_empty() { + return lic.key; + } } #[cfg(target_os = "ios")] let mut key = Config::get_option("key"); diff --git a/src/core_main.rs b/src/core_main.rs index e261ed5b254..cc0a4834f71 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -280,7 +280,11 @@ pub fn core_main() -> Option> { { // arg: starting with `host=`, e.g. `host=127.0.0.1,api=https://test.com,key=asfs`, // or the filename (without ext) used in renaming exe. - let name = format!("{}.exe", args[1]); + let name = if args[1].ends_with(".exe") { + args[1].to_owned() + } else { + format!("{}.exe", args[1]) + }; if let Ok(lic) = crate::license::get_license_from_string(&name) { if !lic.host.is_empty() { crate::ui_interface::set_option("key".into(), lic.key); diff --git a/src/hbbs_http/sync.rs b/src/hbbs_http/sync.rs index 00fbc993386..e3df8f7d564 100644 --- a/src/hbbs_http/sync.rs +++ b/src/hbbs_http/sync.rs @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; const TIME_HEARTBEAT: Duration = Duration::from_secs(15); +const UPLOAD_SYSINFO_TIMEOUT: Duration = Duration::from_secs(120); const TIME_CONN: Duration = Duration::from_secs(3); #[cfg(not(any(target_os = "ios")))] @@ -45,55 +46,80 @@ pub struct StrategyOptions { #[cfg(not(any(target_os = "ios")))] #[tokio::main(flavor = "current_thread")] async fn start_hbbs_sync_async() { - tokio::spawn(async move { - let mut interval = tokio::time::interval_at(Instant::now() + TIME_CONN, TIME_CONN); - let mut last_send = Instant::now(); - loop { - tokio::select! { - _ = interval.tick() => { - let url = heartbeat_url(); - if !url.is_empty() { - let conns = Connection::alive_conns(); - if conns.is_empty() && last_send.elapsed() < TIME_HEARTBEAT { - continue; + let mut interval = tokio::time::interval_at(Instant::now() + TIME_CONN, TIME_CONN); + let mut last_sent: Option = None; + let mut info_uploaded: (bool, String, Option) = (false, "".to_owned(), None); + loop { + tokio::select! { + _ = interval.tick() => { + let url = heartbeat_url(); + if url.is_empty() { + continue; + } + if !Config::get_option("stop-service").is_empty() { + continue; + } + let conns = Connection::alive_conns(); + if info_uploaded.0 && url != info_uploaded.1 { + info_uploaded.0 = false; + } + if !info_uploaded.0 && info_uploaded.2.map(|x| x.elapsed() >= UPLOAD_SYSINFO_TIMEOUT).unwrap_or(true){ + let mut v = crate::get_sysinfo(); + v["verion"] = json!(crate::VERSION); + v["id"] = json!(Config::get_id()); + v["uuid"] = json!(crate::encode64(hbb_common::get_uuid())); + match crate::post_request(url.replace("heartbeat", "sysinfo"), v.to_string(), "").await { + Ok(x) => { + if x == "SYSINFO_UPDATED" { + info_uploaded = (true, url.clone(), None); + hbb_common::log::info!("sysinfo updated"); + } else if x == "ID_NOT_FOUND" { + info_uploaded.2 = None; // next heartbeat will upload sysinfo again + } else { + info_uploaded.2 = Some(Instant::now()); + } } - last_send = Instant::now(); - let mut v = Value::default(); - v["id"] = json!(Config::get_id()); - v["ver"] = json!(hbb_common::get_version_number(crate::VERSION)); - if !conns.is_empty() { - v["conns"] = json!(conns); + _ => { + info_uploaded.2 = Some(Instant::now()); } - let modified_at = LocalConfig::get_option("strategy_timestamp").parse::().unwrap_or(0); - v["modified_at"] = json!(modified_at); - if let Ok(s) = crate::post_request(url.clone(), v.to_string(), "").await { - if let Ok(mut rsp) = serde_json::from_str::>(&s) { - if let Some(conns) = rsp.remove("disconnect") { - if let Ok(conns) = serde_json::from_value::>(conns) { - SENDER.lock().unwrap().send(conns).ok(); - } - } - if let Some(rsp_modified_at) = rsp.remove("modified_at") { - if let Ok(rsp_modified_at) = serde_json::from_value::(rsp_modified_at) { - if rsp_modified_at != modified_at { - LocalConfig::set_option("strategy_timestamp".to_string(), rsp_modified_at.to_string()); - } - } + } + } + if conns.is_empty() && last_sent.map(|x| x.elapsed() < TIME_HEARTBEAT).unwrap_or(false){ + continue; + } + last_sent = Some(Instant::now()); + let mut v = Value::default(); + v["id"] = json!(Config::get_id()); + v["ver"] = json!(hbb_common::get_version_number(crate::VERSION)); + if !conns.is_empty() { + v["conns"] = json!(conns); + } + let modified_at = LocalConfig::get_option("strategy_timestamp").parse::().unwrap_or(0); + v["modified_at"] = json!(modified_at); + if let Ok(s) = crate::post_request(url.clone(), v.to_string(), "").await { + if let Ok(mut rsp) = serde_json::from_str::>(&s) { + if let Some(conns) = rsp.remove("disconnect") { + if let Ok(conns) = serde_json::from_value::>(conns) { + SENDER.lock().unwrap().send(conns).ok(); } - if let Some(strategy) = rsp.remove("strategy") { - if let Ok(strategy) = serde_json::from_value::(strategy) { - handle_config_options(strategy.config_options); - } + } + if let Some(rsp_modified_at) = rsp.remove("modified_at") { + if let Ok(rsp_modified_at) = serde_json::from_value::(rsp_modified_at) { + if rsp_modified_at != modified_at { + LocalConfig::set_option("strategy_timestamp".to_string(), rsp_modified_at.to_string()); } } } + if let Some(strategy) = rsp.remove("strategy") { + if let Ok(strategy) = serde_json::from_value::(strategy) { + handle_config_options(strategy.config_options); + } + } } } } } - }) - .await - .ok(); + } } fn heartbeat_url() -> String { diff --git a/src/rendezvous_mediator.rs b/src/rendezvous_mediator.rs index 30cf65d73eb..4b2e8bbc698 100644 --- a/src/rendezvous_mediator.rs +++ b/src/rendezvous_mediator.rs @@ -89,7 +89,9 @@ impl RendezvousMediator { for host in servers.clone() { let server = server.clone(); futs.push(tokio::spawn(async move { - allow_err!(Self::start(server, host).await); + if let Err(err) = Self::start(server, host).await { + log::error!("rendezvous mediator error: {err}"); + } // SHOULD_EXIT here is to ensure once one exits, the others also exit. SHOULD_EXIT.store(true, Ordering::SeqCst); }));