Skip to content

Commit

Permalink
fix link error and add switch for specify work directory
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Oct 12, 2024
1 parent 3d7b3c7 commit 65149c3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
19 changes: 13 additions & 6 deletions src/main/core/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -25,15 +26,21 @@ 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)
// 确保可以拿到基础日志信息
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> {
Expand All @@ -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)
}
Expand Down
26 changes: 18 additions & 8 deletions src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
mihomoCorePath,
mihomoProfileWorkDir,
mihomoTestDir,
mihomoWorkConfigPath
mihomoWorkConfigPath,
mihomoWorkDir
} from '../utils/dirs'
import { generateProfile } from './factory'
import {
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -205,15 +215,15 @@ 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)
try {
await execFilePromise(corePath, [
'-t',
'-f',
mihomoWorkConfigPath(current),
diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work'),
'-d',
mihomoTestDir()
])
Expand Down
6 changes: 5 additions & 1 deletion src/main/utils/dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 27 additions & 1 deletion src/renderer/src/components/settings/mihomo-config.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ interface IAppConfig {
autoQuitWithoutCoreDelay?: number
useCustomSubStore?: boolean
customSubStoreUrl?: string
diffWorkDir?: boolean
autoSetDNS?: boolean
originDNS?: string
useWindowFrame: boolean
Expand Down

0 comments on commit 65149c3

Please sign in to comment.