mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-11-24 04:12:20 +08:00
sys info
This commit is contained in:
parent
b52795bd59
commit
07137ac566
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -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",
|
||||
|
@ -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 = []
|
||||
|
@ -625,6 +625,12 @@ pub async fn get_rendezvous_server(ms_timeout: u64) -> (String, Vec<String>, 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<String> = 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<T: std::string::ToString>(host: T, port: i32) -> String {
|
||||
hbb_common::socket_client::check_port(host, port)
|
||||
@ -984,7 +1013,9 @@ pub fn decode64<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, 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");
|
||||
|
@ -280,7 +280,11 @@ pub fn core_main() -> Option<Vec<String>> {
|
||||
{
|
||||
// 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);
|
||||
|
@ -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<Instant> = None;
|
||||
let mut info_uploaded: (bool, String, Option<Instant>) = (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::<i64>().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::<HashMap::<&str, Value>>(&s) {
|
||||
if let Some(conns) = rsp.remove("disconnect") {
|
||||
if let Ok(conns) = serde_json::from_value::<Vec<i32>>(conns) {
|
||||
SENDER.lock().unwrap().send(conns).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
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::<i64>().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::<HashMap::<&str, Value>>(&s) {
|
||||
if let Some(conns) = rsp.remove("disconnect") {
|
||||
if let Ok(conns) = serde_json::from_value::<Vec<i32>>(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::<i64>(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::<StrategyOptions>(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::<i64>(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::<StrategyOptions>(strategy) {
|
||||
handle_config_options(strategy.config_options);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn heartbeat_url() -> String {
|
||||
|
@ -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);
|
||||
}));
|
||||
|
Loading…
Reference in New Issue
Block a user