mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-26 20:50:30 +08:00
refactor the main thread code
This commit is contained in:
parent
fcef4b18fb
commit
945ab611fe
17
src/main/config/app.ts
Normal file
17
src/main/config/app.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { appConfigPath } from '../utils/dirs'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
|
||||
export let appConfig: IAppConfig // config.yaml
|
||||
|
||||
export function getAppConfig(force = false): IAppConfig {
|
||||
if (force || !appConfig) {
|
||||
appConfig = yaml.parse(fs.readFileSync(appConfigPath(), 'utf-8'))
|
||||
}
|
||||
return appConfig
|
||||
}
|
||||
|
||||
export function setAppConfig(patch: Partial<IAppConfig>): void {
|
||||
appConfig = Object.assign(appConfig, patch)
|
||||
fs.writeFileSync(appConfigPath(), yaml.stringify(appConfig))
|
||||
}
|
||||
17
src/main/config/controledMihomo.ts
Normal file
17
src/main/config/controledMihomo.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { controledMihomoConfigPath } from '../utils/dirs'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
|
||||
export let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
|
||||
|
||||
export function getControledMihomoConfig(force = false): Partial<IMihomoConfig> {
|
||||
if (force || !controledMihomoConfig) {
|
||||
controledMihomoConfig = yaml.parse(fs.readFileSync(controledMihomoConfigPath(), 'utf-8'))
|
||||
}
|
||||
return controledMihomoConfig
|
||||
}
|
||||
|
||||
export function setControledMihomoConfig(patch: Partial<IMihomoConfig>): void {
|
||||
controledMihomoConfig = Object.assign(controledMihomoConfig, patch)
|
||||
fs.writeFileSync(controledMihomoConfigPath(), yaml.stringify(controledMihomoConfig))
|
||||
}
|
||||
17
src/main/config/index.ts
Normal file
17
src/main/config/index.ts
Normal file
@ -0,0 +1,17 @@
|
||||
export { appConfig, getAppConfig, setAppConfig } from './app'
|
||||
export {
|
||||
controledMihomoConfig,
|
||||
getControledMihomoConfig,
|
||||
setControledMihomoConfig
|
||||
} from './controledMihomo'
|
||||
export {
|
||||
profileConfig,
|
||||
currentProfile,
|
||||
getCurrentProfile,
|
||||
getCurrentProfileItem,
|
||||
getProfileItem,
|
||||
getProfileConfig,
|
||||
addProfileItem,
|
||||
removeProfileItem,
|
||||
createProfile
|
||||
} from './profile'
|
||||
@ -1,62 +1,12 @@
|
||||
import { controledMihomoConfig } from './controledMihomo'
|
||||
import { profileConfigPath, profilePath } from '../utils/dirs'
|
||||
import { app } from 'electron'
|
||||
import axios from 'axios'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import {
|
||||
defaultConfig,
|
||||
defaultControledMihomoConfig,
|
||||
defaultProfile,
|
||||
defaultProfileConfig
|
||||
} from './template'
|
||||
import { appConfigPath, controledMihomoConfigPath, profileConfigPath, profilePath } from './dirs'
|
||||
import axios from 'axios'
|
||||
import { app } from 'electron'
|
||||
|
||||
export let appConfig: IAppConfig // config.yaml
|
||||
export let profileConfig: IProfileConfig // profile.yaml
|
||||
export let currentProfile: Partial<IMihomoConfig> // profiles/xxx.yaml
|
||||
export let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
|
||||
|
||||
export function initConfig(): void {
|
||||
if (!fs.existsSync(appConfigPath())) {
|
||||
fs.writeFileSync(appConfigPath(), yaml.stringify(defaultConfig))
|
||||
}
|
||||
if (!fs.existsSync(profileConfigPath())) {
|
||||
fs.writeFileSync(profileConfigPath(), yaml.stringify(defaultProfileConfig))
|
||||
}
|
||||
if (!fs.existsSync(profilePath('default'))) {
|
||||
fs.writeFileSync(profilePath('default'), yaml.stringify(defaultProfile))
|
||||
}
|
||||
if (!fs.existsSync(controledMihomoConfigPath())) {
|
||||
fs.writeFileSync(controledMihomoConfigPath(), yaml.stringify(defaultControledMihomoConfig))
|
||||
}
|
||||
getAppConfig(true)
|
||||
getControledMihomoConfig(true)
|
||||
getProfileConfig(true)
|
||||
getCurrentProfile(true)
|
||||
}
|
||||
|
||||
export function getAppConfig(force = false): IAppConfig {
|
||||
if (force || !appConfig) {
|
||||
appConfig = yaml.parse(fs.readFileSync(appConfigPath(), 'utf-8'))
|
||||
}
|
||||
return appConfig
|
||||
}
|
||||
|
||||
export function setAppConfig(patch: Partial<IAppConfig>): void {
|
||||
appConfig = Object.assign(appConfig, patch)
|
||||
fs.writeFileSync(appConfigPath(), yaml.stringify(appConfig))
|
||||
}
|
||||
|
||||
export function getControledMihomoConfig(force = false): Partial<IMihomoConfig> {
|
||||
if (force || !controledMihomoConfig) {
|
||||
controledMihomoConfig = yaml.parse(fs.readFileSync(controledMihomoConfigPath(), 'utf-8'))
|
||||
}
|
||||
return controledMihomoConfig
|
||||
}
|
||||
|
||||
export function setControledMihomoConfig(patch: Partial<IMihomoConfig>): void {
|
||||
controledMihomoConfig = Object.assign(controledMihomoConfig, patch)
|
||||
fs.writeFileSync(controledMihomoConfigPath(), yaml.stringify(controledMihomoConfig))
|
||||
}
|
||||
|
||||
export function getProfileConfig(force = false): IProfileConfig {
|
||||
if (force || !profileConfig) {
|
||||
@ -92,16 +42,6 @@ export function removeProfileItem(id: string): void {
|
||||
export function getCurrentProfileItem(): IProfileItem {
|
||||
return getProfileItem(profileConfig.current)
|
||||
}
|
||||
export function getCurrentProfile(force = false): Partial<IMihomoConfig> {
|
||||
if (force || !currentProfile) {
|
||||
if (profileConfig.current) {
|
||||
currentProfile = yaml.parse(fs.readFileSync(profilePath(profileConfig.current), 'utf-8'))
|
||||
} else {
|
||||
currentProfile = yaml.parse(fs.readFileSync(profilePath('default'), 'utf-8'))
|
||||
}
|
||||
}
|
||||
return currentProfile
|
||||
}
|
||||
|
||||
export async function createProfile(item: Partial<IProfileItem>): Promise<IProfileItem> {
|
||||
const id = item.id || new Date().getTime().toString(16)
|
||||
@ -163,3 +103,14 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
|
||||
|
||||
return newItem
|
||||
}
|
||||
|
||||
export function getCurrentProfile(force = false): Partial<IMihomoConfig> {
|
||||
if (force || !currentProfile) {
|
||||
if (profileConfig.current) {
|
||||
currentProfile = yaml.parse(fs.readFileSync(profilePath(profileConfig.current), 'utf-8'))
|
||||
} else {
|
||||
currentProfile = yaml.parse(fs.readFileSync(profilePath('default'), 'utf-8'))
|
||||
}
|
||||
}
|
||||
return currentProfile
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
import { ChildProcess, execSync, spawn } from 'child_process'
|
||||
import { logPath, mihomoCorePath, mihomoWorkDir } from './dirs'
|
||||
import { generateProfile } from './factory'
|
||||
import { appConfig } from './config'
|
||||
import { logPath, mihomoCorePath, mihomoWorkDir } from '../utils/dirs'
|
||||
import { generateProfile } from '../resolve/factory'
|
||||
import { appConfig } from '../config'
|
||||
import fs from 'fs'
|
||||
let child: ChildProcess
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import axios, { AxiosInstance } from 'axios'
|
||||
import { controledMihomoConfig } from './config'
|
||||
import { controledMihomoConfig } from '../config'
|
||||
import WebSocket from 'ws'
|
||||
import { window } from '.'
|
||||
import { window } from '..'
|
||||
|
||||
let axiosIns: AxiosInstance = null!
|
||||
let mihomoTrafficWs: WebSocket = null!
|
||||
@ -1,11 +1,11 @@
|
||||
import { appConfig, controledMihomoConfig, setAppConfig, setControledMihomoConfig } from './config'
|
||||
import icoIcon from '../../resources/icon.ico?asset'
|
||||
import pngIcon from '../../resources/icon.png?asset'
|
||||
import { appConfig, controledMihomoConfig, setAppConfig, setControledMihomoConfig } from '../config'
|
||||
import icoIcon from '../../../resources/icon.ico?asset'
|
||||
import pngIcon from '../../../resources/icon.png?asset'
|
||||
import { patchMihomoConfig } from './mihomoApi'
|
||||
import { window } from '.'
|
||||
import { window } from '..'
|
||||
import { app, ipcMain, Menu, shell, Tray } from 'electron'
|
||||
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from './dirs'
|
||||
import { triggerSysProxy } from './sysproxy'
|
||||
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
|
||||
import { triggerSysProxy } from '../resolve/sysproxy'
|
||||
|
||||
let tray: Tray | null = null
|
||||
|
||||
@ -60,7 +60,7 @@ const buildContextMenu = (): Menu => {
|
||||
{
|
||||
type: 'checkbox',
|
||||
label: '系统代理',
|
||||
checked: appConfig.sysProxy.enable,
|
||||
checked: appConfig.sysProxy?.enable ?? false,
|
||||
click: (item): void => {
|
||||
const enable = item.checked
|
||||
setAppConfig({ sysProxy: { enable } })
|
||||
@ -1,13 +1,13 @@
|
||||
import { app, shell, BrowserWindow } from 'electron'
|
||||
import { join } from 'path'
|
||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
||||
import { registerIpcMainHandlers } from './utils/cmds'
|
||||
import { app, shell, BrowserWindow } from 'electron'
|
||||
import { stopCore, startCore } from './core/manager'
|
||||
import icon from '../../resources/icon.png?asset'
|
||||
import { registerIpcMainHandlers } from './cmds'
|
||||
import { initConfig, appConfig } from './config'
|
||||
import { stopCore, startCore } from './manager'
|
||||
import { initDirs } from './dirs'
|
||||
import { mihomoTraffic } from './mihomoApi'
|
||||
import { createTray } from './tray'
|
||||
import { mihomoTraffic } from './core/mihomoApi'
|
||||
import { createTray } from './core/tray'
|
||||
import { init } from './resolve/init'
|
||||
import { appConfig } from './config'
|
||||
import { join } from 'path'
|
||||
|
||||
export let window: BrowserWindow | null = null
|
||||
|
||||
@ -16,8 +16,7 @@ const gotTheLock = app.requestSingleInstanceLock()
|
||||
if (!gotTheLock) {
|
||||
app.quit()
|
||||
} else {
|
||||
initDirs()
|
||||
initConfig()
|
||||
init()
|
||||
startCore()
|
||||
|
||||
app.on('second-instance', () => {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { controledMihomoConfig, currentProfile } from './config'
|
||||
import { mihomoWorkConfigPath } from './dirs'
|
||||
import { controledMihomoConfig, currentProfile } from '../config'
|
||||
import { mihomoWorkConfigPath } from '../utils/dirs'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
|
||||
77
src/main/resolve/init.ts
Normal file
77
src/main/resolve/init.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import {
|
||||
appConfigPath,
|
||||
controledMihomoConfigPath,
|
||||
dataDir,
|
||||
logDir,
|
||||
mihomoWorkDir,
|
||||
profileConfigPath,
|
||||
profilePath,
|
||||
profilesDir,
|
||||
resourcesFilesDir
|
||||
} from '../utils/dirs'
|
||||
import {
|
||||
getAppConfig,
|
||||
getControledMihomoConfig,
|
||||
getCurrentProfile,
|
||||
getProfileConfig
|
||||
} from '../config'
|
||||
import {
|
||||
defaultConfig,
|
||||
defaultControledMihomoConfig,
|
||||
defaultProfile,
|
||||
defaultProfileConfig
|
||||
} from '../utils/template'
|
||||
import yaml from 'yaml'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
function initDirs(): void {
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir)
|
||||
}
|
||||
if (!fs.existsSync(profilesDir())) {
|
||||
fs.mkdirSync(profilesDir())
|
||||
}
|
||||
if (!fs.existsSync(mihomoWorkDir())) {
|
||||
fs.mkdirSync(mihomoWorkDir())
|
||||
}
|
||||
if (!fs.existsSync(logDir())) {
|
||||
fs.mkdirSync(logDir())
|
||||
}
|
||||
}
|
||||
|
||||
function initConfig(): void {
|
||||
if (!fs.existsSync(appConfigPath())) {
|
||||
fs.writeFileSync(appConfigPath(), yaml.stringify(defaultConfig))
|
||||
}
|
||||
if (!fs.existsSync(profileConfigPath())) {
|
||||
fs.writeFileSync(profileConfigPath(), yaml.stringify(defaultProfileConfig))
|
||||
}
|
||||
if (!fs.existsSync(profilePath('default'))) {
|
||||
fs.writeFileSync(profilePath('default'), yaml.stringify(defaultProfile))
|
||||
}
|
||||
if (!fs.existsSync(controledMihomoConfigPath())) {
|
||||
fs.writeFileSync(controledMihomoConfigPath(), yaml.stringify(defaultControledMihomoConfig))
|
||||
}
|
||||
getAppConfig(true)
|
||||
getControledMihomoConfig(true)
|
||||
getProfileConfig(true)
|
||||
getCurrentProfile(true)
|
||||
}
|
||||
|
||||
function initFiles(): void {
|
||||
const fileList = ['Country.mmdb', 'geoip.dat', 'geosite.dat']
|
||||
for (const file of fileList) {
|
||||
const targetPath = path.join(profilesDir(), file)
|
||||
const sourcePath = path.join(resourcesFilesDir(), file)
|
||||
if (!fs.existsSync(targetPath) && fs.existsSync(sourcePath)) {
|
||||
fs.copyFileSync(sourcePath, targetPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function init(): void {
|
||||
initDirs()
|
||||
initConfig()
|
||||
initFiles()
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
import { controledMihomoConfig } from './config'
|
||||
import { controledMihomoConfig } from '../config'
|
||||
|
||||
export function triggerSysProxy(enable: boolean): void {
|
||||
if (enable) {
|
||||
@ -5,8 +5,8 @@ import {
|
||||
mihomoRules,
|
||||
mihomoVersion,
|
||||
patchMihomoConfig
|
||||
} from './mihomoApi'
|
||||
import { checkAutoRun, disableAutoRun, enableAutoRun } from './autoRun'
|
||||
} from '../core/mihomoApi'
|
||||
import { checkAutoRun, disableAutoRun, enableAutoRun } from '../resolve/autoRun'
|
||||
import {
|
||||
getAppConfig,
|
||||
setAppConfig,
|
||||
@ -17,9 +17,9 @@ import {
|
||||
getProfileItem,
|
||||
addProfileItem,
|
||||
removeProfileItem
|
||||
} from './config'
|
||||
import { restartCore } from './manager'
|
||||
import { triggerSysProxy } from './sysproxy'
|
||||
} from '../config'
|
||||
import { restartCore } from '../core/manager'
|
||||
import { triggerSysProxy } from '../resolve/sysproxy'
|
||||
|
||||
export function registerIpcMainHandlers(): void {
|
||||
ipcMain.handle('mihomoVersion', mihomoVersion)
|
||||
@ -1,31 +1,23 @@
|
||||
import { is } from '@electron-toolkit/utils'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
export const dataDir = app.getPath('userData')
|
||||
|
||||
export function initDirs(): void {
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir)
|
||||
}
|
||||
if (!fs.existsSync(profilesDir())) {
|
||||
fs.mkdirSync(profilesDir())
|
||||
}
|
||||
if (!fs.existsSync(mihomoWorkDir())) {
|
||||
fs.mkdirSync(mihomoWorkDir())
|
||||
}
|
||||
if (!fs.existsSync(logDir())) {
|
||||
fs.mkdirSync(logDir())
|
||||
export function resourcesDir(): string {
|
||||
if (is.dev) {
|
||||
return path.join(__dirname, '../../resources')
|
||||
} else {
|
||||
return path.join(process.resourcesPath)
|
||||
}
|
||||
}
|
||||
|
||||
export function mihomoCoreDir(): string {
|
||||
if (is.dev) {
|
||||
return path.join(__dirname, '../../resources/sidecar')
|
||||
} else {
|
||||
return path.join(process.resourcesPath, 'sidecar')
|
||||
export function resourcesFilesDir(): string {
|
||||
return path.join(resourcesDir(), 'files')
|
||||
}
|
||||
|
||||
export function mihomoCoreDir(): string {
|
||||
return path.join(resourcesDir(), 'sidecar')
|
||||
}
|
||||
|
||||
export function mihomoCorePath(core: string): string {
|
||||
Loading…
x
Reference in New Issue
Block a user