fix link error and add switch for specify work directory

This commit is contained in:
pompurin404 2024-10-12 21:32:04 +08:00
parent 3d7b3c703a
commit 65149c33da
No known key found for this signature in database
5 changed files with 64 additions and 16 deletions

View File

@ -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<void> {
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<void> {
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<void> {
@ -43,7 +50,7 @@ async function prepareProfileWorkDir(current: string | undefined): Promise<void>
const ln = async (file: string): Promise<void> => {
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)
}

View File

@ -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<Promise<void>[]> {
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<Promise<void>[]> {
}
}
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<void> {
}
async function checkProfile(): Promise<void> {
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<void> {
await execFilePromise(corePath, [
'-t',
'-f',
mihomoWorkConfigPath(current),
diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'),
'-d',
mihomoTestDir()
])

View File

@ -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 {

View File

@ -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 = () => {
<SelectItem key="4"></SelectItem>
</Select>
</SettingItem>
<SettingItem
title="为不同订阅分别指定工作目录"
actions={
<Tooltip content="开启后可以避免不同订阅中存在相同代理组名时无法分别保存选择的节点">
<Button isIconOnly size="sm" variant="light">
<IoIosHelpCircle className="text-lg" />
</Button>
</Tooltip>
}
divider
>
<Switch
size="sm"
isSelected={diffWorkDir}
onValueChange={async (v) => {
try {
await patchAppConfig({ diffWorkDir: v })
await restartCore()
} catch (e) {
alert(e)
}
}}
/>
</SettingItem>
<SettingItem title="接管 DNS 设置" divider>
<Switch
size="sm"

View File

@ -238,6 +238,7 @@ interface IAppConfig {
autoQuitWithoutCoreDelay?: number
useCustomSubStore?: boolean
customSubStoreUrl?: string
diffWorkDir?: boolean
autoSetDNS?: boolean
originDNS?: string
useWindowFrame: boolean