From e6e2b1f142e8acdfd442b5b0438410625471fd86 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Wed, 19 Jun 2024 10:04:28 +0800 Subject: [PATCH] refactor: change core binary name --- scripts/check.mjs | 12 ++-- scripts/portable-fixed-webview2.mjs | 4 +- scripts/portable.mjs | 4 +- src-tauri/src/config/clash.rs | 2 +- src-tauri/src/config/verge.rs | 2 +- src-tauri/src/core/core.rs | 71 ++++++++----------- src-tauri/src/core/service.rs | 19 ++++- src-tauri/src/enhance/chain.rs | 4 +- src-tauri/tauri.conf.json | 2 +- src-tauri/tauri.linux.conf.json | 12 ++-- src-tauri/template/installer.nsi | 32 +++++++++ src/components/layout/layout-traffic.tsx | 4 +- .../setting/mods/clash-core-viewer.tsx | 6 +- src/components/setting/mods/tun-viewer.tsx | 12 ++-- src/hooks/use-clash.ts | 2 +- src/pages/proxies.tsx | 7 +- 16 files changed, 113 insertions(+), 82 deletions(-) diff --git a/scripts/check.mjs b/scripts/check.mjs index 99b9755..335bd6c 100644 --- a/scripts/check.mjs +++ b/scripts/check.mjs @@ -172,8 +172,8 @@ function clashMetaAlpha() { const zipFile = `${name}-${META_ALPHA_VERSION}.${urlExt}`; return { - name: "clash-meta-alpha", - targetFile: `clash-meta-alpha-${SIDECAR_HOST}${isWin ? ".exe" : ""}`, + name: "verge-mihomo-alpha", + targetFile: `verge-mihomo-alpha-${SIDECAR_HOST}${isWin ? ".exe" : ""}`, exeFile, zipFile, downloadURL, @@ -189,8 +189,8 @@ function clashMeta() { const zipFile = `${name}-${META_VERSION}.${urlExt}`; return { - name: "clash-meta", - targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`, + name: "verge-mihomo", + targetFile: `verge-mihomo-${SIDECAR_HOST}${isWin ? ".exe" : ""}`, exeFile, zipFile, downloadURL, @@ -434,13 +434,13 @@ const resolveEnableLoopback = () => const tasks = [ // { name: "clash", func: resolveClash, retry: 5 }, { - name: "clash-meta-alpha", + name: "verge-mihomo-alpha", func: () => getLatestAlphaVersion().then(() => resolveSidecar(clashMetaAlpha())), retry: 5, }, { - name: "clash-meta", + name: "verge-mihomo", func: () => getLatestReleaseVersion().then(() => resolveSidecar(clashMeta())), retry: 5, diff --git a/scripts/portable-fixed-webview2.mjs b/scripts/portable-fixed-webview2.mjs index 76ecdc0..94285ce 100644 --- a/scripts/portable-fixed-webview2.mjs +++ b/scripts/portable-fixed-webview2.mjs @@ -40,8 +40,8 @@ async function resolvePortable() { const zip = new AdmZip(); zip.addLocalFile(path.join(releaseDir, "Clash Verge.exe")); - zip.addLocalFile(path.join(releaseDir, "clash-meta.exe")); - zip.addLocalFile(path.join(releaseDir, "clash-meta-alpha.exe")); + zip.addLocalFile(path.join(releaseDir, "verge-mihomo.exe")); + zip.addLocalFile(path.join(releaseDir, "verge-mihomo-alpha.exe")); zip.addLocalFolder(path.join(releaseDir, "resources"), "resources"); zip.addLocalFolder( path.join( diff --git a/scripts/portable.mjs b/scripts/portable.mjs index 53c5cb1..1a9d125 100644 --- a/scripts/portable.mjs +++ b/scripts/portable.mjs @@ -39,8 +39,8 @@ async function resolvePortable() { const zip = new AdmZip(); zip.addLocalFile(path.join(releaseDir, "Clash Verge.exe")); - zip.addLocalFile(path.join(releaseDir, "clash-meta.exe")); - zip.addLocalFile(path.join(releaseDir, "clash-meta-alpha.exe")); + zip.addLocalFile(path.join(releaseDir, "verge-mihomo.exe")); + zip.addLocalFile(path.join(releaseDir, "verge-mihomo-alpha.exe")); zip.addLocalFolder(path.join(releaseDir, "resources"), "resources"); zip.addLocalFolder(configDir, ".config"); diff --git a/src-tauri/src/config/clash.rs b/src-tauri/src/config/clash.rs index d9ff5ce..7f782a3 100644 --- a/src-tauri/src/config/clash.rs +++ b/src-tauri/src/config/clash.rs @@ -33,7 +33,7 @@ impl IClashTemp { let mut map = Mapping::new(); let mut tun = Mapping::new(); tun.insert("stack".into(), "gvisor".into()); - tun.insert("device".into(), "Meta".into()); + tun.insert("device".into(), "Mihomo".into()); tun.insert("auto-route".into(), true.into()); tun.insert("strict-route".into(), false.into()); tun.insert("auto-detect-interface".into(), true.into()); diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 940b360..be454c5 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -198,7 +198,7 @@ impl IVerge { pub fn template() -> Self { Self { - clash_core: Some("clash-meta".into()), + clash_core: Some("verge-mihomo".into()), language: Some("zh".into()), theme_mode: Some("system".into()), #[cfg(not(target_os = "windows"))] diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index afdf355..8f8ca51 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -1,7 +1,7 @@ -use super::service; -use super::{clash_api, logger::Logger}; +use crate::config::*; +use crate::core::{clash_api, handle, logger::Logger, service}; use crate::log_err; -use crate::{config::*, utils::dirs}; +use crate::utils::dirs; use anyhow::{bail, Result}; use once_cell::sync::OnceCell; use parking_lot::Mutex; @@ -72,21 +72,11 @@ impl CoreManager { #[allow(unused_mut)] let mut should_kill = match self.sidecar.lock().take() { - Some(child) => { - log::debug!(target: "app", "stop the core by sidecar"); - let _ = child.kill(); - true - } + Some(_) => true, None => false, }; - let mut system = System::new(); - system.refresh_all(); - let procs = system.processes_by_name("clash-meta"); - for proc in procs { - log::debug!(target: "app", "kill all clash process"); - proc.kill(); - } + self.stop_core()?; // 先停止 if *self.use_service_mode.lock() { log::debug!(target: "app", "stop the core by service"); @@ -128,16 +118,25 @@ impl CoreManager { let app_dir = dirs::path_to_str(&app_dir)?; let clash_core = { Config::verge().latest().clash_core.clone() }; - let clash_core = clash_core.unwrap_or("clash".into()); - let is_clash = clash_core == "clash"; + let mut clash_core = clash_core.unwrap_or("verge-mihomo".into()); + + // compatibility + if clash_core.contains("clash") { + clash_core = "verge-mihomo".to_string(); + Config::verge().draft().patch_config(IVerge { + clash_core: Some("verge-mihomo".to_string()), + ..IVerge::default() + }); + Config::verge().apply(); + match Config::verge().data().save_file() { + Ok(_) => handle::Handle::refresh_verge(), + Err(err) => log::error!(target: "app", "{err}"), + } + } let config_path = dirs::path_to_str(&config_path)?; - let args = match clash_core.as_str() { - "clash-meta" => vec!["-d", app_dir, "-f", config_path], - "clash-meta-alpha" => vec!["-d", app_dir, "-f", config_path], - _ => vec!["-d", app_dir, "-f", config_path], - }; + let args = vec!["-d", app_dir, "-f", config_path]; let cmd = Command::new_sidecar(clash_core)?; let (mut rx, cmd_child) = cmd.args(args).spawn()?; @@ -150,25 +149,19 @@ impl CoreManager { while let Some(event) = rx.recv().await { match event { CommandEvent::Stdout(line) => { - if is_clash { - let stdout = clash_api::parse_log(line.clone()); - log::info!(target: "app", "[clash]: {stdout}"); - } else { - log::info!(target: "app", "[clash]: {line}"); - }; + log::info!(target: "app", "[mihomo]: {line}"); Logger::global().set_log(line); } CommandEvent::Stderr(err) => { - // let stdout = clash_api::parse_log(err.clone()); - log::error!(target: "app", "[clash]: {err}"); + log::error!(target: "app", "[mihomo]: {err}"); Logger::global().set_log(err); } CommandEvent::Error(err) => { - log::error!(target: "app", "[clash]: {err}"); + log::error!(target: "app", "[mihomo]: {err}"); Logger::global().set_log(err); } CommandEvent::Terminated(_) => { - log::info!(target: "app", "clash core terminated"); + log::info!(target: "app", "mihomo core terminated"); let _ = CoreManager::global().recover_core(); break; } @@ -188,9 +181,7 @@ impl CoreManager { } // 清空原来的sidecar值 - if let Some(sidecar) = self.sidecar.lock().take() { - let _ = sidecar.kill(); - } + let _ = self.sidecar.lock().take(); tauri::async_runtime::spawn(async move { // 6秒之后再查看服务是否正常 (时间随便搞的) @@ -234,13 +225,11 @@ impl CoreManager { } let mut sidecar = self.sidecar.lock(); - if let Some(child) = sidecar.take() { - log::debug!(target: "app", "stop the core by sidecar"); - let _ = child.kill(); - } + let _ = sidecar.take(); + let mut system = System::new(); system.refresh_all(); - let procs = system.processes_by_name("clash-meta"); + let procs = system.processes_by_name("verge-mihomo"); for proc in procs { log::debug!(target: "app", "kill all clash process"); proc.kill(); @@ -251,7 +240,7 @@ impl CoreManager { /// 切换核心 pub async fn change_core(&self, clash_core: Option) -> Result<()> { let clash_core = clash_core.ok_or(anyhow::anyhow!("clash core is null"))?; - const CLASH_CORES: [&str; 2] = ["clash-meta", "clash-meta-alpha"]; + const CLASH_CORES: [&str; 2] = ["verge-mihomo", "verge-mihomo-alpha"]; if !CLASH_CORES.contains(&clash_core.as_str()) { bail!("invalid clash core name \"{clash_core}\""); diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 2257b69..6872b02 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -1,4 +1,5 @@ -use crate::config::Config; +use crate::config::{Config, IVerge}; +use crate::core::handle; use crate::utils::dirs; use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; @@ -237,7 +238,21 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> { } let clash_core = { Config::verge().latest().clash_core.clone() }; - let clash_core = clash_core.unwrap_or("clash".into()); + let mut clash_core = clash_core.unwrap_or("verge-mihomo".into()); + + // compatibility + if clash_core.contains("clash") { + clash_core = "verge-mihomo".to_string(); + Config::verge().draft().patch_config(IVerge { + clash_core: Some("verge-mihomo".to_string()), + ..IVerge::default() + }); + Config::verge().apply(); + match Config::verge().data().save_file() { + Ok(_) => handle::Handle::refresh_verge(), + Err(err) => log::error!(target: "app", "{err}"), + } + } let bin_ext = if cfg!(windows) { ".exe" } else { "" }; let clash_bin = format!("{clash_core}{bin_ext}"); diff --git a/src-tauri/src/enhance/chain.rs b/src-tauri/src/enhance/chain.rs index aa939b6..bc88d42 100644 --- a/src-tauri/src/enhance/chain.rs +++ b/src-tauri/src/enhance/chain.rs @@ -92,8 +92,8 @@ impl ChainSupport { (self, core.as_str()), (ChainSupport::All, _) | (ChainSupport::Clash, "clash") - | (ChainSupport::ClashMeta, "clash-meta") - | (ChainSupport::ClashMetaAlpha, "clash-meta-alpha") + | (ChainSupport::ClashMeta, "verge-mihomo") + | (ChainSupport::ClashMetaAlpha, "verge-mihomo-alpha") ), None => true, } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index b5a1f8c..7db1115 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -23,7 +23,7 @@ ], "resources": ["resources"], "publisher": "Clash Verge Rev", - "externalBin": ["sidecar/clash-meta", "sidecar/clash-meta-alpha"], + "externalBin": ["sidecar/verge-mihomo", "sidecar/verge-mihomo-alpha"], "copyright": "GNU General Public License v3.0", "category": "DeveloperTool", "shortDescription": "A Clash Meta GUI based on tauri.", diff --git a/src-tauri/tauri.linux.conf.json b/src-tauri/tauri.linux.conf.json index cbf131f..3a2dc9e 100644 --- a/src-tauri/tauri.linux.conf.json +++ b/src-tauri/tauri.linux.conf.json @@ -10,16 +10,16 @@ "deb": { "depends": ["openssl"], "desktopTemplate": "./template/clash-verge.desktop", - "provides": ["clash-verge", "clash-meta"], - "conflicts": ["clash-verge", "clash-meta"], - "replaces": ["clash-verge", "clash-meta"] + "provides": ["clash-verge"], + "conflicts": ["clash-verge"], + "replaces": ["clash-verge"] }, "rpm": { "depends": ["openssl"], "desktopTemplate": "./template/clash-verge.desktop", - "provides": ["clash-verge", "clash-meta"], - "conflicts": ["clash-verge", "clash-meta"], - "obsoletes": ["clash-verge", "clash-meta"] + "provides": ["clash-verge"], + "conflicts": ["clash-verge"], + "obsoletes": ["clash-verge"] } } } diff --git a/src-tauri/template/installer.nsi b/src-tauri/template/installer.nsi index 11280aa..6b90a09 100644 --- a/src-tauri/template/installer.nsi +++ b/src-tauri/template/installer.nsi @@ -441,6 +441,38 @@ FunctionEnd !endif ${EndIf} + ; Check if verge-mihomo-alpha.exe is running + !if "${INSTALLMODE}" == "currentUser" + nsis_tauri_utils::FindProcessCurrentUser "verge-mihomo-alpha.exe" + !else + nsis_tauri_utils::FindProcess "verge-mihomo-alpha.exe" + !endif + Pop $R0 + ${If} $R0 = 0 + DetailPrint "Kill verge-mihomo-alpha.exe..." + !if "${INSTALLMODE}" == "currentUser" + nsis_tauri_utils::KillProcessCurrentUser "verge-mihomo-alpha.exe" + !else + nsis_tauri_utils::KillProcess "verge-mihomo-alpha.exe" + !endif + ${EndIf} + + ; Check if verge-mihomo.exe is running + !if "${INSTALLMODE}" == "currentUser" + nsis_tauri_utils::FindProcessCurrentUser "verge-mihomo.exe" + !else + nsis_tauri_utils::FindProcess "verge-mihomo.exe" + !endif + Pop $R0 + ${If} $R0 = 0 + DetailPrint "Kill verge-mihomo.exe..." + !if "${INSTALLMODE}" == "currentUser" + nsis_tauri_utils::KillProcessCurrentUser "verge-mihomo.exe" + !else + nsis_tauri_utils::KillProcess "verge-mihomo.exe" + !endif + ${EndIf} + ; Check if clash-meta-alpha.exe is running !if "${INSTALLMODE}" == "currentUser" nsis_tauri_utils::FindProcessCurrentUser "clash-meta-alpha.exe" diff --git a/src/components/layout/layout-traffic.tsx b/src/components/layout/layout-traffic.tsx index 336c10e..747a039 100644 --- a/src/components/layout/layout-traffic.tsx +++ b/src/components/layout/layout-traffic.tsx @@ -70,8 +70,8 @@ export const LayoutTraffic = () => { ); /* --------- meta memory information --------- */ - const isMetaCore = verge?.clash_core?.includes("clash-meta"); - const displayMemory = isMetaCore && (verge?.enable_memory_usage ?? true); + + const displayMemory = verge?.enable_memory_usage ?? true; const { data: memory = { inuse: 0 } } = useSWRSubscription< MemoryUsage, diff --git a/src/components/setting/mods/clash-core-viewer.tsx b/src/components/setting/mods/clash-core-viewer.tsx index 88e89ab..3c7a9aa 100644 --- a/src/components/setting/mods/clash-core-viewer.tsx +++ b/src/components/setting/mods/clash-core-viewer.tsx @@ -12,8 +12,8 @@ import { closeAllConnections, upgradeCore } from "@/services/api"; import getSystem from "@/utils/get-system"; const VALID_CORE = [ - { name: "Clash Meta", core: "clash-meta" }, - { name: "Clash Meta Alpha", core: "clash-meta-alpha" }, + { name: "Mihomo", core: "verge-mihomo" }, + { name: "Mihomo Alpha", core: "verge-mihomo-alpha" }, ]; const OS = getSystem(); @@ -31,7 +31,7 @@ export const ClashCoreViewer = forwardRef((props, ref) => { close: () => setOpen(false), })); - const { clash_core = "clash-meta" } = verge ?? {}; + const { clash_core = "verge-mihomo" } = verge ?? {}; const onCoreChange = useLockFn(async (core: string) => { if (core === clash_core) return; diff --git a/src/components/setting/mods/tun-viewer.tsx b/src/components/setting/mods/tun-viewer.tsx index 14ca0b2..7e437b4 100644 --- a/src/components/setting/mods/tun-viewer.tsx +++ b/src/components/setting/mods/tun-viewer.tsx @@ -23,7 +23,7 @@ export const TunViewer = forwardRef((props, ref) => { const [open, setOpen] = useState(false); const [values, setValues] = useState({ stack: "gvisor", - device: "Meta", + device: "Mihomo", autoRoute: true, autoDetectInterface: true, dnsHijack: ["any:53"], @@ -36,7 +36,7 @@ export const TunViewer = forwardRef((props, ref) => { setOpen(true); setValues({ stack: clash?.tun.stack ?? "gvisor", - device: clash?.tun.device ?? "Meta", + device: clash?.tun.device ?? "Mihomo", autoRoute: clash?.tun["auto-route"] ?? true, autoDetectInterface: clash?.tun["auto-detect-interface"] ?? true, dnsHijack: clash?.tun["dns-hijack"] ?? ["any:53"], @@ -51,7 +51,7 @@ export const TunViewer = forwardRef((props, ref) => { try { let tun = { stack: values.stack, - device: values.device === "" ? "Meta" : values.device, + device: values.device === "" ? "Mihomo" : values.device, "auto-route": values.autoRoute, "auto-detect-interface": values.autoDetectInterface, "dns-hijack": values.dnsHijack[0] === "" ? [] : values.dnsHijack, @@ -90,7 +90,7 @@ export const TunViewer = forwardRef((props, ref) => { onClick={async () => { let tun = { stack: "gvisor", - device: "Meta", + device: "Mihomo", "auto-route": true, "auto-detect-interface": true, "dns-hijack": ["any:53"], @@ -99,7 +99,7 @@ export const TunViewer = forwardRef((props, ref) => { }; setValues({ stack: "gvisor", - device: "Meta", + device: "Mihomo", autoRoute: true, autoDetectInterface: true, dnsHijack: ["any:53"], @@ -151,7 +151,7 @@ export const TunViewer = forwardRef((props, ref) => { spellCheck="false" sx={{ width: 250 }} value={values.device} - placeholder="Meta" + placeholder="Mihomo" onChange={(e) => setValues((v) => ({ ...v, device: e.target.value })) } diff --git a/src/hooks/use-clash.ts b/src/hooks/use-clash.ts index 472a2f0..143b478 100644 --- a/src/hooks/use-clash.ts +++ b/src/hooks/use-clash.ts @@ -27,7 +27,7 @@ export const useClash = () => { const version = versionData?.premium ? `${versionData.version} Premium` : versionData?.meta - ? `${versionData.version} Meta` + ? `${versionData.version} Mihomo` : versionData?.version || "-"; return { diff --git a/src/pages/proxies.tsx b/src/pages/proxies.tsx index 6c99d0b..a489479 100644 --- a/src/pages/proxies.tsx +++ b/src/pages/proxies.tsx @@ -24,12 +24,7 @@ const ProxyPage = () => { const { verge } = useVerge(); - const modeList = useMemo(() => { - if (verge?.clash_core?.includes("clash-meta")) { - return ["rule", "global", "direct"]; - } - return ["rule", "global", "direct", "script"]; - }, [verge?.clash_core]); + const modeList = ["rule", "global", "direct"]; const curMode = clashConfig?.mode?.toLowerCase();