Revert "temp commit"

This reverts commit 2e03ca19f6afe9bb705e041af3b97e2ddb398ba7.
This commit is contained in:
chenbaiyu 2022-01-13 16:07:18 +08:00
parent 1995f9fa4e
commit e51e5ac4b1
9 changed files with 119 additions and 356 deletions

77
Cargo.lock generated
View File

@ -1115,31 +1115,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "fs_extra"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]]
name = "fsevent"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6"
dependencies = [
"bitflags",
"fsevent-sys",
]
[[package]]
name = "fsevent-sys"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "fuchsia-cprng" name = "fuchsia-cprng"
version = "0.1.1" version = "0.1.1"
@ -1648,7 +1623,6 @@ dependencies = [
"dirs-next", "dirs-next",
"env_logger 0.9.0", "env_logger 0.9.0",
"filetime", "filetime",
"fs_extra",
"futures", "futures",
"futures-util", "futures-util",
"lazy_static", "lazy_static",
@ -1724,26 +1698,6 @@ dependencies = [
"tiff", "tiff",
] ]
[[package]]
name = "inotify"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f"
dependencies = [
"bitflags",
"inotify-sys",
"libc",
]
[[package]]
name = "inotify-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -2106,18 +2060,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "mio-extras"
version = "2.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19"
dependencies = [
"lazycell",
"log",
"mio 0.6.23",
"slab",
]
[[package]] [[package]]
name = "mio-named-pipes" name = "mio-named-pipes"
version = "0.1.7" version = "0.1.7"
@ -2282,24 +2224,6 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "notify"
version = "4.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae03c8c853dba7bfd23e571ff0cff7bc9dceb40a4cd684cd1681824183f45257"
dependencies = [
"bitflags",
"filetime",
"fsevent",
"fsevent-sys",
"inotify",
"libc",
"mio 0.6.23",
"mio-extras",
"walkdir",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "ntapi" name = "ntapi"
version = "0.3.6" version = "0.3.6"
@ -3229,7 +3153,6 @@ dependencies = [
"mac_address", "mac_address",
"machine-uid", "machine-uid",
"magnum-opus", "magnum-opus",
"notify",
"objc", "objc",
"parity-tokio-ipc", "parity-tokio-ipc",
"psutil", "psutil",

View File

@ -69,7 +69,6 @@ cocoa = "0.24"
dispatch = "0.2" dispatch = "0.2"
core-foundation = "0.9" core-foundation = "0.9"
core-graphics = "0.22" core-graphics = "0.22"
notify = "4.0.17"
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
libpulse-simple-binding = "2.24" libpulse-simple-binding = "2.24"

View File

@ -43,9 +43,6 @@ protobuf-codegen-pure = "3.0.0-alpha.2"
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winuser"] } winapi = { version = "0.3", features = ["winuser"] }
[target.'cfg(target_os = "macos")'.dependencies]
fs_extra = "1.2.0"
[dev-dependencies] [dev-dependencies]
toml = "0.5" toml = "0.5"
serde_json = "1.0" serde_json = "1.0"

View File

@ -11,7 +11,6 @@ use std::{
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
time::SystemTime, time::SystemTime,
}; };
use std::borrow::Borrow;
pub const APP_NAME: &str = "RustDesk"; pub const APP_NAME: &str = "RustDesk";
pub const RENDEZVOUS_TIMEOUT: u64 = 12_000; pub const RENDEZVOUS_TIMEOUT: u64 = 12_000;
@ -70,8 +69,7 @@ pub struct Config {
#[serde(default)] #[serde(default)]
salt: String, salt: String,
#[serde(default)] #[serde(default)]
key_pair: (Vec<u8>, Vec<u8>), key_pair: (Vec<u8>, Vec<u8>), // sk, pk
// sk, pk
#[serde(default)] #[serde(default)]
key_confirmed: bool, key_confirmed: bool,
#[serde(default)] #[serde(default)]
@ -92,8 +90,7 @@ pub struct Socks5Server {
#[derive(Debug, Default, Serialize, Deserialize, Clone)] #[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Config2 { pub struct Config2 {
#[serde(default)] #[serde(default)]
remote_id: String, remote_id: String, // latest used one
// latest used one
#[serde(default)] #[serde(default)]
size: Size, size: Size,
#[serde(default)] #[serde(default)]
@ -122,8 +119,7 @@ pub struct PeerConfig {
#[serde(default)] #[serde(default)]
pub size_pf: Size, pub size_pf: Size,
#[serde(default)] #[serde(default)]
pub view_style: String, pub view_style: String, // original (default), scale
// original (default), scale
#[serde(default)] #[serde(default)]
pub image_quality: String, pub image_quality: String,
#[serde(default)] #[serde(default)]
@ -163,28 +159,28 @@ pub struct PeerInfoSerde {
fn patch(path: PathBuf) -> PathBuf { fn patch(path: PathBuf) -> PathBuf {
if let Some(_tmp) = path.to_str() { if let Some(_tmp) = path.to_str() {
#[cfg(windows)] #[cfg(windows)]
return _tmp return _tmp
.replace( .replace(
"system32\\config\\systemprofile", "system32\\config\\systemprofile",
"ServiceProfiles\\LocalService", "ServiceProfiles\\LocalService",
) )
.into(); .into();
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
return _tmp.replace("Application Support", "Preferences").into(); return _tmp.replace("Application Support", "Preferences").into();
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
if _tmp == "/root" { if _tmp == "/root" {
if let Ok(output) = std::process::Command::new("whoami").output() { if let Ok(output) = std::process::Command::new("whoami").output() {
let user = String::from_utf8_lossy(&output.stdout) let user = String::from_utf8_lossy(&output.stdout)
.to_string() .to_string()
.trim() .trim()
.to_owned(); .to_owned();
if user != "root" { if user != "root" {
return format!("/home/{}", user).into(); return format!("/home/{}", user).into();
}
} }
} }
} }
}
} }
path path
} }
@ -194,11 +190,6 @@ impl Config2 {
Config::load_::<Config2>("2") Config::load_::<Config2>("2")
} }
fn reload(&mut self) {
let new_config = Config2::load();
*self = new_config;
}
fn store(&self) { fn store(&self) {
Config::store_(self, "2"); Config::store_(self, "2");
} }
@ -238,11 +229,6 @@ impl Config {
Config::store_(self, ""); Config::store_(self, "");
} }
fn reload(&mut self) {
let new_config = Config::load();
*self = new_config;
}
pub fn file() -> PathBuf { pub fn file() -> PathBuf {
Self::file_("") Self::file_("")
} }
@ -276,7 +262,7 @@ impl Config {
pub fn get_home() -> PathBuf { pub fn get_home() -> PathBuf {
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
return Self::path(""); return Self::path("");
if let Some(path) = dirs_next::home_dir() { if let Some(path) = dirs_next::home_dir() {
patch(path) patch(path)
} else if let Ok(path) = std::env::current_dir() { } else if let Ok(path) = std::env::current_dir() {
@ -286,17 +272,17 @@ impl Config {
} }
} }
pub fn path<P: AsRef<Path>>(p: P) -> PathBuf { fn path<P: AsRef<Path>>(p: P) -> PathBuf {
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
{ {
let mut path: PathBuf = APP_DIR.read().unwrap().clone().into(); let mut path: PathBuf = APP_DIR.read().unwrap().clone().into();
path.push(p); path.push(p);
return path; return path;
} }
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
let org = ""; let org = "";
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let org = ORG; let org = ORG;
// /var/root for root // /var/root for root
if let Some(project) = ProjectDirs::from("", org, APP_NAME) { if let Some(project) = ProjectDirs::from("", org, APP_NAME) {
let mut path = patch(project.config_dir().to_path_buf()); let mut path = patch(project.config_dir().to_path_buf());
@ -309,19 +295,19 @@ impl Config {
#[allow(unreachable_code)] #[allow(unreachable_code)]
pub fn log_path() -> PathBuf { pub fn log_path() -> PathBuf {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
if let Some(path) = dirs_next::home_dir().as_mut() { if let Some(path) = dirs_next::home_dir().as_mut() {
path.push(format!("Library/Logs/{}", APP_NAME)); path.push(format!("Library/Logs/{}", APP_NAME));
return path.clone(); return path.clone();
}
} }
}
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
let mut path = Self::get_home(); let mut path = Self::get_home();
path.push(format!(".local/share/logs/{}", APP_NAME)); path.push(format!(".local/share/logs/{}", APP_NAME));
std::fs::create_dir_all(&path).ok(); std::fs::create_dir_all(&path).ok();
return path; return path;
} }
if let Some(path) = Self::path("").parent() { if let Some(path) = Self::path("").parent() {
let mut path: PathBuf = path.into(); let mut path: PathBuf = path.into();
path.push("log"); path.push("log");
@ -332,21 +318,21 @@ impl Config {
pub fn ipc_path(postfix: &str) -> String { pub fn ipc_path(postfix: &str) -> String {
#[cfg(windows)] #[cfg(windows)]
{ {
// \\ServerName\pipe\PipeName // \\ServerName\pipe\PipeName
// where ServerName is either the name of a remote computer or a period, to specify the local computer. // where ServerName is either the name of a remote computer or a period, to specify the local computer.
// https://docs.microsoft.com/en-us/windows/win32/ipc/pipe-names // https://docs.microsoft.com/en-us/windows/win32/ipc/pipe-names
format!("\\\\.\\pipe\\{}\\query{}", APP_NAME, postfix) format!("\\\\.\\pipe\\{}\\query{}", APP_NAME, postfix)
} }
#[cfg(not(windows))] #[cfg(not(windows))]
{ {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
let mut path: PathBuf = format!("/tmp/{}", APP_NAME).into(); let mut path: PathBuf = format!("/tmp/{}", APP_NAME).into();
fs::create_dir(&path).ok(); fs::create_dir(&path).ok();
fs::set_permissions(&path, fs::Permissions::from_mode(0o0777)).ok(); fs::set_permissions(&path, fs::Permissions::from_mode(0o0777)).ok();
path.push(format!("ipc{}", postfix)); path.push(format!("ipc{}", postfix));
path.to_str().unwrap_or("").to_owned() path.to_str().unwrap_or("").to_owned()
} }
} }
pub fn icon_path() -> PathBuf { pub fn icon_path() -> PathBuf {
@ -460,7 +446,7 @@ impl Config {
fn get_auto_id() -> Option<String> { fn get_auto_id() -> Option<String> {
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
return None; return None;
let mut id = 0u32; let mut id = 0u32;
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Ok(Some(ma)) = mac_address::get_mac_address() { if let Ok(Some(ma)) = mac_address::get_mac_address() {
@ -673,45 +659,6 @@ impl Config {
Some(_) => NetworkType::ProxySocks, Some(_) => NetworkType::ProxySocks,
} }
} }
pub fn copy_and_reload_config_dir<P: AsRef<Path>>(
target_username: String,
from: P,
) -> Result<bool, fs_extra::error::Error> {
let to = Self::path("");
let to_parent = to.parent().unwrap();
let mut options = fs_extra::dir::CopyOptions::new();
options.overwrite = true;
options.copy_inside = true;
let mut f = from.as_ref();
return match fs_extra::dir::copy(f, to_parent, &options) {
Ok(count) => {
if count > 0 {
log::info!("{}",target_username);
log::info!("{}",f.to_str().unwrap().to_string());
log::info!("{}",to.to_str().unwrap().to_string());
std::process::Command::new("chown")
.arg("-R")
.arg(target_username)
.arg(to.to_str().unwrap().to_string())
.spawn();
CONFIG.write().unwrap().reload();
CONFIG2.write().unwrap().reload();
}
Ok(count > 0)
}
Err(e) => {
log::error!("config copy failed: {}", e);
Err(e)
}
};
}
} }
const PEERS: &str = "peers"; const PEERS: &str = "peers";
@ -810,7 +757,6 @@ impl Fav {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_serialize() { fn test_serialize() {
let cfg: Config = Default::default(); let cfg: Config = Default::default();

View File

@ -1,10 +1,19 @@
use hbb_common::{allow_err, bail, bytes, bytes_codec::BytesCodec, config::{self, Config}, futures::StreamExt as _, futures_util::sink::SinkExt, log, timeout, tokio, tokio::io::{AsyncRead, AsyncWrite}, tokio_util::codec::Framed, ResultType}; use hbb_common::{
allow_err, bail, bytes,
bytes_codec::BytesCodec,
config::{self, Config},
futures::StreamExt as _,
futures_util::sink::SinkExt,
log, timeout, tokio,
tokio::io::{AsyncRead, AsyncWrite},
tokio_util::codec::Framed,
ResultType,
};
use parity_tokio_ipc::{ use parity_tokio_ipc::{
Connection as Conn, ConnectionClient as ConnClient, Endpoint, Incoming, SecurityAttributes, Connection as Conn, ConnectionClient as ConnClient, Endpoint, Incoming, SecurityAttributes,
}; };
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf;
#[cfg(not(windows))] #[cfg(not(windows))]
use std::{fs::File, io::prelude::*}; use std::{fs::File, io::prelude::*};
@ -83,11 +92,6 @@ pub enum Data {
Socks(Option<config::Socks5Server>), Socks(Option<config::Socks5Server>),
FS(FS), FS(FS),
Test, Test,
ConfigCopyReq {
target_username: String,
dir_path: String,
},
ConfigCopyResp(Option<bool>),
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
@ -125,7 +129,7 @@ pub async fn start(postfix: &str) -> ResultType<()> {
pub async fn new_listener(postfix: &str) -> ResultType<Incoming> { pub async fn new_listener(postfix: &str) -> ResultType<Incoming> {
let path = Config::ipc_path(postfix); let path = Config::ipc_path(postfix);
#[cfg(not(windows))] #[cfg(not(windows))]
check_pid(postfix).await; check_pid(postfix).await;
let mut endpoint = Endpoint::new(path.clone()); let mut endpoint = Endpoint::new(path.clone());
match SecurityAttributes::allow_everyone_create() { match SecurityAttributes::allow_everyone_create() {
Ok(attr) => endpoint.set_security_attributes(attr), Ok(attr) => endpoint.set_security_attributes(attr),
@ -135,11 +139,11 @@ pub async fn new_listener(postfix: &str) -> ResultType<Incoming> {
Ok(incoming) => { Ok(incoming) => {
log::info!("Started ipc{} server at path: {}", postfix, &path); log::info!("Started ipc{} server at path: {}", postfix, &path);
#[cfg(not(windows))] #[cfg(not(windows))]
{ {
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o0777)).ok(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o0777)).ok();
write_pid(postfix); write_pid(postfix);
} }
Ok(incoming) Ok(incoming)
} }
Err(err) => { Err(err) => {
@ -248,23 +252,6 @@ async fn handle(data: Data, stream: &mut Connection) {
let t = Config::get_nat_type(); let t = Config::get_nat_type();
allow_err!(stream.send(&Data::NatType(Some(t))).await); allow_err!(stream.send(&Data::NatType(Some(t))).await);
} }
Data::ConfigCopyReq { target_username, dir_path } => {
let from = PathBuf::from(dir_path);
if !from.exists() {
allow_err!(stream.send(&Data::ConfigCopyResp(None)).await);
return;
}
match Config::copy_and_reload_config_dir(target_username, from) {
Ok(result) => {
allow_err!(stream.send(&Data::ConfigCopyResp(Some(result))).await);
}
Err(e) => {
log::error!("copy_and_reload_config_dir failed: {:?}",e);
allow_err!(stream.send(&Data::ConfigCopyResp(Some(false))).await);
}
}
}
_ => {} _ => {}
} }
} }
@ -325,8 +312,8 @@ pub struct ConnectionTmpl<T> {
pub type Connection = ConnectionTmpl<Conn>; pub type Connection = ConnectionTmpl<Conn>;
impl<T> ConnectionTmpl<T> impl<T> ConnectionTmpl<T>
where where
T: AsyncRead + AsyncWrite + std::marker::Unpin, T: AsyncRead + AsyncWrite + std::marker::Unpin,
{ {
pub fn new(conn: T) -> Self { pub fn new(conn: T) -> Self {
Self { Self {

View File

@ -27,4 +27,4 @@ use common::*;
pub mod cli; pub mod cli;
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
mod port_forward; mod port_forward;
mod lang; mod lang;

View File

@ -10,7 +10,7 @@ fn main() {
common::test_rendezvous_server(); common::test_rendezvous_server();
common::test_nat_type(); common::test_nat_type();
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
crate::common::check_software_update(); crate::common::check_software_update();
mobile::Session::start(""); mobile::Session::start("");
} }
@ -29,53 +29,53 @@ fn main() {
return; return;
} }
#[cfg(not(feature = "inline"))] #[cfg(not(feature = "inline"))]
{ {
use hbb_common::env_logger::*; use hbb_common::env_logger::*;
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info")); init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
} }
#[cfg(feature = "inline")] #[cfg(feature = "inline")]
{ {
let mut path = hbb_common::config::Config::log_path(); let mut path = hbb_common::config::Config::log_path();
if args.len() > 0 && args[0].starts_with("--") { if args.len() > 0 && args[0].starts_with("--") {
let name = args[0].replace("--", ""); let name = args[0].replace("--", "");
if !name.is_empty() { if !name.is_empty() {
path.push(name); path.push(name);
}
} }
use flexi_logger::*;
Logger::try_with_env_or_str("debug")
.map(|x| {
x.log_to_file(FileSpec::default().directory(path))
.format(opt_format)
.rotate(
Criterion::Age(Age::Day),
Naming::Timestamps,
Cleanup::KeepLogFiles(6),
)
.start()
.ok();
})
.ok();
} }
use flexi_logger::*;
Logger::try_with_env_or_str("debug")
.map(|x| {
x.log_to_file(FileSpec::default().directory(path))
.format(opt_format)
.rotate(
Criterion::Age(Age::Day),
Naming::Timestamps,
Cleanup::KeepLogFiles(6),
)
.start()
.ok();
})
.ok();
}
if args.is_empty() { if args.is_empty() {
std::thread::spawn(move || start_server(false, false)); std::thread::spawn(move || start_server(false, false));
} else { } else {
#[cfg(windows)] #[cfg(windows)]
{ {
if args[0] == "--uninstall" { if args[0] == "--uninstall" {
if let Err(err) = platform::uninstall_me() { if let Err(err) = platform::uninstall_me() {
log::error!("Failed to uninstall: {}", err); log::error!("Failed to uninstall: {}", err);
}
return;
} else if args[0] == "--update" {
hbb_common::allow_err!(platform::update_me());
return;
} else if args[0] == "--reinstall" {
hbb_common::allow_err!(platform::uninstall_me());
hbb_common::allow_err!(platform::install_me("desktopicon startmenu",));
return;
} }
return;
} else if args[0] == "--update" {
hbb_common::allow_err!(platform::update_me());
return;
} else if args[0] == "--reinstall" {
hbb_common::allow_err!(platform::uninstall_me());
hbb_common::allow_err!(platform::install_me("desktopicon startmenu",));
return;
} }
}
if args[0] == "--remove" { if args[0] == "--remove" {
if args.len() == 2 { if args.len() == 2 {
// sleep a while so that process of removed exe exit // sleep a while so that process of removed exe exit
@ -101,10 +101,6 @@ fn main() {
ipc::set_password(args[1].to_owned()).unwrap(); ipc::set_password(args[1].to_owned()).unwrap();
} }
return; return;
} else if cfg!(target_os = "macos") && args[0] == "--daemon" {
log::info!("start --daemon");
crate::platform::start_daemon();
return;
} }
} }
ui::start(&mut args[..]); ui::start(&mut args[..]);

View File

@ -333,10 +333,3 @@ pub fn block_input(_v: bool) {
pub fn is_installed() -> bool { pub fn is_installed() -> bool {
true true
} }
pub fn start_daemon(){
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::{ConnectionTmpl, Data}; use crate::ipc::Data;
use connection::{ConnInner, Connection}; use connection::{ConnInner, Connection};
use hbb_common::{ use hbb_common::{
allow_err, allow_err,
@ -9,24 +9,18 @@ use hbb_common::{
message_proto::*, message_proto::*,
protobuf::{Message as _, ProtobufEnum}, protobuf::{Message as _, ProtobufEnum},
rendezvous_proto::*, rendezvous_proto::*,
sleep, socket_client, sleep,
sodiumoxide::crypto::{box_, secretbox, sign}, sodiumoxide::crypto::{box_, secretbox, sign},
timeout, tokio, ResultType, Stream, timeout, tokio, ResultType, Stream,
socket_client,
}; };
use service::{GenericService, Service, ServiceTmpl, Subscriber}; use service::{GenericService, Service, ServiceTmpl, Subscriber};
use std::sync::mpsc::RecvError;
use std::time::Duration;
use std::{ use std::{
collections::HashMap, collections::HashMap,
net::SocketAddr, net::SocketAddr,
sync::{Arc, Mutex, RwLock, Weak}, sync::{Arc, Mutex, RwLock, Weak},
}; };
use hbb_common::log::info;
#[cfg(target_os = "macos")]
use notify::{watcher, RecursiveMode, Watcher};
use parity_tokio_ipc::ConnectionClient;
mod audio_service; mod audio_service;
mod clipboard_service; mod clipboard_service;
mod connection; mod connection;
@ -172,7 +166,7 @@ pub async fn create_relay_connection(
secure: bool, secure: bool,
) { ) {
if let Err(err) = if let Err(err) =
create_relay_connection_(server, relay_server, uuid.clone(), peer_addr, secure).await create_relay_connection_(server, relay_server, uuid.clone(), peer_addr, secure).await
{ {
log::error!( log::error!(
"Failed to create relay connection for {} with uuid {}: {}", "Failed to create relay connection for {} with uuid {}: {}",
@ -195,7 +189,7 @@ async fn create_relay_connection_(
Config::get_any_listen_addr(), Config::get_any_listen_addr(),
CONNECT_TIMEOUT, CONNECT_TIMEOUT,
) )
.await?; .await?;
let mut msg_out = RendezvousMessage::new(); let mut msg_out = RendezvousMessage::new();
msg_out.set_request_relay(RequestRelay { msg_out.set_request_relay(RequestRelay {
uuid, uuid,
@ -270,13 +264,10 @@ pub fn check_zombie() {
#[tokio::main] #[tokio::main]
pub async fn start_server(is_server: bool, _tray: bool) { pub async fn start_server(is_server: bool, _tray: bool) {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
log::info!("DISPLAY={:?}", std::env::var("DISPLAY")); log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY")); log::info!("XAUTHORITY={:?}", std::env::var("XAUTHORITY"));
} }
sync_and_watch_config_dir().await;
if is_server { if is_server {
std::thread::spawn(move || { std::thread::spawn(move || {
if let Err(err) = crate::ipc::start("") { if let Err(err) = crate::ipc::start("") {
@ -307,7 +298,7 @@ pub async fn start_server(is_server: bool, _tray: bool) {
} else { } else {
allow_err!(conn.send(&Data::ConfirmedKey(None)).await); allow_err!(conn.send(&Data::ConfirmedKey(None)).await);
if let Ok(Some(Data::ConfirmedKey(Some(pair)))) = if let Ok(Some(Data::ConfirmedKey(Some(pair)))) =
conn.next_timeout(1000).await conn.next_timeout(1000).await
{ {
Config::set_key_pair(pair); Config::set_key_pair(pair);
Config::set_key_confirmed(true); Config::set_key_confirmed(true);
@ -326,72 +317,3 @@ pub async fn start_server(is_server: bool, _tray: bool) {
} }
} }
} }
async fn sync_and_watch_config_dir() -> ResultType<()> {
let mut conn = crate::ipc::connect(1000, "_daemon").await?;
sync_config_dir(&mut conn, "/var/root/Library/Preferences/com.carriez.RustDesk/".to_string()).await?;
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(1)).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()
);
sync_config_dir(&mut conn, Config::path("").to_str().unwrap().to_string()).await;
}
x => {
log::info!("another {:?}", x)
}
},
Err(e) => println!("watch error: {:?}", e),
}
}
});
Ok(())
}
async fn sync_config_dir(conn: &mut ConnectionTmpl<ConnectionClient>, path: String) -> ResultType<()> {
allow_err!(
conn.send(&Data::ConfigCopyReq {
target_username: crate::username(),
dir_path: path
})
.await
);
if let Ok(Some(data)) = conn.next_timeout(1000).await {
match data {
Data::ConfigCopyResp(result) => match result {
Some(success) => {
if success {
log::info!("copy and reload config dir success");
} else {
log::info!("copy config dir failed. may be first running");
}
}
None => {}
},
x => {
log::info!("receive another {:?}",x)
}
};
};
Ok(())
}