mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
sys info
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user