Add user and root config sync action

This commit is contained in:
chenbaiyu
2022-01-13 15:26:57 +08:00
parent 81e43c2970
commit f45a2c7a94
7 changed files with 319 additions and 4 deletions

View File

@@ -92,6 +92,15 @@ pub enum Data {
Socks(Option<config::Socks5Server>),
FS(FS),
Test,
SyncConfigToRootReq {
from: String,
},
SyncConfigToRootResp(bool),
SyncConfigToUserReq {
username: String,
to: String,
},
SyncConfigToUserResp(bool),
}
#[tokio::main(flavor = "current_thread")]
@@ -252,6 +261,24 @@ async fn handle(data: Data, stream: &mut Connection) {
let t = Config::get_nat_type();
allow_err!(stream.send(&Data::NatType(Some(t))).await);
}
Data::SyncConfigToRootReq { from } => {
allow_err!(
stream
.send(&Data::SyncConfigToRootResp(Config::sync_config_to_root(
from
)))
.await
);
}
Data::SyncConfigToUserReq { username, to } => {
allow_err!(
stream
.send(&Data::SyncConfigToUserResp(Config::sync_config_to_user(
username, to
)))
.await
);
}
_ => {}
}
}

View File

@@ -101,6 +101,10 @@ fn main() {
ipc::set_password(args[1].to_owned()).unwrap();
}
return;
} else if cfg!(target_os = "macos") && args[0] == "--daemon" {
log::info!("start --daemon");
crate::platform::start_daemon();
return;
}
}
ui::start(&mut args[..]);

View File

@@ -333,3 +333,11 @@ pub fn block_input(_v: bool) {
pub fn is_installed() -> bool {
true
}
pub fn start_daemon(){
log::info!("{}",crate::username());
if let Err(err) = crate::ipc::start("_daemon") {
log::error!("Failed to start ipc_daemon: {}", err);
std::process::exit(-1);
}
}

View File

@@ -1,4 +1,4 @@
use crate::ipc::Data;
use crate::ipc::{ConnectionTmpl, Data};
use connection::{ConnInner, Connection};
use hbb_common::{
allow_err,
@@ -9,12 +9,15 @@ use hbb_common::{
message_proto::*,
protobuf::{Message as _, ProtobufEnum},
rendezvous_proto::*,
sleep,
sleep, socket_client,
sodiumoxide::crypto::{box_, secretbox, sign},
timeout, tokio, ResultType, Stream,
socket_client,
};
use notify::{watcher, RecursiveMode, Watcher};
use parity_tokio_ipc::ConnectionClient;
use service::{GenericService, Service, ServiceTmpl, Subscriber};
use std::path::PathBuf;
use std::time::Duration;
use std::{
collections::HashMap,
net::SocketAddr,
@@ -268,6 +271,9 @@ pub async fn start_server(is_server: bool, _tray: bool) {
log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY"));
}
sync_and_watch_config_dir().await;
if is_server {
std::thread::spawn(move || {
if let Err(err) = crate::ipc::start("") {
@@ -317,3 +323,105 @@ pub async fn start_server(is_server: bool, _tray: bool) {
}
}
}
async fn sync_and_watch_config_dir() {
if crate::username() == "root" {
return;
}
match crate::ipc::connect(1000, "_daemon").await {
Ok(mut conn) => {
match sync_config_to_user(&mut conn).await {
Err(e) => log::error!("sync config to user failed:{}", e),
_ => {}
}
tokio::spawn(async move {
log::info!(
"watching config dir: {}",
Config::path("").to_str().unwrap().to_string()
);
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
watcher
.watch(Config::path("").as_path(), RecursiveMode::Recursive)
.unwrap();
loop {
let ev = rx.recv();
match ev {
Ok(event) => match event {
notify::DebouncedEvent::Write(path) => {
log::info!(
"config file changed, call ipc_daemon to sync: {}",
path.to_str().unwrap().to_string()
);
match sync_config_to_root(&mut conn, path).await {
Err(e) => log::error!("sync config to root failed: {}", e),
_ => {}
}
}
x => {
log::debug!("another {:?}", x)
}
},
Err(e) => println!("watch error: {:?}", e),
}
}
});
}
Err(_) => {
log::info!("connect ipc_daemon failed, skip config sync");
return;
}
}
}
async fn sync_config_to_user(conn: &mut ConnectionTmpl<ConnectionClient>) -> ResultType<()> {
allow_err!(
conn.send(&Data::SyncConfigToUserReq {
username: crate::username(),
to: Config::path("").to_str().unwrap().to_string(),
})
.await
);
if let Some(data) = conn.next_timeout(2000).await? {
match data {
Data::SyncConfigToUserResp(success) => {
log::info!("copy and reload config dir success: {:?}", success);
}
_ => {}
};
};
Ok(())
}
async fn sync_config_to_root(
conn: &mut ConnectionTmpl<ConnectionClient>,
from: PathBuf,
) -> ResultType<()> {
allow_err!(
conn.send(&Data::SyncConfigToRootReq {
from: from.to_str().unwrap().to_string()
})
.await
);
// todo: this code will block outer loop, resolve it later.
// if let Some(data) = conn.next_timeout(2000).await? {
// match data {
// Data::SyncConfigToRootResp(success) => {
// log::info!("copy config to root dir success: {:?}", success);
// }
// x => {
// log::info!("receive another {:?}", x)
// }
// };
// };
Ok(())
}