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, getProfileItem,
getOverride, getOverride,
getOverrideItem, getOverrideItem,
getOverrideConfig getOverrideConfig,
getAppConfig
} from '../config' } from '../config'
import { import {
mihomoProfileWorkDir, mihomoProfileWorkDir,
mihomoWorkConfigPath, mihomoWorkConfigPath,
overridePath, mihomoWorkDir,
resourcesFilesDir overridePath
} from '../utils/dirs' } from '../utils/dirs'
import yaml from 'yaml' import yaml from 'yaml'
import { link, mkdir, writeFile } from 'fs/promises' import { link, mkdir, writeFile } from 'fs/promises'
@ -25,6 +26,7 @@ let runtimeConfig: IMihomoConfig
export async function generateProfile(): Promise<void> { export async function generateProfile(): Promise<void> {
const { current } = await getProfileConfig() const { current } = await getProfileConfig()
const { diffWorkDir = false } = await getAppConfig()
const currentProfile = await overrideProfile(current, await getProfile(current)) const currentProfile = await overrideProfile(current, await getProfile(current))
const controledMihomoConfig = await getControledMihomoConfig() const controledMihomoConfig = await getControledMihomoConfig()
const profile = deepMerge(currentProfile, controledMihomoConfig) const profile = deepMerge(currentProfile, controledMihomoConfig)
@ -32,8 +34,13 @@ export async function generateProfile(): Promise<void> {
profile['log-level'] = 'info' profile['log-level'] = 'info'
runtimeConfig = profile runtimeConfig = profile
runtimeConfigStr = yaml.stringify(profile) runtimeConfigStr = yaml.stringify(profile)
await prepareProfileWorkDir(current) if (diffWorkDir) {
await writeFile(mihomoWorkConfigPath(current), runtimeConfigStr) await prepareProfileWorkDir(current)
}
await writeFile(
diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'),
runtimeConfigStr
)
} }
async function prepareProfileWorkDir(current: string | undefined): Promise<void> { 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 ln = async (file: string): Promise<void> => {
const targetPath = path.join(mihomoProfileWorkDir(current), file) const targetPath = path.join(mihomoProfileWorkDir(current), file)
const sourcePath = path.join(resourcesFilesDir(), file) const sourcePath = path.join(mihomoWorkDir(), file)
if (!existsSync(targetPath) && existsSync(sourcePath)) { if (!existsSync(targetPath) && existsSync(sourcePath)) {
await link(sourcePath, targetPath) await link(sourcePath, targetPath)
} }

View File

@ -6,7 +6,8 @@ import {
mihomoCorePath, mihomoCorePath,
mihomoProfileWorkDir, mihomoProfileWorkDir,
mihomoTestDir, mihomoTestDir,
mihomoWorkConfigPath mihomoWorkConfigPath,
mihomoWorkDir
} from '../utils/dirs' } from '../utils/dirs'
import { generateProfile } from './factory' import { generateProfile } from './factory'
import { import {
@ -56,7 +57,12 @@ let child: ChildProcess
let retry = 10 let retry = 10
export async function startCore(detached = false): Promise<Promise<void>[]> { 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() const { 'log-level': logLevel } = await getControledMihomoConfig()
if (existsSync(path.join(dataDir(), 'core.pid'))) { if (existsSync(path.join(dataDir(), 'core.pid'))) {
const pid = parseInt(await readFile(path.join(dataDir(), 'core.pid'), 'utf-8')) 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], { child = spawn(
detached: detached, corePath,
stdio: detached ? 'ignore' : undefined ['-d', diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), ctlParam, mihomoIpcPath],
}) {
detached: detached,
stdio: detached ? 'ignore' : undefined
}
)
if (detached) { if (detached) {
child.unref() child.unref()
return new Promise((resolve) => { return new Promise((resolve) => {
@ -205,7 +215,7 @@ export async function quitWithoutCore(): Promise<void> {
} }
async function checkProfile(): Promise<void> { async function checkProfile(): Promise<void> {
const { core = 'mihomo' } = await getAppConfig() const { core = 'mihomo', diffWorkDir = false } = await getAppConfig()
const { current } = await getProfileConfig() const { current } = await getProfileConfig()
const corePath = mihomoCorePath(core) const corePath = mihomoCorePath(core)
const execFilePromise = promisify(execFile) const execFilePromise = promisify(execFile)
@ -213,7 +223,7 @@ async function checkProfile(): Promise<void> {
await execFilePromise(corePath, [ await execFilePromise(corePath, [
'-t', '-t',
'-f', '-f',
mihomoWorkConfigPath(current), diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'),
'-d', '-d',
mihomoTestDir() mihomoTestDir()
]) ])

View File

@ -111,7 +111,11 @@ export function mihomoTestDir(): string {
} }
export function mihomoWorkConfigPath(id: string | undefined): 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 { export function logDir(): string {

View File

@ -1,16 +1,18 @@
import React, { useState } from 'react' import React, { useState } from 'react'
import SettingCard from '../base/base-setting-card' import SettingCard from '../base/base-setting-card'
import SettingItem from '../base/base-setting-item' 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 { useAppConfig } from '@renderer/hooks/use-app-config'
import debounce from '@renderer/utils/debounce' import debounce from '@renderer/utils/debounce'
import { getGistUrl, patchControledMihomoConfig, restartCore } from '@renderer/utils/ipc' import { getGistUrl, patchControledMihomoConfig, restartCore } from '@renderer/utils/ipc'
import { MdDeleteForever } from 'react-icons/md' import { MdDeleteForever } from 'react-icons/md'
import { BiCopy } from 'react-icons/bi' import { BiCopy } from 'react-icons/bi'
import { IoIosHelpCircle } from 'react-icons/io'
const MihomoConfig: React.FC = () => { const MihomoConfig: React.FC = () => {
const { appConfig, patchAppConfig } = useAppConfig() const { appConfig, patchAppConfig } = useAppConfig()
const { const {
diffWorkDir = false,
controlDns = true, controlDns = true,
controlSniff = true, controlSniff = true,
delayTestConcurrency, delayTestConcurrency,
@ -132,6 +134,30 @@ const MihomoConfig: React.FC = () => {
<SelectItem key="4"></SelectItem> <SelectItem key="4"></SelectItem>
</Select> </Select>
</SettingItem> </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> <SettingItem title="接管 DNS 设置" divider>
<Switch <Switch
size="sm" size="sm"

View File

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