change profile

This commit is contained in:
pompurin404 2024-08-02 23:12:38 +08:00
parent e3fc01dcf0
commit dd7134c389
No known key found for this signature in database
9 changed files with 84 additions and 27 deletions

View File

@ -22,21 +22,29 @@ export function getProfileItem(id: string | undefined): IProfileItem {
return items?.find((item) => item.id === id) || { id: 'default', type: 'local', name: '空白订阅' }
}
export async function changeCurrentProfile(id: string): Promise<void> {
const oldId = getProfileConfig().current
profileConfig.current = id
getCurrentProfile(true)
try {
restartCore()
} catch (e) {
profileConfig.current = oldId
getCurrentProfile(true)
} finally {
window?.webContents.send('profileConfigUpdated')
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
}
}
export async function addProfileItem(item: Partial<IProfileItem>): Promise<void> {
const newItem = await createProfile(item)
profileConfig.items = getProfileConfig().items.filter((item) => item.id !== newItem.id)
profileConfig.items.push(newItem)
let changeProfile = false
if (!getProfileConfig().current) {
profileConfig.current = newItem.id
changeProfile = true
changeCurrentProfile(newItem.id)
}
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
window?.webContents.send('profileConfigUpdated')
if (changeProfile) {
getCurrentProfile(true)
restartCore()
}
}
export function removeProfileItem(id: string): void {

View File

@ -1,13 +1,21 @@
import { ChildProcess, execSync, spawn } from 'child_process'
import { logPath, mihomoCorePath, mihomoWorkDir } from '../utils/dirs'
import {
logPath,
mihomoCorePath,
mihomoTestDir,
mihomoWorkConfigPath,
mihomoWorkDir
} from '../utils/dirs'
import { generateProfile } from '../resolve/factory'
import { getAppConfig } from '../config'
import fs from 'fs'
let child: ChildProcess
export async function startCore(): Promise<void> {
export function startCore(): void {
const corePath = mihomoCorePath(getAppConfig().core ?? 'mihomo')
generateProfile()
checkProfile()
stopCore()
if (process.platform !== 'win32') {
execSync(`chmod +x ${corePath}`)
@ -41,3 +49,14 @@ export function stopCore(): void {
export function restartCore(): void {
startCore()
}
// mihomo -t -d path return status code
export function checkProfile(): void {
const corePath = mihomoCorePath(getAppConfig().core ?? 'mihomo')
generateProfile()
if (process.platform !== 'win32') {
execSync(`chmod +x ${corePath}`)
}
execSync(`${corePath} -t -f ${mihomoWorkConfigPath()} -d ${mihomoTestDir()}`)
}

View File

@ -3,6 +3,7 @@ import {
controledMihomoConfigPath,
dataDir,
logDir,
mihomoTestDir,
mihomoWorkDir,
profileConfigPath,
profilePath,
@ -35,6 +36,9 @@ function initDirs(): void {
if (!fs.existsSync(logDir())) {
fs.mkdirSync(logDir())
}
if (!fs.existsSync(mihomoTestDir())) {
fs.mkdirSync(mihomoTestDir())
}
}
function initConfig(): void {
@ -56,10 +60,14 @@ function initFiles(): void {
const fileList = ['Country.mmdb', 'geoip.dat', 'geosite.dat']
for (const file of fileList) {
const targetPath = path.join(mihomoWorkDir(), file)
const testTargrtPath = path.join(mihomoTestDir(), file)
const sourcePath = path.join(resourcesFilesDir(), file)
if (!fs.existsSync(targetPath) && fs.existsSync(sourcePath)) {
fs.copyFileSync(sourcePath, targetPath)
}
if (!fs.existsSync(testTargrtPath) && fs.existsSync(sourcePath)) {
fs.copyFileSync(sourcePath, testTargrtPath)
}
}
}

View File

@ -22,6 +22,7 @@ import {
} from '../config'
import { restartCore } from '../core/manager'
import { triggerSysProxy } from '../resolve/sysproxy'
import { changeCurrentProfile } from '../config/profile'
export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoVersion', mihomoVersion)
@ -41,6 +42,7 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('getProfileConfig', (_e, force) => getProfileConfig(force))
ipcMain.handle('getCurrentProfileItem', getCurrentProfileItem)
ipcMain.handle('getProfileItem', (_e, id) => getProfileItem(id))
ipcMain.handle('changeCurrentProfile', (_e, id) => changeCurrentProfile(id))
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
ipcMain.handle('restartCore', () => restartCore())

View File

@ -49,6 +49,10 @@ export function mihomoWorkDir(): string {
return path.join(dataDir, 'work')
}
export function mihomoTestDir(): string {
return path.join(dataDir, 'test')
}
export function mihomoWorkConfigPath(): string {
return path.join(mihomoWorkDir(), 'config.yaml')
}

View File

@ -6,7 +6,7 @@ import { IoMdRefresh } from 'react-icons/io'
interface Props {
info: IProfileItem
isCurrent: boolean
onClick: () => void
onClick: () => Promise<void>
}
const ProfileItem: React.FC<Props> = (props) => {
@ -14,6 +14,7 @@ const ProfileItem: React.FC<Props> = (props) => {
const extra = info?.extra
const usage = (extra?.upload ?? 0) + (extra?.download ?? 0)
const total = extra?.total ?? 0
return (
<Card fullWidth isPressable onPress={onClick} className={isCurrent ? 'bg-primary' : ''}>
<CardBody>

View File

@ -2,7 +2,8 @@ import useSWR from 'swr'
import {
getProfileConfig,
addProfileItem as add,
removeProfileItem as remove
removeProfileItem as remove,
changeCurrentProfile as change
} from '@renderer/utils/ipc'
import { useEffect } from 'react'
@ -10,7 +11,8 @@ interface RetuenType {
profileConfig: IProfileConfig | undefined
mutateProfileConfig: () => void
addProfileItem: (item: Partial<IProfileItem>) => Promise<void>
removeProfileItem: (id: string) => void
removeProfileItem: (id: string) => Promise<void>
changeCurrentProfile: (id: string) => Promise<void>
}
export const useProfileConfig = (): RetuenType => {
@ -27,6 +29,12 @@ export const useProfileConfig = (): RetuenType => {
await remove(id)
mutateProfileConfig()
}
const changeCurrentProfile = async (id: string): Promise<void> => {
await change(id)
mutateProfileConfig()
}
useEffect(() => {
window.electron.ipcRenderer.on('profileConfigUpdated', () => {
mutateProfileConfig()
@ -40,6 +48,7 @@ export const useProfileConfig = (): RetuenType => {
profileConfig,
mutateProfileConfig,
addProfileItem,
removeProfileItem
removeProfileItem,
changeCurrentProfile
}
}

View File

@ -6,7 +6,7 @@ import { useState } from 'react'
import { MdContentPaste } from 'react-icons/md'
const Profiles: React.FC = () => {
const { profileConfig, addProfileItem } = useProfileConfig()
const { profileConfig, addProfileItem, changeCurrentProfile } = useProfileConfig()
const { current, items } = profileConfig || {}
const [importing, setImporting] = useState(false)
const [url, setUrl] = useState('')
@ -56,7 +56,9 @@ const Profiles: React.FC = () => {
key={item.id}
isCurrent={item.id === current}
info={item}
onClick={() => {}}
onClick={async () => {
await changeCurrentProfile(item.id)
}}
/>
))}
</div>

View File

@ -14,15 +14,15 @@ export async function mihomoRules(): Promise<IMihomoRulesInfo> {
return await window.electron.ipcRenderer.invoke('mihomoRules')
}
export async function startMihomoLogs(): Promise<void> {
await window.electron.ipcRenderer.invoke('startMihomoLogs')
return await window.electron.ipcRenderer.invoke('startMihomoLogs')
}
export async function stopMihomoLogs(): Promise<void> {
await window.electron.ipcRenderer.invoke('stopMihomoLogs')
return await window.electron.ipcRenderer.invoke('stopMihomoLogs')
}
export async function patchMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
await window.electron.ipcRenderer.invoke('patchMihomoConfig', patch)
return await window.electron.ipcRenderer.invoke('patchMihomoConfig', patch)
}
export async function checkAutoRun(): Promise<boolean> {
@ -30,11 +30,11 @@ export async function checkAutoRun(): Promise<boolean> {
}
export async function enableAutoRun(): Promise<void> {
await window.electron.ipcRenderer.invoke('enableAutoRun')
return await window.electron.ipcRenderer.invoke('enableAutoRun')
}
export async function disableAutoRun(): Promise<void> {
await window.electron.ipcRenderer.invoke('disableAutoRun')
return await window.electron.ipcRenderer.invoke('disableAutoRun')
}
export async function getAppConfig(force = false): Promise<IAppConfig> {
@ -42,7 +42,7 @@ export async function getAppConfig(force = false): Promise<IAppConfig> {
}
export async function setAppConfig(patch: Partial<IAppConfig>): Promise<void> {
await window.electron.ipcRenderer.invoke('setAppConfig', patch)
return await window.electron.ipcRenderer.invoke('setAppConfig', patch)
}
export async function getControledMihomoConfig(force = false): Promise<Partial<IMihomoConfig>> {
@ -50,7 +50,7 @@ export async function getControledMihomoConfig(force = false): Promise<Partial<I
}
export async function setControledMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
await window.electron.ipcRenderer.invoke('setControledMihomoConfig', patch)
return await window.electron.ipcRenderer.invoke('setControledMihomoConfig', patch)
}
export async function getProfileConfig(force = false): Promise<IProfileConfig> {
@ -65,18 +65,22 @@ export async function getProfileItem(id: string | undefined): Promise<IProfileIt
return await window.electron.ipcRenderer.invoke('getProfileItem', id)
}
export async function changeCurrentProfile(id: string): Promise<void> {
return await window.electron.ipcRenderer.invoke('changeCurrentProfile', id)
}
export async function addProfileItem(item: Partial<IProfileItem>): Promise<void> {
await window.electron.ipcRenderer.invoke('addProfileItem', item)
return await window.electron.ipcRenderer.invoke('addProfileItem', item)
}
export async function removeProfileItem(id: string): Promise<void> {
await window.electron.ipcRenderer.invoke('removeProfileItem', id)
return await window.electron.ipcRenderer.invoke('removeProfileItem', id)
}
export async function restartCore(): Promise<void> {
await window.electron.ipcRenderer.invoke('restartCore')
return await window.electron.ipcRenderer.invoke('restartCore')
}
export async function triggerSysProxy(enable: boolean): Promise<void> {
await window.electron.ipcRenderer.invoke('triggerSysProxy', enable)
return await window.electron.ipcRenderer.invoke('triggerSysProxy', enable)
}