mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
refactor config sync
This commit is contained in:
13
src/ipc.rs
13
src/ipc.rs
@@ -278,18 +278,8 @@ async fn handle(data: Data, stream: &mut Connection) {
|
||||
allow_err!(stream.send(&Data::Options(Some(v))).await);
|
||||
}
|
||||
Some(value) => {
|
||||
let v0 = Config::get_option("stop-service");
|
||||
let v1 = Config::get_rendezvous_servers();
|
||||
let v2 = Config::get_option("audio-input");
|
||||
let _chk = CheckIfRestart::new();
|
||||
Config::set_options(value);
|
||||
if v0 != Config::get_option("stop-service")
|
||||
|| v1 != Config::get_rendezvous_servers()
|
||||
{
|
||||
RendezvousMediator::restart();
|
||||
}
|
||||
if v2 != Config::get_option("audio-input") {
|
||||
crate::audio_service::restart();
|
||||
}
|
||||
allow_err!(stream.send(&Data::Options(None)).await);
|
||||
}
|
||||
},
|
||||
@@ -298,6 +288,7 @@ async fn handle(data: Data, stream: &mut Connection) {
|
||||
allow_err!(stream.send(&Data::NatType(Some(t))).await);
|
||||
}
|
||||
Data::SyncConfig(Some((config, config2))) => {
|
||||
let _chk = CheckIfRestart::new();
|
||||
Config::set(config);
|
||||
Config2::set(config2);
|
||||
allow_err!(stream.send(&Data::SyncConfig(None)).await);
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@@ -95,7 +95,7 @@ fn main() {
|
||||
}
|
||||
} else if args[0] == "--import-config" {
|
||||
if args.len() == 2 {
|
||||
hbb_common::config::Config::import(&args[1]);
|
||||
import_config(&args[1]);
|
||||
}
|
||||
return;
|
||||
} else if args[0] == "--password" {
|
||||
@@ -106,6 +106,31 @@ fn main() {
|
||||
}
|
||||
}
|
||||
ui::start(&mut args[..]);
|
||||
_async_logger_holder.map(|x| x.flush());
|
||||
}
|
||||
|
||||
fn import_config(path: &str) {
|
||||
use hbb_common::{config::*, get_modified_time};
|
||||
let path2 = path.replace(".toml", "2.toml");
|
||||
let path2 = std::path::Path::new(&path2);
|
||||
let path = std::path::Path::new(path);
|
||||
log::info!("import config from {:?} and {:?}", path, path2);
|
||||
let config: Config = load_path(path.into());
|
||||
if config.id.is_empty() || config.key_pair.0.is_empty() {
|
||||
log::info!("Empty source config, skipped");
|
||||
return;
|
||||
}
|
||||
if get_modified_time(&path) > get_modified_time(&Config::file()) {
|
||||
if Config::set(config) {
|
||||
log::info!("config written");
|
||||
}
|
||||
}
|
||||
let config2: Config2 = load_path(path2.into());
|
||||
if get_modified_time(&path2) > get_modified_time(&Config2::file()) {
|
||||
if Config2::set(config2) {
|
||||
log::info!("config2 written");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
|
||||
@@ -879,8 +879,6 @@ copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{start_menu}\\\"
|
||||
|
||||
let meta = std::fs::symlink_metadata(std::env::current_exe()?)?;
|
||||
let size = meta.len() / 1024;
|
||||
// save_tmp is for ensuring not copying file while writing
|
||||
let config_path = Config::save_tmp();
|
||||
let ext = APP_NAME.to_lowercase();
|
||||
// https://docs.microsoft.com/zh-cn/windows/win32/msi/uninstall-registry-key?redirectedfrom=MSDNa
|
||||
// https://www.windowscentral.com/how-edit-registry-using-command-prompt-windows-10
|
||||
@@ -927,8 +925,6 @@ sc stop {app_name}
|
||||
sc delete {app_name}
|
||||
sc create {app_name} binpath= \"\\\"{exe}\\\" --service\" start= auto DisplayName= \"{app_name} Service\"
|
||||
netsh advfirewall firewall add rule name=\"{app_name} Service\" dir=in action=allow program=\"{exe}\" enable=yes
|
||||
del /f \"{config_path}\"
|
||||
del /f \"{config2_path}\"
|
||||
sc start {app_name}
|
||||
",
|
||||
path=path,
|
||||
@@ -946,8 +942,7 @@ sc start {app_name}
|
||||
tray_shortcut=tray_shortcut,
|
||||
tmp_path=tmp_path,
|
||||
shortcuts=shortcuts,
|
||||
config_path=config_path,
|
||||
config2_path=config_path.replace(".toml", "2.toml"),
|
||||
config_path=Config::file().to_str().unwrap_or(""),
|
||||
ext=ext,
|
||||
);
|
||||
run_cmds(cmds, false)?;
|
||||
|
||||
@@ -4,7 +4,7 @@ use hbb_common::{
|
||||
allow_err,
|
||||
anyhow::{anyhow, Context},
|
||||
bail,
|
||||
config::{Config, CONNECT_TIMEOUT, RELAY_PORT},
|
||||
config::{Config, Config2, CONNECT_TIMEOUT, RELAY_PORT},
|
||||
log,
|
||||
message_proto::*,
|
||||
protobuf::{Message as _, ProtobufEnum},
|
||||
@@ -289,32 +289,18 @@ pub async fn start_server(is_server: bool, _tray: bool) {
|
||||
} else {
|
||||
match crate::ipc::connect(1000, "").await {
|
||||
Ok(mut conn) => {
|
||||
allow_err!(conn.send(&Data::SystemInfo(None)).await);
|
||||
if let Ok(Some(data)) = conn.next_timeout(1000).await {
|
||||
log::info!("server info: {:?}", data);
|
||||
}
|
||||
// sync key pair
|
||||
let mut n = 0;
|
||||
loop {
|
||||
if Config::get_key_confirmed() {
|
||||
// check ipc::get_id(), key_confirmed may change, so give some chance to correct
|
||||
n += 1;
|
||||
if n > 3 {
|
||||
break;
|
||||
} else {
|
||||
sleep(1.).await;
|
||||
}
|
||||
} else {
|
||||
allow_err!(conn.send(&Data::ConfirmedKey(None)).await);
|
||||
if let Ok(Some(Data::ConfirmedKey(Some(pair)))) =
|
||||
conn.next_timeout(1000).await
|
||||
{
|
||||
Config::set_key_pair(pair);
|
||||
Config::set_key_confirmed(true);
|
||||
log::info!("key pair synced");
|
||||
break;
|
||||
} else {
|
||||
sleep(1.).await;
|
||||
if conn.send(&Data::SyncConfig(None)).await.is_ok() {
|
||||
if let Ok(Some(data)) = conn.next_timeout(1000).await {
|
||||
match data {
|
||||
Data::SyncConfig(Some((config, config2))) => {
|
||||
if Config::set(config) {
|
||||
log::info!("config synced");
|
||||
}
|
||||
if Config2::set(config2) {
|
||||
log::info!("config2 synced");
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,7 +319,6 @@ async fn sync_and_watch_config_dir() {
|
||||
return;
|
||||
}
|
||||
|
||||
use hbb_common::config::Config2;
|
||||
let mut cfg0 = (Config::get(), Config2::get());
|
||||
let mut synced = false;
|
||||
let tries =
|
||||
|
||||
Reference in New Issue
Block a user