feat: support to grant permission to clash core

This commit is contained in:
GyDi
2023-03-16 11:16:54 +08:00
parent fdcbc3904a
commit 9e2812d55c
7 changed files with 177 additions and 3 deletions

View File

@@ -175,6 +175,15 @@ pub async fn restart_sidecar() -> CmdResult {
wrap_err!(CoreManager::global().run_core().await)
}
#[tauri::command]
pub fn grant_permission(core: String) -> CmdResult {
#[cfg(target_os = "macos")]
return wrap_err!(manager::grant_permission(core));
#[cfg(not(target_os = "macos"))]
return Err("Unsupported target");
}
/// get the system proxy
#[tauri::command]
pub fn get_sys_proxy() -> CmdResult<Mapping> {

View File

@@ -0,0 +1,37 @@
/// 给clash内核的tun模式授权
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn grant_permission(core: String) -> anyhow::Result<()> {
use std::process::Command;
use tauri::utils::platform::current_exe;
let path = current_exe()?.with_file_name(core).canonicalize()?;
let path = path.display();
log::debug!("grant_permission path: {path}");
#[cfg(target_os = "macos")]
let output = {
let shell = format!("chown root:admin {path}\nchmod +sx {path}");
let command = format!(r#"do shell script "{shell}" with administrator privileges"#);
Command::new("osascript")
.args(vec!["-e", &command])
.output()?
};
#[cfg(target_os = "linux")]
let output = {
let shell = format!("setcap cap_net_bind_service,cap_net_admin=+ep {path}");
Command::new("sudo")
.arg("sh")
.arg("-c")
.arg(shell)
.output()?
};
if output.status.success() {
Ok(())
} else {
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("");
anyhow::bail!("{stderr}");
}
}

View File

@@ -3,6 +3,7 @@ mod core;
pub mod handle;
pub mod hotkey;
pub mod logger;
pub mod manager;
pub mod sysopt;
pub mod timer;
pub mod tray;

View File

@@ -36,6 +36,7 @@ fn main() -> std::io::Result<()> {
cmds::open_core_dir,
// cmds::kill_sidecar,
cmds::restart_sidecar,
cmds::grant_permission,
// clash
cmds::get_clash_info,
cmds::get_clash_logs,