Skip to content

Commit

Permalink
sys info
Browse files Browse the repository at this point in the history
  • Loading branch information
rustdesk committed Jul 22, 2023
1 parent b52795b commit 07137ac
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/hbb_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
33 changes: 32 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion src/core_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
104 changes: 65 additions & 39 deletions src/hbbs_http/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down Expand Up @@ -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 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 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(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 {
Expand Down
4 changes: 3 additions & 1 deletion src/rendezvous_mediator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
Expand Down

0 comments on commit 07137ac

Please sign in to comment.