From dc941575fee2cd0524e1e5e65c85eb079949afdd Mon Sep 17 00:00:00 2001 From: GyDi Date: Mon, 14 Nov 2022 22:50:47 +0800 Subject: [PATCH] refactor: fix --- src-tauri/src/cmds.rs | 51 +++++++++++------------------------ src-tauri/src/config/clash.rs | 13 +-------- src-tauri/src/config/verge.rs | 1 + src-tauri/src/core/core.rs | 22 ++++++++------- 4 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index f622bb7..39e9bf4 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -44,49 +44,32 @@ pub async fn update_profile(index: String, option: Option) -> CmdResu } #[tauri::command] -pub fn select_profile(index: String) -> CmdResult { - let mut profiles = ProfilesN::global().config.lock(); - wrap_err!(profiles.put_current(index))?; - drop(profiles); - - wrap_err!(tauri::async_runtime::block_on(async { - CoreManager::global().activate_config().await - })) +pub async fn select_profile(index: String) -> CmdResult { + wrap_err!({ ProfilesN::global().config.lock().put_current(index) })?; + wrap_err!(CoreManager::global().activate_config().await) } /// change the profile chain #[tauri::command] -pub fn change_profile_chain(chain: Option>) -> CmdResult { - let mut profiles = ProfilesN::global().config.lock(); - wrap_err!(profiles.put_chain(chain))?; - drop(profiles); - - wrap_err!(tauri::async_runtime::block_on(async { - CoreManager::global().activate_config().await - })) +pub async fn change_profile_chain(chain: Option>) -> CmdResult { + wrap_err!({ ProfilesN::global().config.lock().put_chain(chain) })?; + wrap_err!(CoreManager::global().activate_config().await) } #[tauri::command] -pub fn change_profile_valid(valid: Option>) -> CmdResult { - let mut profiles = ProfilesN::global().config.lock(); - wrap_err!(profiles.put_valid(valid))?; - drop(profiles); - - wrap_err!(tauri::async_runtime::block_on(async { - CoreManager::global().activate_config().await - })) +pub async fn change_profile_valid(valid: Option>) -> CmdResult { + wrap_err!({ ProfilesN::global().config.lock().put_valid(valid) })?; + wrap_err!(CoreManager::global().activate_config().await) } #[tauri::command] -pub fn delete_profile(index: String) -> CmdResult { - let mut profiles = ProfilesN::global().config.lock(); - if wrap_err!(profiles.delete_item(index))? { - drop(profiles); +pub async fn delete_profile(index: String) -> CmdResult { + let should_update = { wrap_err!(ProfilesN::global().config.lock().delete_item(index))? }; - wrap_err!(tauri::async_runtime::block_on(async { - CoreManager::global().activate_config().await - }))?; + if should_update { + wrap_err!(CoreManager::global().activate_config().await)?; } + Ok(()) } @@ -185,10 +168,8 @@ pub fn patch_verge_config(payload: IVerge) -> CmdResult { } #[tauri::command] -pub fn change_clash_core(clash_core: Option) -> CmdResult { - wrap_err!(tauri::async_runtime::block_on(async { - CoreManager::global().change_core(clash_core).await - })) +pub async fn change_clash_core(clash_core: Option) -> CmdResult { + wrap_err!(CoreManager::global().change_core(clash_core).await) } /// restart the sidecar diff --git a/src-tauri/src/config/clash.rs b/src-tauri/src/config/clash.rs index e498a34..a00f3c9 100644 --- a/src-tauri/src/config/clash.rs +++ b/src-tauri/src/config/clash.rs @@ -58,22 +58,11 @@ impl ClashN { pub fn patch_config(&self, patch: Mapping) -> Result<()> { let mut config = self.config.lock(); - let port_key = Value::from("mixed-port"); - let server_key = Value::from("external-controller"); - let secret_key = Value::from("secret"); - - let change_info = patch.contains_key(&port_key) - || patch.contains_key(&server_key) - || patch.contains_key(&secret_key); - for (key, value) in patch.into_iter() { config.insert(key, value); } - if change_info { - let mut info = self.info.lock(); - *info = ClashInfoN::from(&*config); - } + drop(config); self.save_config() } diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 97b5f10..47593b7 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -67,6 +67,7 @@ impl VergeN { patch!(auto_close_connection); patch!(default_latency_test); + drop(config); self.save_file() } diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 92296e7..4c587aa 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -79,12 +79,12 @@ impl CoreManager { let clash_core = { self.clash_core.lock().clone() }; let output = Command::new_sidecar(clash_core)? - .args(["-t", config_path]) + .args(["-t", "-f", config_path]) .output()?; if !output.status.success() { - Logger::global().set_log(output.stderr.clone()); - bail!("{}", output.stderr); // 过滤掉终端颜色值 + Logger::global().set_log(output.stdout.clone()); + bail!("{}", output.stdout); // 过滤掉终端颜色值 } Ok(()) @@ -177,17 +177,19 @@ impl CoreManager { // 清掉旧日志 Logger::global().clear_log(); - let mut self_core = self.clash_core.lock(); - let old_core = self_core.clone(); // 保存一下旧值 - *self_core = clash_core.clone(); - drop(self_core); + let old_core = { + let mut self_core = self.clash_core.lock(); + let old_core = self_core.to_owned(); // 保存一下旧值 + *self_core = clash_core.clone(); + old_core + }; match self.run_core() { Ok(_) => { // 更新到配置文件 - let mut verge = VergeN::global().config.lock(); - verge.clash_core = Some(clash_core); - drop(verge); + { + VergeN::global().config.lock().clash_core = Some(clash_core); + } let _ = VergeN::global().save_file();