refactor config sync
This commit is contained in:
parent
0d585a751a
commit
b5395d954a
@ -62,13 +62,13 @@ pub enum NetworkType {
|
|||||||
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
|
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
id: String,
|
pub id: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
password: String,
|
password: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
salt: String,
|
salt: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
key_pair: (Vec<u8>, Vec<u8>), // sk, pk
|
pub key_pair: (Vec<u8>, Vec<u8>), // sk, pk
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
key_confirmed: bool,
|
key_confirmed: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@ -199,26 +199,37 @@ impl Config2 {
|
|||||||
return CONFIG2.read().unwrap().clone();
|
return CONFIG2.read().unwrap().clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(cfg: Config2) {
|
pub fn set(cfg: Config2) -> bool {
|
||||||
let mut lock = CONFIG2.write().unwrap();
|
let mut lock = CONFIG2.write().unwrap();
|
||||||
|
if *lock == cfg {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
*lock = cfg;
|
*lock = cfg;
|
||||||
lock.store();
|
lock.store();
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn load_path<T: serde::Serialize + serde::de::DeserializeOwned + Default + std::fmt::Debug>(
|
||||||
|
file: PathBuf,
|
||||||
|
) -> T {
|
||||||
|
let cfg = match confy::load_path(&file) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to load config: {}", err);
|
||||||
|
T::default()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cfg
|
||||||
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
fn load_<T: serde::Serialize + serde::de::DeserializeOwned + Default + std::fmt::Debug>(
|
fn load_<T: serde::Serialize + serde::de::DeserializeOwned + Default + std::fmt::Debug>(
|
||||||
suffix: &str,
|
suffix: &str,
|
||||||
) -> T {
|
) -> T {
|
||||||
let file = Self::file_(suffix);
|
let file = Self::file_(suffix);
|
||||||
log::debug!("Configuration path: {}", file.display());
|
log::debug!("Configuration path: {}", file.display());
|
||||||
let cfg = match confy::load_path(&file) {
|
let cfg = load_path(file);
|
||||||
Ok(config) => config,
|
|
||||||
Err(err) => {
|
|
||||||
log::error!("Failed to load config: {}", err);
|
|
||||||
T::default()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if suffix.is_empty() {
|
if suffix.is_empty() {
|
||||||
log::debug!("{:?}", cfg);
|
log::debug!("{:?}", cfg);
|
||||||
}
|
}
|
||||||
@ -244,28 +255,6 @@ impl Config {
|
|||||||
Self::file_("")
|
Self::file_("")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn import(from: &str) {
|
|
||||||
log::info!("import {}", from);
|
|
||||||
// load first to create path
|
|
||||||
Self::load();
|
|
||||||
crate::allow_err!(std::fs::copy(from, Self::file()));
|
|
||||||
crate::allow_err!(std::fs::copy(
|
|
||||||
from.replace(".toml", "2.toml"),
|
|
||||||
Self::file_("2")
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_tmp() -> String {
|
|
||||||
let _lock = CONFIG.read().unwrap(); // do not use let _, which will be dropped immediately
|
|
||||||
let path = Self::file_("2").to_str().unwrap_or("").to_owned();
|
|
||||||
let path2 = format!("{}_tmp", path);
|
|
||||||
crate::allow_err!(std::fs::copy(&path, &path2));
|
|
||||||
let path = Self::file().to_str().unwrap_or("").to_owned();
|
|
||||||
let path2 = format!("{}_tmp", path);
|
|
||||||
crate::allow_err!(std::fs::copy(&path, &path2));
|
|
||||||
path2
|
|
||||||
}
|
|
||||||
|
|
||||||
fn file_(suffix: &str) -> PathBuf {
|
fn file_(suffix: &str) -> PathBuf {
|
||||||
let name = format!("{}{}", APP_NAME, suffix);
|
let name = format!("{}{}", APP_NAME, suffix);
|
||||||
Self::path(name).with_extension("toml")
|
Self::path(name).with_extension("toml")
|
||||||
@ -645,10 +634,14 @@ impl Config {
|
|||||||
return CONFIG.read().unwrap().clone();
|
return CONFIG.read().unwrap().clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(cfg: Config) {
|
pub fn set(cfg: Config) -> bool {
|
||||||
let mut lock = CONFIG.write().unwrap();
|
let mut lock = CONFIG.write().unwrap();
|
||||||
|
if *lock == cfg {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
*lock = cfg;
|
*lock = cfg;
|
||||||
lock.store();
|
lock.store();
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
allow_err!(stream.send(&Data::Options(Some(v))).await);
|
||||||
}
|
}
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
let v0 = Config::get_option("stop-service");
|
let _chk = CheckIfRestart::new();
|
||||||
let v1 = Config::get_rendezvous_servers();
|
|
||||||
let v2 = Config::get_option("audio-input");
|
|
||||||
Config::set_options(value);
|
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);
|
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);
|
allow_err!(stream.send(&Data::NatType(Some(t))).await);
|
||||||
}
|
}
|
||||||
Data::SyncConfig(Some((config, config2))) => {
|
Data::SyncConfig(Some((config, config2))) => {
|
||||||
|
let _chk = CheckIfRestart::new();
|
||||||
Config::set(config);
|
Config::set(config);
|
||||||
Config2::set(config2);
|
Config2::set(config2);
|
||||||
allow_err!(stream.send(&Data::SyncConfig(None)).await);
|
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" {
|
} else if args[0] == "--import-config" {
|
||||||
if args.len() == 2 {
|
if args.len() == 2 {
|
||||||
hbb_common::config::Config::import(&args[1]);
|
import_config(&args[1]);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if args[0] == "--password" {
|
} else if args[0] == "--password" {
|
||||||
@ -106,6 +106,31 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ui::start(&mut args[..]);
|
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")]
|
#[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 meta = std::fs::symlink_metadata(std::env::current_exe()?)?;
|
||||||
let size = meta.len() / 1024;
|
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();
|
let ext = APP_NAME.to_lowercase();
|
||||||
// https://docs.microsoft.com/zh-cn/windows/win32/msi/uninstall-registry-key?redirectedfrom=MSDNa
|
// 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
|
// 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 delete {app_name}
|
||||||
sc create {app_name} binpath= \"\\\"{exe}\\\" --service\" start= auto DisplayName= \"{app_name} Service\"
|
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
|
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}
|
sc start {app_name}
|
||||||
",
|
",
|
||||||
path=path,
|
path=path,
|
||||||
@ -946,8 +942,7 @@ sc start {app_name}
|
|||||||
tray_shortcut=tray_shortcut,
|
tray_shortcut=tray_shortcut,
|
||||||
tmp_path=tmp_path,
|
tmp_path=tmp_path,
|
||||||
shortcuts=shortcuts,
|
shortcuts=shortcuts,
|
||||||
config_path=config_path,
|
config_path=Config::file().to_str().unwrap_or(""),
|
||||||
config2_path=config_path.replace(".toml", "2.toml"),
|
|
||||||
ext=ext,
|
ext=ext,
|
||||||
);
|
);
|
||||||
run_cmds(cmds, false)?;
|
run_cmds(cmds, false)?;
|
||||||
|
@ -4,7 +4,7 @@ use hbb_common::{
|
|||||||
allow_err,
|
allow_err,
|
||||||
anyhow::{anyhow, Context},
|
anyhow::{anyhow, Context},
|
||||||
bail,
|
bail,
|
||||||
config::{Config, CONNECT_TIMEOUT, RELAY_PORT},
|
config::{Config, Config2, CONNECT_TIMEOUT, RELAY_PORT},
|
||||||
log,
|
log,
|
||||||
message_proto::*,
|
message_proto::*,
|
||||||
protobuf::{Message as _, ProtobufEnum},
|
protobuf::{Message as _, ProtobufEnum},
|
||||||
@ -289,32 +289,18 @@ pub async fn start_server(is_server: bool, _tray: bool) {
|
|||||||
} else {
|
} else {
|
||||||
match crate::ipc::connect(1000, "").await {
|
match crate::ipc::connect(1000, "").await {
|
||||||
Ok(mut conn) => {
|
Ok(mut conn) => {
|
||||||
allow_err!(conn.send(&Data::SystemInfo(None)).await);
|
if conn.send(&Data::SyncConfig(None)).await.is_ok() {
|
||||||
if let Ok(Some(data)) = conn.next_timeout(1000).await {
|
if let Ok(Some(data)) = conn.next_timeout(1000).await {
|
||||||
log::info!("server info: {:?}", data);
|
match data {
|
||||||
|
Data::SyncConfig(Some((config, config2))) => {
|
||||||
|
if Config::set(config) {
|
||||||
|
log::info!("config synced");
|
||||||
}
|
}
|
||||||
// sync key pair
|
if Config2::set(config2) {
|
||||||
let mut n = 0;
|
log::info!("config2 synced");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -333,7 +319,6 @@ async fn sync_and_watch_config_dir() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
use hbb_common::config::Config2;
|
|
||||||
let mut cfg0 = (Config::get(), Config2::get());
|
let mut cfg0 = (Config::get(), Config2::get());
|
||||||
let mut synced = false;
|
let mut synced = false;
|
||||||
let tries =
|
let tries =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user