From 1880da63516f9bba32cf4ca7a6934dbf23081e6c Mon Sep 17 00:00:00 2001 From: GyDi Date: Thu, 17 Nov 2022 22:52:22 +0800 Subject: [PATCH] refactor: rm dead code --- src-tauri/src/cmds.rs | 24 +-- src-tauri/src/core/mod.rs | 336 ------------------------------ src-tauri/src/core/win_service.rs | 43 ---- src-tauri/src/main.rs | 2 - src/services/cmds.ts | 8 - 5 files changed, 1 insertion(+), 412 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 8b09293..2dbb5e4 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -256,23 +256,9 @@ pub mod service { use super::*; use crate::core::win_service; - #[tauri::command] - pub async fn start_service() -> CmdResult { - wrap_err!(win_service::start_service().await) - } - - #[tauri::command] - pub async fn stop_service() -> CmdResult { - wrap_err!(win_service::stop_service().await) - } - #[tauri::command] pub async fn check_service() -> CmdResult { - // no log - match win_service::check_service().await { - Ok(res) => Ok(res), - Err(err) => Err(err.to_string()), - } + wrap_err!(win_service::check_service().await) } #[tauri::command] @@ -290,14 +276,6 @@ pub mod service { pub mod service { use super::*; - #[tauri::command] - pub async fn start_service() -> CmdResult { - Ok(()) - } - #[tauri::command] - pub async fn stop_service() -> CmdResult { - Ok(()) - } #[tauri::command] pub async fn check_service() -> CmdResult { Ok(()) diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 20e5600..18655ab 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -1,347 +1,11 @@ -// use self::handle::Handle; -// use self::hotkey::Hotkey; -// use self::sysopt::Sysopt; -// use self::timer::Timer; -// // use crate::data::*; -// // use crate::enhance::enhance_config; -// use crate::log_if_err; -// use anyhow::{bail, Result}; -// use once_cell::sync::OnceCell; -// use parking_lot::Mutex; -// use serde_yaml::{Mapping, Value}; -// use std::sync::Arc; - pub mod clash_api; mod core; pub mod handle; pub mod hotkey; pub mod logger; -// pub mod service; pub mod sysopt; pub mod timer; pub mod tray; pub mod win_service; pub use self::core::*; -// pub use self::service::*; - -#[derive(Clone)] -pub struct Core {} - -impl Core { - // pub fn global() -> &'static Core { - // static CORE: OnceCell = OnceCell::new(); - - // CORE.get_or_init(|| Core { - // service: Arc::new(Mutex::new(Service::new())), - // sysopt: Arc::new(Mutex::new(Sysopt::new())), - // timer: Arc::new(Mutex::new(Timer::new())), - // hotkey: Arc::new(Mutex::new(Hotkey::new())), - // runtime: Arc::new(Mutex::new(RuntimeResult::default())), - // handle: Arc::new(Mutex::new(Handle::default())), - // }) - // } - - // /// initialize the core state - // pub fn init(&self, app_handle: tauri::AppHandle) { - // kill old clash process - // Service::kill_old_clash(); - - // let mut handle = self.handle.lock(); - // handle.set_inner(app_handle.clone()); - // drop(handle); - - // let mut service = self.service.lock(); - // log_if_err!(service.start()); - // drop(service); - - // log_if_err!(self.activate()); - - // let mut sysopt = self.sysopt.lock(); - // log_if_err!(sysopt.init_launch()); - // log_if_err!(sysopt.init_sysproxy()); - // drop(sysopt); - - // let handle = self.handle.lock(); - // log_if_err!(handle.update_systray_part()); - // drop(handle); - - // let mut hotkey = self.hotkey.lock(); - // log_if_err!(hotkey.init(app_handle)); - // drop(hotkey); - - // // timer initialize - // let mut timer = self.timer.lock(); - // log_if_err!(timer.restore()); - // } - - // /// restart the clash sidecar - // pub fn restart_clash(&self) -> Result<()> { - // let mut service = self.service.lock(); - // service.restart()?; - // drop(service); - // self.activate() - // } - - // /// change the clash core - // pub fn change_core(&self, clash_core: Option) -> Result<()> { - // let clash_core = clash_core.unwrap_or("clash".into()); - - // if &clash_core != "clash" && &clash_core != "clash-meta" { - // bail!("invalid clash core name \"{clash_core}\""); - // } - - // let global = Data::global(); - // let mut verge = global.verge.lock(); - // verge.patch_config(Verge { - // clash_core: Some(clash_core.clone()), - // ..Verge::default() - // })?; - // drop(verge); - - // let mut service = self.service.lock(); - // service.clear_logs(); - // service.restart()?; - // drop(service); - - // self.activate() - // } - - // /// Patch Clash - // /// handle the clash config changed - // pub fn patch_clash(&self, patch: Mapping) -> Result<()> { - // let patch_cloned = patch.clone(); - // let clash_mode = patch.get("mode"); - // let mixed_port = patch.get("mixed-port"); - // let external = patch.get("external-controller"); - // let secret = patch.get("secret"); - - // let valid_port = { - // let global = Data::global(); - // let mut clash = global.clash.lock(); - // clash.patch_config(patch_cloned)?; - // clash.info.port.is_some() - // }; - - // // todo: port check - // if (mixed_port.is_some() && valid_port) || external.is_some() || secret.is_some() { - // let mut service = self.service.lock(); - // service.restart()?; - // drop(service); - - // self.activate()?; - - // let mut sysopt = self.sysopt.lock(); - // sysopt.init_sysproxy()?; - // } - - // if clash_mode.is_some() { - // let handle = self.handle.lock(); - // handle.update_systray_part()?; - // } - - // Ok(()) - // } - - // /// Patch Verge - // pub fn patch_verge(&self, patch: Verge) -> Result<()> { - // // save the patch - // let global = Data::global(); - // let mut verge = global.verge.lock(); - // verge.patch_config(patch.clone())?; - // drop(verge); - - // let tun_mode = patch.enable_tun_mode; - // let auto_launch = patch.enable_auto_launch; - // let system_proxy = patch.enable_system_proxy; - // let proxy_bypass = patch.system_proxy_bypass; - // let proxy_guard = patch.enable_proxy_guard; - // let language = patch.language; - - // #[cfg(target_os = "windows")] - // { - // let service_mode = patch.enable_service_mode; - - // // 重启服务 - // if service_mode.is_some() { - // let mut service = self.service.lock(); - // service.restart()?; - // drop(service); - // } - - // if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) { - // let wintun_dll = crate::utils::dirs::app_home_dir().join("wintun.dll"); - // if !wintun_dll.exists() { - // bail!("failed to enable TUN for missing `wintun.dll`"); - // } - // } - - // if service_mode.is_some() || tun_mode.is_some() { - // self.activate()?; - // } - // } - - // #[cfg(not(target_os = "windows"))] - // if tun_mode.is_some() { - // self.activate()?; - // } - - // let mut sysopt = self.sysopt.lock(); - - // if auto_launch.is_some() { - // sysopt.update_launch()?; - // } - // if system_proxy.is_some() || proxy_bypass.is_some() { - // sysopt.update_sysproxy()?; - // sysopt.guard_proxy(); - // } - // if proxy_guard.unwrap_or(false) { - // sysopt.guard_proxy(); - // } - - // // 更新tray - // if language.is_some() { - // let handle = self.handle.lock(); - // handle.update_systray()?; - // } else if system_proxy.is_some() || tun_mode.is_some() { - // let handle = self.handle.lock(); - // handle.update_systray_part()?; - // } - - // if patch.hotkeys.is_some() { - // let mut hotkey = self.hotkey.lock(); - // hotkey.update(patch.hotkeys.unwrap())?; - // } - - // Ok(()) - // } - - // /// update rule/global/direct/script mode - // pub fn update_mode(&self, mode: &str) -> Result<()> { - // // save config to file - // let info = { - // let global = Data::global(); - // let mut clash = global.clash.lock(); - // clash.config.insert(Value::from("mode"), Value::from(mode)); - // clash.save_config()?; - // clash.info.clone() - // }; - - // let mut mapping = Mapping::new(); - // mapping.insert(Value::from("mode"), Value::from(mode)); - - // let handle = self.handle.clone(); - - // tauri::async_runtime::spawn(async move { - // log_if_err!(Service::patch_config(info, mapping.to_owned()).await); - - // // update tray - // let handle = handle.lock(); - // handle.refresh_clash(); - // log_if_err!(handle.update_systray_part()); - // }); - - // Ok(()) - // } - - // /// activate the profile - // /// auto activate enhanced profile - // /// 触发clash配置更新 - // pub fn activate(&self) -> Result<()> { - // let global = Data::global(); - - // let verge = global.verge.lock(); - // let clash = global.clash.lock(); - // let profiles = global.profiles.lock(); - - // let tun_mode = verge.enable_tun_mode.clone().unwrap_or(false); - // let profile_activate = profiles.gen_activate()?; - - // let clash_config = clash.config.clone(); - // let clash_info = clash.info.clone(); - - // drop(clash); - // drop(verge); - // drop(profiles); - - // let (config, exists_keys, logs) = enhance_config( - // clash_config, - // profile_activate.current, - // profile_activate.chain, - // profile_activate.valid, - // tun_mode, - // ); - - // let mut runtime = self.runtime.lock(); - // *runtime = RuntimeResult { - // config: Some(config.clone()), - // config_yaml: Some(serde_yaml::to_string(&config).unwrap_or("".into())), - // exists_keys, - // chain_logs: logs, - // }; - // drop(runtime); - - // let mut service = self.service.lock(); - // service.check_start()?; - // drop(service); - - // let handle = self.handle.clone(); - // tauri::async_runtime::spawn(async move { - // match Service::set_config(clash_info, config).await { - // Ok(_) => { - // let handle = handle.lock(); - // handle.refresh_clash(); - // handle.notice_message("set_config::ok".into(), "ok".into()); - // } - // Err(err) => { - // let handle = handle.lock(); - // handle.notice_message("set_config::error".into(), format!("{err}")); - // log::error!(target: "app", "{err}") - // } - // } - // }); - - // Ok(()) - // } - - // /// Static function - // /// update profile item - // pub async fn update_profile_item(&self, uid: String, option: Option) -> Result<()> { - // let global = Data::global(); - - // let (url, opt) = { - // let profiles = global.profiles.lock(); - // let item = profiles.get_item(&uid)?; - - // if let Some(typ) = item.itype.as_ref() { - // // maybe only valid for `local` profile - // if *typ != "remote" { - // // reactivate the config - // if Some(uid) == profiles.get_current() { - // drop(profiles); - // self.activate()?; - // } - // return Ok(()); - // } - // } - // if item.url.is_none() { - // bail!("failed to get the profile item url"); - // } - // (item.url.clone().unwrap(), item.option.clone()) - // }; - - // let merged_opt = PrfOption::merge(opt, option); - // let item = PrfItem::from_url(&url, None, None, merged_opt).await?; - - // let mut profiles = global.profiles.lock(); - // profiles.update_item(uid.clone(), item)?; - - // // reactivate the profile - // if Some(uid) == profiles.get_current() { - // drop(profiles); - // self.activate()?; - // } - - // Ok(()) - // } -} diff --git a/src-tauri/src/core/win_service.rs b/src-tauri/src/core/win_service.rs index b0ddb8f..3120474 100644 --- a/src-tauri/src/core/win_service.rs +++ b/src-tauri/src/core/win_service.rs @@ -91,49 +91,6 @@ pub async fn uninstall_service() -> Result<()> { Ok(()) } -/// start service -/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程 -#[deprecated] -pub async fn start_service() -> Result<()> { - let token = Token::with_current_process()?; - let level = token.privilege_level()?; - - let args = ["start", SERVICE_NAME]; - - let status = match level { - PrivilegeLevel::NotPrivileged => RunasCommand::new("sc").args(&args).status()?, - _ => StdCommand::new("sc").args(&args).status()?, - }; - - match status.success() { - true => Ok(()), - false => bail!( - "failed to start service with status {}", - status.code().unwrap() - ), - } -} - -/// stop service -pub async fn stop_service() -> Result<()> { - let url = format!("{SERVICE_URL}/stop_service"); - let res = reqwest::ClientBuilder::new() - .no_proxy() - .build()? - .post(url) - .send() - .await? - .json::() - .await - .context("failed to connect to the Clash Verge Service")?; - - if res.code != 0 { - bail!(res.msg); - } - - Ok(()) -} - /// check the windows service status pub async fn check_service() -> Result { let url = format!("{SERVICE_URL}/get_clash"); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1c89859..f78c4e8 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -63,8 +63,6 @@ fn main() -> std::io::Result<()> { cmds::read_profile_file, cmds::save_profile_file, // service mode - cmds::service::start_service, - cmds::service::stop_service, cmds::service::check_service, cmds::service::install_service, cmds::service::uninstall_service, diff --git a/src/services/cmds.ts b/src/services/cmds.ts index 9ab45fe..2203be6 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -158,14 +158,6 @@ export async function openWebUrl(url: string) { /// service mode -export async function startService() { - return invoke("start_service"); -} - -export async function stopService() { - return invoke("stop_service"); -} - export async function checkService() { try { const result = await invoke("check_service");