fix auto update profile

This commit is contained in:
pompurin404 2024-08-12 15:55:26 +08:00
parent 6edb0a6653
commit 2b62a2f1e1
No known key found for this signature in database
9 changed files with 30 additions and 53 deletions

View File

@ -56,10 +56,6 @@ export async function createOverride(item: Partial<IOverrideItem>): Promise<IOve
switch (newItem.type) {
case 'remote': {
if (!item.url) {
dialog.showErrorBox(
'URL is required for remote script',
'URL is required for remote script'
)
throw new Error('URL is required for remote script')
}
try {
@ -81,10 +77,6 @@ export async function createOverride(item: Partial<IOverrideItem>): Promise<IOve
}
case 'local': {
if (!item.file) {
dialog.showErrorBox(
'File is required for local script',
'File is required for local script'
)
throw new Error('File is required for local script')
}
const data = item.file

View File

@ -119,10 +119,6 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
switch (newItem.type) {
case 'remote': {
if (!item.url) {
dialog.showErrorBox(
'URL is required for remote profile',
'URL is required for remote profile'
)
throw new Error('URL is required for remote profile')
}
try {
@ -152,7 +148,7 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
if (headers['subscription-userinfo']) {
newItem.extra = parseSubinfo(headers['subscription-userinfo'])
}
setProfileStr(id, data)
await setProfileStr(id, data)
} catch (e) {
dialog.showErrorBox('Failed to fetch remote profile', `${e}\nurl: ${item.url}`)
throw new Error(`Failed to fetch remote profile ${e}`)
@ -161,14 +157,10 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
}
case 'local': {
if (!item.file) {
dialog.showErrorBox(
'File is required for local profile',
'File is required for local profile'
)
throw new Error('File is required for local profile')
}
const data = item.file
setProfileStr(id, data)
await setProfileStr(id, data)
break
}
}

View File

@ -2,7 +2,6 @@ import axios, { AxiosInstance } from 'axios'
import { getAppConfig, getControledMihomoConfig } from '../config'
import WebSocket from 'ws'
import { window } from '..'
import { dialog } from 'electron'
let axiosIns: AxiosInstance = null!
let mihomoTrafficWs: WebSocket | null = null
@ -189,8 +188,6 @@ const mihomoTraffic = (): void => {
if (trafficRetry) {
trafficRetry--
mihomoTraffic()
} else {
dialog.showErrorBox('External controller traffic error', 'Retry limit reached')
}
}
@ -234,8 +231,6 @@ const mihomoMemory = (): void => {
if (memoryRetry) {
memoryRetry--
mihomoMemory()
} else {
dialog.showErrorBox('External controller memory error', 'Retry limit reached')
}
}
@ -281,8 +276,6 @@ const mihomoLogs = (): void => {
if (logsRetry) {
logsRetry--
mihomoLogs()
} else {
dialog.showErrorBox('External controller logs error', 'Retry limit reached')
}
}
@ -328,8 +321,6 @@ const mihomoConnections = (): void => {
if (connectionsRetry) {
connectionsRetry--
mihomoConnections()
} else {
dialog.showErrorBox('External controller connections error', 'Retry limit reached')
}
}

View File

@ -1,34 +1,42 @@
import { addProfileItem, getProfileConfig, getProfileItem } from '../config'
import { addProfileItem, getCurrentProfileItem, getProfileConfig, getProfileItem } from '../config'
const intervalPool: Record<string, NodeJS.Timeout> = {}
export function initProfileUpdater(): void {
const { items } = getProfileConfig()
for (const item of items) {
export async function initProfileUpdater(): Promise<void> {
const { items, current } = getProfileConfig()
const currentItem = getCurrentProfileItem()
for (const item of items.filter((i) => i.id !== current)) {
if (item.type === 'remote' && item.interval) {
addProfileItem(getProfileItem(item.id))
await addProfileItem(item)
intervalPool[item.id] = setInterval(
() => {
addProfileItem(getProfileItem(item.id))
async () => {
await addProfileItem(item)
},
item.interval * 60 * 1000
)
}
}
if (currentItem.type === 'remote' && currentItem.interval) {
await addProfileItem(currentItem)
intervalPool[currentItem.id] = setInterval(
async () => {
await addProfileItem(currentItem)
},
currentItem.interval * 60 * 1000 + 10000 // +10s
)
}
}
export function addProfileUpdater(id: string): void {
const { items } = getProfileConfig()
const item = items.find((i) => i.id === id)
const item = getProfileItem(id)
if (item?.type === 'remote' && item.interval) {
if (item.type === 'remote' && item.interval) {
if (intervalPool[id]) {
clearInterval(intervalPool[id])
}
intervalPool[id] = setInterval(
() => {
addProfileItem(getProfileItem(id))
async () => {
await addProfileItem(item)
},
item.interval * 60 * 1000
)

View File

@ -74,7 +74,6 @@ const buildContextMenu = (): Menu => {
window?.webContents.send('appConfigUpdated')
} catch (e) {
setAppConfig({ sysProxy: { enable: !enable } })
console.error(e)
} finally {
updateTrayMenu()
}

View File

@ -59,9 +59,9 @@ if (!gotTheLock) {
// Set app user model id for windows
electronApp.setAppUserModelId('party.mihomo.app')
startCore().then(() => {
setTimeout(() => {
initProfileUpdater()
}, 30000)
setTimeout(async () => {
await initProfileUpdater()
}, 60000)
})
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.

View File

@ -116,7 +116,7 @@ export function registerIpcMainHandlers(): void {
function getFilePath(ext: string[]): string[] | undefined {
return dialog.showOpenDialogSync({
title: '选择订阅文件',
filters: [{ name: 'Yaml Files', extensions: ext }],
filters: [{ name: `${ext} file`, extensions: ext }],
properties: ['openFile']
})
}

View File

@ -19,12 +19,8 @@ const SysproxySwitcher: React.FC = () => {
id: 'sysproxy'
})
const onChange = async (enable: boolean): Promise<void> => {
try {
await triggerSysProxy(enable)
await patchAppConfig({ sysProxy: { enable } })
} catch (e) {
console.error(e)
}
await triggerSysProxy(enable)
await patchAppConfig({ sysProxy: { enable } })
}
return (

View File

@ -90,9 +90,8 @@ const Sysproxy: React.FC = () => {
try {
await triggerSysProxy(true)
await patchAppConfig({ sysProxy: { enable: true } })
} catch (e) {
} catch {
await patchAppConfig({ sysProxy: { enable: false } })
console.error(e)
}
}