Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/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 client/bin/src/fornet_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub async fn main() -> anyhow::Result<()> {
Command::new("list").about("list connected network status, it now only support connect one network"),
);

if cfg!(target_os = "macos") {
if cfg!(any(target_os = "macos", target_os = "windows")) {
command = command.subcommand(
Command::new("autoLaunch")
.about("auto launch when os up")
Expand Down
2 changes: 1 addition & 1 deletion client/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ shell-candy = "0.4"
cmd_lib = "1.3"
tempfile = "3.5"

auto-launch-extra = {version = "0.4.0", package = "auto-launch"}
auto-launch-extra = {version = "0.5", package = "auto-launch"}

mqrstt = "0.2.1"
tokio-rustls = "0.24.1"
Expand Down
2 changes: 1 addition & 1 deletion client/lib/src/api/file_socket_api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl FileSocketApiServer {
}
"autoLaunch" => {
cfg_if! {
if #[cfg(target_os="macos")] {
if #[cfg(any(target_os="macos", target_os = "windows"))] {
match crate::client_manager::auto_launch(command[1]).await {
Ok(resp) => {
let _ = stream.write(api_success(resp).to_json().as_bytes()).await;
Expand Down
32 changes: 25 additions & 7 deletions client/lib/src/client_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,19 @@ pub async fn command_handle_server_message(client:Arc<RwLock<ForNetClient>>, mes
};
}

#[cfg(target_os = "macos")]
pub async fn auto_launch(param:&str)->anyhow::Result<String> {
match std::env::current_dir() {
Ok(x) => {
let app_path = x.join(crate::APP_NAME);
let auto = crate::device::auto_launch::AutoLaunch::new( crate::MAC_OS_PACKAGE_NAME.to_owned(), app_path.to_str().unwrap().to_owned());

tracing::debug!("app name:{}, app path: {:?}",crate::APP_NAME, app_path);
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub async fn auto_launch(param:&str)->anyhow::Result<String> {
match std::env::current_exe() {
Ok(app_path) => {
let name = app_path.file_name().unwrap().to_str().unwrap().to_owned();
//std::env::current_exe()
#[cfg(target_os = "macos")]
let auto = crate::device::auto_launch::AutoLaunch::new(crate::MAC_OS_PACKAGE_NAME.to_owned(), app_path.to_str().unwrap().to_owned())?;
#[cfg(target_os = "windows")]
let auto =crate::device::auto_launch::AutoLaunch::new(name.clone(),app_path.to_str().unwrap().to_owned())?;

tracing::debug!("auto launch app name:{}, app path: {:?} command: {}",name, app_path, param);
let is_enabled = auto.is_enabled();
match param {
"enable" => {
Expand Down Expand Up @@ -448,4 +453,17 @@ pub enum ServerMessage {
StopWR{network_id:String,reason:String, delete_network:bool, },
SyncPeers(String, crate::protobuf::config::PeerChange),
SyncConfig(String, WrConfig),
}

#[cfg(test)]
mod test {
#[test]
fn test_name() {
println!("hello world");
let app_name = std::env::current_exe();
assert!(app_name.is_ok());
let app_name = app_name.unwrap().as_os_str().to_str().unwrap();
println!("{:?}", std::env::current_exe())

}
}
4 changes: 2 additions & 2 deletions client/lib/src/device/auto_launch/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct AutoLaunch {
const LAUNCH_ROOT_PATH:&str = "/Library/LaunchDaemons";

impl AutoLaunch {
pub fn new(app_name:String, path:String) -> Self {
AutoLaunch {app_name, path}
pub fn new(app_name:String, path:String) -> anyhow::Result<Self> {
Ok(AutoLaunch {app_name, path})
}
pub fn is_enabled(&self) -> anyhow::Result<bool> {

Expand Down
5 changes: 5 additions & 0 deletions client/lib/src/device/auto_launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ cfg_if! {
if #[cfg(target_os="macos")] {
mod macos;
pub use macos::AutoLaunch;
} else if #[cfg(target_os="windows")] {
//mod windows;

}
}

mod windows;
pub use self::windows::AutoLaunch;
//TODO: test other platform with auto-launch-extra, be careful about root permission!
30 changes: 30 additions & 0 deletions client/lib/src/device/auto_launch/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anyhow::ensure;
use auto_launch_extra::*;

pub struct AutoLaunch {
inner: auto_launch_extra::AutoLaunch
}

impl AutoLaunch {
pub fn new(app_name:String, path:String) -> anyhow::Result<Self> {
let auto = AutoLaunchBuilder::new()
.set_app_name(&app_name)
.set_app_path(&path)
//.set_args(&["--minimized"]) // would stop self
.build().map_err(anyhow::Error::from)?;
Ok(AutoLaunch {
inner:auto
})
}

pub fn is_enabled(&self) -> anyhow::Result<bool> {
self.inner.is_enabled().map_err(anyhow::Error::from)
}
pub fn enable(&self) -> anyhow::Result<()> {
self.inner.enable().map_err(anyhow::Error::from)
}

pub fn disable(&self) -> anyhow::Result<()> {
self.inner.disable().map_err(anyhow::Error::from)
}
}
5 changes: 4 additions & 1 deletion client/lib/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ impl Default for Peers {
}
}

/*

#[cfg(test)]
mod test {
Expand Down Expand Up @@ -770,4 +771,6 @@ mod test {
tokio::time::sleep(Duration::from_secs(5)).await;
device.close().await;
}
}
}

*/