mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 19:50:28 +08:00
fix: resolve async/null-safety issues in lifecycle, config and ipc modules
This commit is contained in:
parent
9e5d11c3c8
commit
3097019e9e
@ -54,9 +54,12 @@ export async function addOverrideItem(item: Partial<IOverrideItem>): Promise<voi
|
||||
export async function removeOverrideItem(id: string): Promise<void> {
|
||||
const config = await getOverrideConfig()
|
||||
const item = await getOverrideItem(id)
|
||||
config.items = config.items?.filter((item) => item.id !== id)
|
||||
if (!item) return
|
||||
config.items = config.items?.filter((i) => i.id !== id)
|
||||
await setOverrideConfig(config)
|
||||
await rm(overridePath(id, item?.ext || 'js'))
|
||||
if (existsSync(overridePath(id, item.ext))) {
|
||||
await rm(overridePath(id, item.ext))
|
||||
}
|
||||
}
|
||||
|
||||
export async function createOverride(item: Partial<IOverrideItem>): Promise<IOverrideItem> {
|
||||
|
||||
@ -368,12 +368,16 @@ export async function getProfile(id: string | undefined): Promise<IMihomoConfig>
|
||||
// attachment;filename=xxx.yaml; filename*=UTF-8''%xx%xx%xx
|
||||
function parseFilename(str: string): string {
|
||||
if (str.match(/filename\*=.*''/)) {
|
||||
const filename = decodeURIComponent(str.split(/filename\*=.*''/)[1])
|
||||
return filename
|
||||
} else {
|
||||
const filename = str.split('filename=')[1]
|
||||
return filename
|
||||
const parts = str.split(/filename\*=.*''/)
|
||||
if (parts[1]) {
|
||||
return decodeURIComponent(parts[1])
|
||||
}
|
||||
}
|
||||
const parts = str.split('filename=')
|
||||
if (parts[1]) {
|
||||
return parts[1].replace(/^["']|["']$/g, '')
|
||||
}
|
||||
return 'Remote File'
|
||||
}
|
||||
|
||||
// subscription-userinfo: upload=1234; download=2234; total=1024000; expire=2218532293
|
||||
|
||||
@ -25,8 +25,8 @@ import { createLogger } from '../utils/logger'
|
||||
|
||||
const factoryLogger = createLogger('Factory')
|
||||
|
||||
let runtimeConfigStr: string
|
||||
let runtimeConfig: IMihomoConfig
|
||||
let runtimeConfigStr: string = ''
|
||||
let runtimeConfig: IMihomoConfig = {} as IMihomoConfig
|
||||
|
||||
// 辅助函数:处理带偏移量的规则
|
||||
function processRulesWithOffset(ruleStrings: string[], currentRules: string[], isAppend = false) {
|
||||
|
||||
@ -14,8 +14,7 @@ done
|
||||
${process.argv.join(' ')} & disown
|
||||
exit
|
||||
`
|
||||
spawn('sh', ['-c', `"${script}"`], {
|
||||
shell: true,
|
||||
spawn('sh', ['-c', script], {
|
||||
detached: true,
|
||||
stdio: 'ignore'
|
||||
})
|
||||
@ -57,7 +56,7 @@ export function setupPlatformSpecifics(): void {
|
||||
export function setupAppLifecycle(): void {
|
||||
app.on('before-quit', async (e) => {
|
||||
e.preventDefault()
|
||||
triggerSysProxy(false)
|
||||
await triggerSysProxy(false)
|
||||
await stopCore()
|
||||
app.exit()
|
||||
})
|
||||
|
||||
@ -10,47 +10,52 @@ import axios from 'axios'
|
||||
import fs from 'fs'
|
||||
import { proxyLogger } from '../utils/logger'
|
||||
|
||||
let defaultBypass: string[]
|
||||
let triggerSysProxyTimer: NodeJS.Timeout | null = null
|
||||
const helperSocketPath = '/tmp/mihomo-party-helper.sock'
|
||||
|
||||
if (process.platform === 'linux')
|
||||
defaultBypass = ['localhost', '127.0.0.1', '192.168.0.0/16', '10.0.0.0/8', '172.16.0.0/12', '::1']
|
||||
if (process.platform === 'darwin')
|
||||
defaultBypass = [
|
||||
'127.0.0.1',
|
||||
'192.168.0.0/16',
|
||||
'10.0.0.0/8',
|
||||
'172.16.0.0/12',
|
||||
'localhost',
|
||||
'*.local',
|
||||
'*.crashlytics.com',
|
||||
'<local>'
|
||||
]
|
||||
if (process.platform === 'win32')
|
||||
defaultBypass = [
|
||||
'localhost',
|
||||
'127.*',
|
||||
'192.168.*',
|
||||
'10.*',
|
||||
'172.16.*',
|
||||
'172.17.*',
|
||||
'172.18.*',
|
||||
'172.19.*',
|
||||
'172.20.*',
|
||||
'172.21.*',
|
||||
'172.22.*',
|
||||
'172.23.*',
|
||||
'172.24.*',
|
||||
'172.25.*',
|
||||
'172.26.*',
|
||||
'172.27.*',
|
||||
'172.28.*',
|
||||
'172.29.*',
|
||||
'172.30.*',
|
||||
'172.31.*',
|
||||
'<local>'
|
||||
]
|
||||
const defaultBypass: string[] = (() => {
|
||||
switch (process.platform) {
|
||||
case 'linux':
|
||||
return ['localhost', '127.0.0.1', '192.168.0.0/16', '10.0.0.0/8', '172.16.0.0/12', '::1']
|
||||
case 'darwin':
|
||||
return [
|
||||
'127.0.0.1',
|
||||
'192.168.0.0/16',
|
||||
'10.0.0.0/8',
|
||||
'172.16.0.0/12',
|
||||
'localhost',
|
||||
'*.local',
|
||||
'*.crashlytics.com',
|
||||
'<local>'
|
||||
]
|
||||
case 'win32':
|
||||
return [
|
||||
'localhost',
|
||||
'127.*',
|
||||
'192.168.*',
|
||||
'10.*',
|
||||
'172.16.*',
|
||||
'172.17.*',
|
||||
'172.18.*',
|
||||
'172.19.*',
|
||||
'172.20.*',
|
||||
'172.21.*',
|
||||
'172.22.*',
|
||||
'172.23.*',
|
||||
'172.24.*',
|
||||
'172.25.*',
|
||||
'172.26.*',
|
||||
'172.27.*',
|
||||
'172.28.*',
|
||||
'172.29.*',
|
||||
'172.30.*',
|
||||
'172.31.*',
|
||||
'<local>'
|
||||
]
|
||||
default:
|
||||
return ['localhost', '127.0.0.1', '192.168.0.0/16', '10.0.0.0/8', '172.16.0.0/12', '::1']
|
||||
}
|
||||
})()
|
||||
|
||||
export async function triggerSysProxy(enable: boolean): Promise<void> {
|
||||
if (net.isOnline()) {
|
||||
|
||||
@ -213,8 +213,12 @@ async function cleanup(): Promise<void> {
|
||||
// logs
|
||||
const { maxLogDays = 7 } = await getAppConfig()
|
||||
const logs = await readdir(logDir())
|
||||
const datePattern = /^\d{4}-\d{2}-\d{2}/
|
||||
for (const log of logs) {
|
||||
const date = new Date(log.split('.')[0])
|
||||
const match = log.match(datePattern)
|
||||
if (!match) continue
|
||||
const date = new Date(match[0])
|
||||
if (isNaN(date.getTime())) continue
|
||||
const diff = Date.now() - date.getTime()
|
||||
if (diff > maxLogDays * 24 * 60 * 60 * 1000) {
|
||||
try {
|
||||
|
||||
@ -118,7 +118,7 @@ function setupWindowEvents(
|
||||
})
|
||||
|
||||
window.on('session-end', async () => {
|
||||
triggerSysProxy(false)
|
||||
await triggerSysProxy(false)
|
||||
await stopCore()
|
||||
})
|
||||
|
||||
|
||||
@ -196,10 +196,11 @@ export const writeTheme = (theme: string, css: string): Promise<void> =>
|
||||
invoke('writeTheme', theme, css)
|
||||
|
||||
let applyThemeRunning = false
|
||||
const applyThemeWaitList: string[] = []
|
||||
let pendingTheme: string | null = null
|
||||
|
||||
export async function applyTheme(theme: string): Promise<void> {
|
||||
if (applyThemeRunning) {
|
||||
applyThemeWaitList.push(theme)
|
||||
pendingTheme = theme
|
||||
return
|
||||
}
|
||||
applyThemeRunning = true
|
||||
@ -207,11 +208,10 @@ export async function applyTheme(theme: string): Promise<void> {
|
||||
await invoke<void>('applyTheme', theme)
|
||||
} finally {
|
||||
applyThemeRunning = false
|
||||
if (applyThemeWaitList.length > 0) {
|
||||
const nextTheme = applyThemeWaitList.shift()
|
||||
if (nextTheme) {
|
||||
await applyTheme(nextTheme)
|
||||
}
|
||||
if (pendingTheme !== null) {
|
||||
const nextTheme = pendingTheme
|
||||
pendingTheme = null
|
||||
await applyTheme(nextTheme)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user