From 65149c33da565ae22fe8de17c2f071f80ef8c97f Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sat, 12 Oct 2024 21:32:04 +0800 Subject: [PATCH] fix link error and add switch for specify work directory --- src/main/core/factory.ts | 19 +++++++++---- src/main/core/manager.ts | 26 +++++++++++------ src/main/utils/dirs.ts | 6 +++- .../src/components/settings/mihomo-config.tsx | 28 ++++++++++++++++++- src/shared/types.d.ts | 1 + 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/main/core/factory.ts b/src/main/core/factory.ts index 587e5d3..c3ee8cb 100644 --- a/src/main/core/factory.ts +++ b/src/main/core/factory.ts @@ -5,13 +5,14 @@ import { getProfileItem, getOverride, getOverrideItem, - getOverrideConfig + getOverrideConfig, + getAppConfig } from '../config' import { mihomoProfileWorkDir, mihomoWorkConfigPath, - overridePath, - resourcesFilesDir + mihomoWorkDir, + overridePath } from '../utils/dirs' import yaml from 'yaml' import { link, mkdir, writeFile } from 'fs/promises' @@ -25,6 +26,7 @@ let runtimeConfig: IMihomoConfig export async function generateProfile(): Promise { const { current } = await getProfileConfig() + const { diffWorkDir = false } = await getAppConfig() const currentProfile = await overrideProfile(current, await getProfile(current)) const controledMihomoConfig = await getControledMihomoConfig() const profile = deepMerge(currentProfile, controledMihomoConfig) @@ -32,8 +34,13 @@ export async function generateProfile(): Promise { profile['log-level'] = 'info' runtimeConfig = profile runtimeConfigStr = yaml.stringify(profile) - await prepareProfileWorkDir(current) - await writeFile(mihomoWorkConfigPath(current), runtimeConfigStr) + if (diffWorkDir) { + await prepareProfileWorkDir(current) + } + await writeFile( + diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'), + runtimeConfigStr + ) } async function prepareProfileWorkDir(current: string | undefined): Promise { @@ -43,7 +50,7 @@ async function prepareProfileWorkDir(current: string | undefined): Promise const ln = async (file: string): Promise => { const targetPath = path.join(mihomoProfileWorkDir(current), file) - const sourcePath = path.join(resourcesFilesDir(), file) + const sourcePath = path.join(mihomoWorkDir(), file) if (!existsSync(targetPath) && existsSync(sourcePath)) { await link(sourcePath, targetPath) } diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 4a3d5a1..78b9437 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -6,7 +6,8 @@ import { mihomoCorePath, mihomoProfileWorkDir, mihomoTestDir, - mihomoWorkConfigPath + mihomoWorkConfigPath, + mihomoWorkDir } from '../utils/dirs' import { generateProfile } from './factory' import { @@ -56,7 +57,12 @@ let child: ChildProcess let retry = 10 export async function startCore(detached = false): Promise[]> { - const { core = 'mihomo', autoSetDNS = true, encryptedPassword } = await getAppConfig() + const { + core = 'mihomo', + autoSetDNS = true, + encryptedPassword, + diffWorkDir = false + } = await getAppConfig() const { 'log-level': logLevel } = await getControledMihomoConfig() if (existsSync(path.join(dataDir(), 'core.pid'))) { const pid = parseInt(await readFile(path.join(dataDir(), 'core.pid'), 'utf-8')) @@ -93,10 +99,14 @@ export async function startCore(detached = false): Promise[]> { } } - child = spawn(corePath, ['-d', mihomoProfileWorkDir(current), ctlParam, mihomoIpcPath], { - detached: detached, - stdio: detached ? 'ignore' : undefined - }) + child = spawn( + corePath, + ['-d', diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), ctlParam, mihomoIpcPath], + { + detached: detached, + stdio: detached ? 'ignore' : undefined + } + ) if (detached) { child.unref() return new Promise((resolve) => { @@ -205,7 +215,7 @@ export async function quitWithoutCore(): Promise { } async function checkProfile(): Promise { - const { core = 'mihomo' } = await getAppConfig() + const { core = 'mihomo', diffWorkDir = false } = await getAppConfig() const { current } = await getProfileConfig() const corePath = mihomoCorePath(core) const execFilePromise = promisify(execFile) @@ -213,7 +223,7 @@ async function checkProfile(): Promise { await execFilePromise(corePath, [ '-t', '-f', - mihomoWorkConfigPath(current), + diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'), '-d', mihomoTestDir() ]) diff --git a/src/main/utils/dirs.ts b/src/main/utils/dirs.ts index ea066ae..0907a60 100644 --- a/src/main/utils/dirs.ts +++ b/src/main/utils/dirs.ts @@ -111,7 +111,11 @@ export function mihomoTestDir(): string { } export function mihomoWorkConfigPath(id: string | undefined): string { - return path.join(mihomoProfileWorkDir(id), 'config.yaml') + if (id === 'work') { + return path.join(mihomoWorkDir(), 'config.yaml') + } else { + return path.join(mihomoProfileWorkDir(id), 'config.yaml') + } } export function logDir(): string { diff --git a/src/renderer/src/components/settings/mihomo-config.tsx b/src/renderer/src/components/settings/mihomo-config.tsx index f92aaff..d5eb115 100644 --- a/src/renderer/src/components/settings/mihomo-config.tsx +++ b/src/renderer/src/components/settings/mihomo-config.tsx @@ -1,16 +1,18 @@ import React, { useState } from 'react' import SettingCard from '../base/base-setting-card' import SettingItem from '../base/base-setting-item' -import { Button, Input, Select, SelectItem, Switch } from '@nextui-org/react' +import { Button, Input, Select, SelectItem, Switch, Tooltip } from '@nextui-org/react' import { useAppConfig } from '@renderer/hooks/use-app-config' import debounce from '@renderer/utils/debounce' import { getGistUrl, patchControledMihomoConfig, restartCore } from '@renderer/utils/ipc' import { MdDeleteForever } from 'react-icons/md' import { BiCopy } from 'react-icons/bi' +import { IoIosHelpCircle } from 'react-icons/io' const MihomoConfig: React.FC = () => { const { appConfig, patchAppConfig } = useAppConfig() const { + diffWorkDir = false, controlDns = true, controlSniff = true, delayTestConcurrency, @@ -132,6 +134,30 @@ const MihomoConfig: React.FC = () => { 四列 + + + + } + divider + > + { + try { + await patchAppConfig({ diffWorkDir: v }) + await restartCore() + } catch (e) { + alert(e) + } + }} + /> +