Skip to content

Commit

Permalink
fix auto update profile
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 12, 2024
1 parent 6edb0a6 commit 2b62a2f
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 53 deletions.
8 changes: 0 additions & 8 deletions src/main/config/override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 2 additions & 10 deletions src/main/config/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}`)
Expand All @@ -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
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,8 +188,6 @@ const mihomoTraffic = (): void => {
if (trafficRetry) {
trafficRetry--
mihomoTraffic()
} else {
dialog.showErrorBox('External controller traffic error', 'Retry limit reached')
}
}

Expand Down Expand Up @@ -234,8 +231,6 @@ const mihomoMemory = (): void => {
if (memoryRetry) {
memoryRetry--
mihomoMemory()
} else {
dialog.showErrorBox('External controller memory error', 'Retry limit reached')
}
}

Expand Down Expand Up @@ -281,8 +276,6 @@ const mihomoLogs = (): void => {
if (logsRetry) {
logsRetry--
mihomoLogs()
} else {
dialog.showErrorBox('External controller logs error', 'Retry limit reached')
}
}

Expand Down Expand Up @@ -328,8 +321,6 @@ const mihomoConnections = (): void => {
if (connectionsRetry) {
connectionsRetry--
mihomoConnections()
} else {
dialog.showErrorBox('External controller connections error', 'Retry limit reached')
}
}

Expand Down
34 changes: 21 additions & 13 deletions src/main/core/profileUpdater.ts
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down
1 change: 0 additions & 1 deletion src/main/core/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const buildContextMenu = (): Menu => {
window?.webContents.send('appConfigUpdated')
} catch (e) {
setAppConfig({ sysProxy: { enable: !enable } })
console.error(e)
} finally {
updateTrayMenu()
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
})
}
Expand Down
8 changes: 2 additions & 6 deletions src/renderer/src/components/sider/sysproxy-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/src/pages/syspeoxy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 2b62a2f

Please sign in to comment.