mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 05:00:30 +08:00
change profile
This commit is contained in:
parent
e3fc01dcf0
commit
dd7134c389
@ -22,21 +22,29 @@ export function getProfileItem(id: string | undefined): IProfileItem {
|
|||||||
return items?.find((item) => item.id === id) || { id: 'default', type: 'local', name: '空白订阅' }
|
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> {
|
export async function addProfileItem(item: Partial<IProfileItem>): Promise<void> {
|
||||||
const newItem = await createProfile(item)
|
const newItem = await createProfile(item)
|
||||||
profileConfig.items = getProfileConfig().items.filter((item) => item.id !== newItem.id)
|
|
||||||
profileConfig.items.push(newItem)
|
profileConfig.items.push(newItem)
|
||||||
let changeProfile = false
|
|
||||||
if (!getProfileConfig().current) {
|
if (!getProfileConfig().current) {
|
||||||
profileConfig.current = newItem.id
|
changeCurrentProfile(newItem.id)
|
||||||
changeProfile = true
|
|
||||||
}
|
}
|
||||||
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
|
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
|
||||||
window?.webContents.send('profileConfigUpdated')
|
window?.webContents.send('profileConfigUpdated')
|
||||||
if (changeProfile) {
|
|
||||||
getCurrentProfile(true)
|
|
||||||
restartCore()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeProfileItem(id: string): void {
|
export function removeProfileItem(id: string): void {
|
||||||
|
|||||||
@ -1,13 +1,21 @@
|
|||||||
import { ChildProcess, execSync, spawn } from 'child_process'
|
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 { generateProfile } from '../resolve/factory'
|
||||||
import { getAppConfig } from '../config'
|
import { getAppConfig } from '../config'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
let child: ChildProcess
|
let child: ChildProcess
|
||||||
|
|
||||||
export async function startCore(): Promise<void> {
|
export function startCore(): void {
|
||||||
const corePath = mihomoCorePath(getAppConfig().core ?? 'mihomo')
|
const corePath = mihomoCorePath(getAppConfig().core ?? 'mihomo')
|
||||||
generateProfile()
|
generateProfile()
|
||||||
|
checkProfile()
|
||||||
stopCore()
|
stopCore()
|
||||||
if (process.platform !== 'win32') {
|
if (process.platform !== 'win32') {
|
||||||
execSync(`chmod +x ${corePath}`)
|
execSync(`chmod +x ${corePath}`)
|
||||||
@ -41,3 +49,14 @@ export function stopCore(): void {
|
|||||||
export function restartCore(): void {
|
export function restartCore(): void {
|
||||||
startCore()
|
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()}`)
|
||||||
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import {
|
|||||||
controledMihomoConfigPath,
|
controledMihomoConfigPath,
|
||||||
dataDir,
|
dataDir,
|
||||||
logDir,
|
logDir,
|
||||||
|
mihomoTestDir,
|
||||||
mihomoWorkDir,
|
mihomoWorkDir,
|
||||||
profileConfigPath,
|
profileConfigPath,
|
||||||
profilePath,
|
profilePath,
|
||||||
@ -35,6 +36,9 @@ function initDirs(): void {
|
|||||||
if (!fs.existsSync(logDir())) {
|
if (!fs.existsSync(logDir())) {
|
||||||
fs.mkdirSync(logDir())
|
fs.mkdirSync(logDir())
|
||||||
}
|
}
|
||||||
|
if (!fs.existsSync(mihomoTestDir())) {
|
||||||
|
fs.mkdirSync(mihomoTestDir())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initConfig(): void {
|
function initConfig(): void {
|
||||||
@ -56,10 +60,14 @@ function initFiles(): void {
|
|||||||
const fileList = ['Country.mmdb', 'geoip.dat', 'geosite.dat']
|
const fileList = ['Country.mmdb', 'geoip.dat', 'geosite.dat']
|
||||||
for (const file of fileList) {
|
for (const file of fileList) {
|
||||||
const targetPath = path.join(mihomoWorkDir(), file)
|
const targetPath = path.join(mihomoWorkDir(), file)
|
||||||
|
const testTargrtPath = path.join(mihomoTestDir(), file)
|
||||||
const sourcePath = path.join(resourcesFilesDir(), file)
|
const sourcePath = path.join(resourcesFilesDir(), file)
|
||||||
if (!fs.existsSync(targetPath) && fs.existsSync(sourcePath)) {
|
if (!fs.existsSync(targetPath) && fs.existsSync(sourcePath)) {
|
||||||
fs.copyFileSync(sourcePath, targetPath)
|
fs.copyFileSync(sourcePath, targetPath)
|
||||||
}
|
}
|
||||||
|
if (!fs.existsSync(testTargrtPath) && fs.existsSync(sourcePath)) {
|
||||||
|
fs.copyFileSync(sourcePath, testTargrtPath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import {
|
|||||||
} from '../config'
|
} from '../config'
|
||||||
import { restartCore } from '../core/manager'
|
import { restartCore } from '../core/manager'
|
||||||
import { triggerSysProxy } from '../resolve/sysproxy'
|
import { triggerSysProxy } from '../resolve/sysproxy'
|
||||||
|
import { changeCurrentProfile } from '../config/profile'
|
||||||
|
|
||||||
export function registerIpcMainHandlers(): void {
|
export function registerIpcMainHandlers(): void {
|
||||||
ipcMain.handle('mihomoVersion', mihomoVersion)
|
ipcMain.handle('mihomoVersion', mihomoVersion)
|
||||||
@ -41,6 +42,7 @@ export function registerIpcMainHandlers(): void {
|
|||||||
ipcMain.handle('getProfileConfig', (_e, force) => getProfileConfig(force))
|
ipcMain.handle('getProfileConfig', (_e, force) => getProfileConfig(force))
|
||||||
ipcMain.handle('getCurrentProfileItem', getCurrentProfileItem)
|
ipcMain.handle('getCurrentProfileItem', getCurrentProfileItem)
|
||||||
ipcMain.handle('getProfileItem', (_e, id) => getProfileItem(id))
|
ipcMain.handle('getProfileItem', (_e, id) => getProfileItem(id))
|
||||||
|
ipcMain.handle('changeCurrentProfile', (_e, id) => changeCurrentProfile(id))
|
||||||
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
|
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
|
||||||
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
|
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
|
||||||
ipcMain.handle('restartCore', () => restartCore())
|
ipcMain.handle('restartCore', () => restartCore())
|
||||||
|
|||||||
@ -49,6 +49,10 @@ export function mihomoWorkDir(): string {
|
|||||||
return path.join(dataDir, 'work')
|
return path.join(dataDir, 'work')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mihomoTestDir(): string {
|
||||||
|
return path.join(dataDir, 'test')
|
||||||
|
}
|
||||||
|
|
||||||
export function mihomoWorkConfigPath(): string {
|
export function mihomoWorkConfigPath(): string {
|
||||||
return path.join(mihomoWorkDir(), 'config.yaml')
|
return path.join(mihomoWorkDir(), 'config.yaml')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { IoMdRefresh } from 'react-icons/io'
|
|||||||
interface Props {
|
interface Props {
|
||||||
info: IProfileItem
|
info: IProfileItem
|
||||||
isCurrent: boolean
|
isCurrent: boolean
|
||||||
onClick: () => void
|
onClick: () => Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
const ProfileItem: React.FC<Props> = (props) => {
|
const ProfileItem: React.FC<Props> = (props) => {
|
||||||
@ -14,6 +14,7 @@ const ProfileItem: React.FC<Props> = (props) => {
|
|||||||
const extra = info?.extra
|
const extra = info?.extra
|
||||||
const usage = (extra?.upload ?? 0) + (extra?.download ?? 0)
|
const usage = (extra?.upload ?? 0) + (extra?.download ?? 0)
|
||||||
const total = extra?.total ?? 0
|
const total = extra?.total ?? 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card fullWidth isPressable onPress={onClick} className={isCurrent ? 'bg-primary' : ''}>
|
<Card fullWidth isPressable onPress={onClick} className={isCurrent ? 'bg-primary' : ''}>
|
||||||
<CardBody>
|
<CardBody>
|
||||||
|
|||||||
@ -2,7 +2,8 @@ import useSWR from 'swr'
|
|||||||
import {
|
import {
|
||||||
getProfileConfig,
|
getProfileConfig,
|
||||||
addProfileItem as add,
|
addProfileItem as add,
|
||||||
removeProfileItem as remove
|
removeProfileItem as remove,
|
||||||
|
changeCurrentProfile as change
|
||||||
} from '@renderer/utils/ipc'
|
} from '@renderer/utils/ipc'
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
|
|
||||||
@ -10,7 +11,8 @@ interface RetuenType {
|
|||||||
profileConfig: IProfileConfig | undefined
|
profileConfig: IProfileConfig | undefined
|
||||||
mutateProfileConfig: () => void
|
mutateProfileConfig: () => void
|
||||||
addProfileItem: (item: Partial<IProfileItem>) => Promise<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 => {
|
export const useProfileConfig = (): RetuenType => {
|
||||||
@ -27,6 +29,12 @@ export const useProfileConfig = (): RetuenType => {
|
|||||||
await remove(id)
|
await remove(id)
|
||||||
mutateProfileConfig()
|
mutateProfileConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const changeCurrentProfile = async (id: string): Promise<void> => {
|
||||||
|
await change(id)
|
||||||
|
mutateProfileConfig()
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.electron.ipcRenderer.on('profileConfigUpdated', () => {
|
window.electron.ipcRenderer.on('profileConfigUpdated', () => {
|
||||||
mutateProfileConfig()
|
mutateProfileConfig()
|
||||||
@ -40,6 +48,7 @@ export const useProfileConfig = (): RetuenType => {
|
|||||||
profileConfig,
|
profileConfig,
|
||||||
mutateProfileConfig,
|
mutateProfileConfig,
|
||||||
addProfileItem,
|
addProfileItem,
|
||||||
removeProfileItem
|
removeProfileItem,
|
||||||
|
changeCurrentProfile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { useState } from 'react'
|
|||||||
import { MdContentPaste } from 'react-icons/md'
|
import { MdContentPaste } from 'react-icons/md'
|
||||||
|
|
||||||
const Profiles: React.FC = () => {
|
const Profiles: React.FC = () => {
|
||||||
const { profileConfig, addProfileItem } = useProfileConfig()
|
const { profileConfig, addProfileItem, changeCurrentProfile } = useProfileConfig()
|
||||||
const { current, items } = profileConfig || {}
|
const { current, items } = profileConfig || {}
|
||||||
const [importing, setImporting] = useState(false)
|
const [importing, setImporting] = useState(false)
|
||||||
const [url, setUrl] = useState('')
|
const [url, setUrl] = useState('')
|
||||||
@ -56,7 +56,9 @@ const Profiles: React.FC = () => {
|
|||||||
key={item.id}
|
key={item.id}
|
||||||
isCurrent={item.id === current}
|
isCurrent={item.id === current}
|
||||||
info={item}
|
info={item}
|
||||||
onClick={() => {}}
|
onClick={async () => {
|
||||||
|
await changeCurrentProfile(item.id)
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -14,15 +14,15 @@ export async function mihomoRules(): Promise<IMihomoRulesInfo> {
|
|||||||
return await window.electron.ipcRenderer.invoke('mihomoRules')
|
return await window.electron.ipcRenderer.invoke('mihomoRules')
|
||||||
}
|
}
|
||||||
export async function startMihomoLogs(): Promise<void> {
|
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> {
|
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> {
|
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> {
|
export async function checkAutoRun(): Promise<boolean> {
|
||||||
@ -30,11 +30,11 @@ export async function checkAutoRun(): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function enableAutoRun(): Promise<void> {
|
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> {
|
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> {
|
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> {
|
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>> {
|
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> {
|
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> {
|
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)
|
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> {
|
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> {
|
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> {
|
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> {
|
export async function triggerSysProxy(enable: boolean): Promise<void> {
|
||||||
await window.electron.ipcRenderer.invoke('triggerSysProxy', enable)
|
return await window.electron.ipcRenderer.invoke('triggerSysProxy', enable)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user