Skip to content

Commit

Permalink
change profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Pompurin404 committed Aug 2, 2024
1 parent 4bdcb10 commit 9f1dadb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 27 deletions.
24 changes: 16 additions & 8 deletions src/main/config/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 21 additions & 2 deletions src/main/core/manager.ts
Original file line number Diff line number Diff line change
@@ -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}`)
Expand Down Expand Up @@ -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()}`)
}
8 changes: 8 additions & 0 deletions src/main/resolve/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
controledMihomoConfigPath,
dataDir,
logDir,
mihomoTestDir,
mihomoWorkDir,
profileConfigPath,
profilePath,
Expand Down Expand Up @@ -35,6 +36,9 @@ function initDirs(): void {
if (!fs.existsSync(logDir())) {
fs.mkdirSync(logDir())
}
if (!fs.existsSync(mihomoTestDir())) {
fs.mkdirSync(mihomoTestDir())
}
}

function initConfig(): void {
Expand All @@ -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)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/utils/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions src/main/utils/dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/components/profiles/profile-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { IoMdRefresh } from 'react-icons/io'
interface Props {
info: IProfileItem
isCurrent: boolean
onClick: () => void
onClick: () => Promise<void>
}

const ProfileItem: React.FC<Props> = (props) => {
const { info, onClick, isCurrent } = 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>
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/src/hooks/use-profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ 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'

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 => {
Expand All @@ -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()
Expand All @@ -40,6 +48,7 @@ export const useProfileConfig = (): RetuenType => {
profileConfig,
mutateProfileConfig,
addProfileItem,
removeProfileItem
removeProfileItem,
changeCurrentProfile
}
}
6 changes: 4 additions & 2 deletions src/renderer/src/pages/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down Expand Up @@ -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>
Expand Down
26 changes: 15 additions & 11 deletions src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@ 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> {
return await window.electron.ipcRenderer.invoke('checkAutoRun')
}

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> {
return await window.electron.ipcRenderer.invoke('getAppConfig', force)
}

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>> {
return await window.electron.ipcRenderer.invoke('getControledMihomoConfig', force)
}

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> {
Expand All @@ -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)
}

0 comments on commit 9f1dadb

Please sign in to comment.