Skip to content

Commit

Permalink
refactor the main thread code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pompurin404 committed Aug 2, 2024
1 parent ebbd3a2 commit 7c00ddc
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 112 deletions.
17 changes: 17 additions & 0 deletions src/main/config/app.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/main/config/controledMihomo.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/main/config/index.ts
Original file line number Diff line number Diff line change
@@ -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'
79 changes: 15 additions & 64 deletions src/main/config.ts → src/main/config/profile.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions src/main/manager.ts → src/main/core/manager.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/main/mihomoApi.ts → src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
14 changes: 7 additions & 7 deletions src/main/tray.ts → src/main/core/tray.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 } })
Expand Down
19 changes: 9 additions & 10 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -16,8 +16,7 @@ const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
initDirs()
initConfig()
init()
startCore()

app.on('second-instance', () => {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/main/factory.ts → src/main/resolve/factory.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
77 changes: 77 additions & 0 deletions src/main/resolve/init.ts
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion src/main/sysproxy.ts → src/main/resolve/sysproxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { controledMihomoConfig } from './config'
import { controledMihomoConfig } from '../config'

export function triggerSysProxy(enable: boolean): void {
if (enable) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/cmds.ts → src/main/utils/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
28 changes: 10 additions & 18 deletions src/main/dirs.ts → src/main/utils/dirs.ts
Original file line number Diff line number Diff line change
@@ -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 resourcesFilesDir(): string {
return path.join(resourcesDir(), 'files')
}

export function mihomoCoreDir(): string {
if (is.dev) {
return path.join(__dirname, '../../resources/sidecar')
} else {
return path.join(process.resourcesPath, 'sidecar')
}
return path.join(resourcesDir(), 'sidecar')
}

export function mihomoCorePath(core: string): string {
Expand Down
File renamed without changes.

0 comments on commit 7c00ddc

Please sign in to comment.