mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 05:00: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 yaml from 'yaml'
|
||||||
import fs from 'fs'
|
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 profileConfig: IProfileConfig // profile.yaml
|
||||||
export let currentProfile: Partial<IMihomoConfig> // profiles/xxx.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 {
|
export function getProfileConfig(force = false): IProfileConfig {
|
||||||
if (force || !profileConfig) {
|
if (force || !profileConfig) {
|
||||||
@ -92,16 +42,6 @@ export function removeProfileItem(id: string): void {
|
|||||||
export function getCurrentProfileItem(): IProfileItem {
|
export function getCurrentProfileItem(): IProfileItem {
|
||||||
return getProfileItem(profileConfig.current)
|
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> {
|
export async function createProfile(item: Partial<IProfileItem>): Promise<IProfileItem> {
|
||||||
const id = item.id || new Date().getTime().toString(16)
|
const id = item.id || new Date().getTime().toString(16)
|
||||||
@ -163,3 +103,14 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
|
|||||||
|
|
||||||
return newItem
|
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 { ChildProcess, execSync, spawn } from 'child_process'
|
||||||
import { logPath, mihomoCorePath, mihomoWorkDir } from './dirs'
|
import { logPath, mihomoCorePath, mihomoWorkDir } from '../utils/dirs'
|
||||||
import { generateProfile } from './factory'
|
import { generateProfile } from '../resolve/factory'
|
||||||
import { appConfig } from './config'
|
import { appConfig } from '../config'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
let child: ChildProcess
|
let child: ChildProcess
|
||||||
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import axios, { AxiosInstance } from 'axios'
|
import axios, { AxiosInstance } from 'axios'
|
||||||
import { controledMihomoConfig } from './config'
|
import { controledMihomoConfig } from '../config'
|
||||||
import WebSocket from 'ws'
|
import WebSocket from 'ws'
|
||||||
import { window } from '.'
|
import { window } from '..'
|
||||||
|
|
||||||
let axiosIns: AxiosInstance = null!
|
let axiosIns: AxiosInstance = null!
|
||||||
let mihomoTrafficWs: WebSocket = null!
|
let mihomoTrafficWs: WebSocket = null!
|
||||||
@ -1,11 +1,11 @@
|
|||||||
import { appConfig, controledMihomoConfig, setAppConfig, setControledMihomoConfig } from './config'
|
import { appConfig, controledMihomoConfig, setAppConfig, setControledMihomoConfig } from '../config'
|
||||||
import icoIcon from '../../resources/icon.ico?asset'
|
import icoIcon from '../../../resources/icon.ico?asset'
|
||||||
import pngIcon from '../../resources/icon.png?asset'
|
import pngIcon from '../../../resources/icon.png?asset'
|
||||||
import { patchMihomoConfig } from './mihomoApi'
|
import { patchMihomoConfig } from './mihomoApi'
|
||||||
import { window } from '.'
|
import { window } from '..'
|
||||||
import { app, ipcMain, Menu, shell, Tray } from 'electron'
|
import { app, ipcMain, Menu, shell, Tray } from 'electron'
|
||||||
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from './dirs'
|
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
|
||||||
import { triggerSysProxy } from './sysproxy'
|
import { triggerSysProxy } from '../resolve/sysproxy'
|
||||||
|
|
||||||
let tray: Tray | null = null
|
let tray: Tray | null = null
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ const buildContextMenu = (): Menu => {
|
|||||||
{
|
{
|
||||||
type: 'checkbox',
|
type: 'checkbox',
|
||||||
label: '系统代理',
|
label: '系统代理',
|
||||||
checked: appConfig.sysProxy.enable,
|
checked: appConfig.sysProxy?.enable ?? false,
|
||||||
click: (item): void => {
|
click: (item): void => {
|
||||||
const enable = item.checked
|
const enable = item.checked
|
||||||
setAppConfig({ sysProxy: { enable } })
|
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 { 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 icon from '../../resources/icon.png?asset'
|
||||||
import { registerIpcMainHandlers } from './cmds'
|
import { mihomoTraffic } from './core/mihomoApi'
|
||||||
import { initConfig, appConfig } from './config'
|
import { createTray } from './core/tray'
|
||||||
import { stopCore, startCore } from './manager'
|
import { init } from './resolve/init'
|
||||||
import { initDirs } from './dirs'
|
import { appConfig } from './config'
|
||||||
import { mihomoTraffic } from './mihomoApi'
|
import { join } from 'path'
|
||||||
import { createTray } from './tray'
|
|
||||||
|
|
||||||
export let window: BrowserWindow | null = null
|
export let window: BrowserWindow | null = null
|
||||||
|
|
||||||
@ -16,8 +16,7 @@ const gotTheLock = app.requestSingleInstanceLock()
|
|||||||
if (!gotTheLock) {
|
if (!gotTheLock) {
|
||||||
app.quit()
|
app.quit()
|
||||||
} else {
|
} else {
|
||||||
initDirs()
|
init()
|
||||||
initConfig()
|
|
||||||
startCore()
|
startCore()
|
||||||
|
|
||||||
app.on('second-instance', () => {
|
app.on('second-instance', () => {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { controledMihomoConfig, currentProfile } from './config'
|
import { controledMihomoConfig, currentProfile } from '../config'
|
||||||
import { mihomoWorkConfigPath } from './dirs'
|
import { mihomoWorkConfigPath } from '../utils/dirs'
|
||||||
import yaml from 'yaml'
|
import yaml from 'yaml'
|
||||||
import fs from 'fs'
|
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 {
|
export function triggerSysProxy(enable: boolean): void {
|
||||||
if (enable) {
|
if (enable) {
|
||||||
@ -5,8 +5,8 @@ import {
|
|||||||
mihomoRules,
|
mihomoRules,
|
||||||
mihomoVersion,
|
mihomoVersion,
|
||||||
patchMihomoConfig
|
patchMihomoConfig
|
||||||
} from './mihomoApi'
|
} from '../core/mihomoApi'
|
||||||
import { checkAutoRun, disableAutoRun, enableAutoRun } from './autoRun'
|
import { checkAutoRun, disableAutoRun, enableAutoRun } from '../resolve/autoRun'
|
||||||
import {
|
import {
|
||||||
getAppConfig,
|
getAppConfig,
|
||||||
setAppConfig,
|
setAppConfig,
|
||||||
@ -17,9 +17,9 @@ import {
|
|||||||
getProfileItem,
|
getProfileItem,
|
||||||
addProfileItem,
|
addProfileItem,
|
||||||
removeProfileItem
|
removeProfileItem
|
||||||
} from './config'
|
} from '../config'
|
||||||
import { restartCore } from './manager'
|
import { restartCore } from '../core/manager'
|
||||||
import { triggerSysProxy } from './sysproxy'
|
import { triggerSysProxy } from '../resolve/sysproxy'
|
||||||
|
|
||||||
export function registerIpcMainHandlers(): void {
|
export function registerIpcMainHandlers(): void {
|
||||||
ipcMain.handle('mihomoVersion', mihomoVersion)
|
ipcMain.handle('mihomoVersion', mihomoVersion)
|
||||||
@ -1,31 +1,23 @@
|
|||||||
import { is } from '@electron-toolkit/utils'
|
import { is } from '@electron-toolkit/utils'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import fs from 'fs'
|
|
||||||
|
|
||||||
export const dataDir = app.getPath('userData')
|
export const dataDir = app.getPath('userData')
|
||||||
|
|
||||||
export function initDirs(): void {
|
export function resourcesDir(): string {
|
||||||
if (!fs.existsSync(dataDir)) {
|
if (is.dev) {
|
||||||
fs.mkdirSync(dataDir)
|
return path.join(__dirname, '../../resources')
|
||||||
}
|
} else {
|
||||||
if (!fs.existsSync(profilesDir())) {
|
return path.join(process.resourcesPath)
|
||||||
fs.mkdirSync(profilesDir())
|
|
||||||
}
|
|
||||||
if (!fs.existsSync(mihomoWorkDir())) {
|
|
||||||
fs.mkdirSync(mihomoWorkDir())
|
|
||||||
}
|
|
||||||
if (!fs.existsSync(logDir())) {
|
|
||||||
fs.mkdirSync(logDir())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mihomoCoreDir(): string {
|
export function resourcesFilesDir(): string {
|
||||||
if (is.dev) {
|
return path.join(resourcesDir(), 'files')
|
||||||
return path.join(__dirname, '../../resources/sidecar')
|
|
||||||
} else {
|
|
||||||
return path.join(process.resourcesPath, 'sidecar')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mihomoCoreDir(): string {
|
||||||
|
return path.join(resourcesDir(), 'sidecar')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mihomoCorePath(core: string): string {
|
export function mihomoCorePath(core: string): string {
|
||||||
Loading…
x
Reference in New Issue
Block a user