From 88f7d4ee0b4744ee11edb44a010faf81bf71a27c Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Fri, 9 Aug 2024 11:52:17 +0800 Subject: [PATCH] rule provider --- src/main/core/mihomoApi.ts | 14 +++ src/main/core/profileUpdater.ts | 1 - src/main/utils/ipc.ts | 4 + .../components/resources/proxy-provider.tsx | 76 ++++++++++------ .../components/resources/rule-provider.tsx | 86 ++++++++++++++++++- src/renderer/src/utils/ipc.ts | 8 ++ src/shared/types.d.ts | 16 +++- 7 files changed, 174 insertions(+), 31 deletions(-) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 7bfa0bb..dbe78ea 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -82,6 +82,20 @@ export const mihomoUpdateProxyProviders = async (name: string): Promise => }) } +export const mihomoRuleProviders = async (): Promise => { + const instance = await getAxios() + return (await instance.get('/providers/rules').catch(() => { + return { providers: {} } + })) as IMihomoRuleProviders +} + +export const mihomoUpdateRuleProviders = async (name: string): Promise => { + const instance = await getAxios() + return instance.put(`/providers/rules/${encodeURIComponent(name)}`).catch((e) => { + return e.response.data + }) +} + export const mihomoChangeProxy = async (group: string, proxy: string): Promise => { const instance = await getAxios() return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => { diff --git a/src/main/core/profileUpdater.ts b/src/main/core/profileUpdater.ts index 616424c..58c6052 100644 --- a/src/main/core/profileUpdater.ts +++ b/src/main/core/profileUpdater.ts @@ -6,7 +6,6 @@ export function initProfileUpdater(): void { const { items } = getProfileConfig() for (const item of items) { - console.log(item.type, item.interval) if (item.type === 'remote' && item.interval) { addProfileItem(getProfileItem(item.id)) intervalPool[item.id] = setInterval( diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 8c2da80..22c48f1 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -6,8 +6,10 @@ import { mihomoProxies, mihomoProxyDelay, mihomoProxyProviders, + mihomoRuleProviders, mihomoRules, mihomoUpdateProxyProviders, + mihomoUpdateRuleProviders, mihomoUpgradeGeo, mihomoVersion, patchMihomoConfig, @@ -48,6 +50,8 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('mihomoProxies', mihomoProxies) ipcMain.handle('mihomoProxyProviders', () => mihomoProxyProviders()) ipcMain.handle('mihomoUpdateProxyProviders', (_e, name) => mihomoUpdateProxyProviders(name)) + ipcMain.handle('mihomoRuleProviders', () => mihomoRuleProviders()) + ipcMain.handle('mihomoUpdateRuleProviders', (_e, name) => mihomoUpdateRuleProviders(name)) ipcMain.handle('mihomoChangeProxy', (_e, group, proxy) => mihomoChangeProxy(group, proxy)) ipcMain.handle('mihomoUpgradeGeo', mihomoUpgradeGeo) ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url)) diff --git a/src/renderer/src/components/resources/proxy-provider.tsx b/src/renderer/src/components/resources/proxy-provider.tsx index e3e3f0e..57fe476 100644 --- a/src/renderer/src/components/resources/proxy-provider.tsx +++ b/src/renderer/src/components/resources/proxy-provider.tsx @@ -1,9 +1,9 @@ import { mihomoProxyProviders, mihomoUpdateProxyProviders } from '@renderer/utils/ipc' -import { useMemo, useState } from 'react' +import { Fragment, useMemo, useState } from 'react' import useSWR from 'swr' import SettingCard from '../base/base-setting-card' import SettingItem from '../base/base-setting-item' -import { Button } from '@nextui-org/react' +import { Button, Chip } from '@nextui-org/react' import { IoMdRefresh } from 'react-icons/io' import dayjs from 'dayjs' import { calcTraffic } from '@renderer/utils/calc' @@ -15,40 +15,61 @@ const ProxyProvider: React.FC = () => { return Object.keys(data.providers) .map((key) => data.providers[key]) .filter((provider) => { - console.log(provider) return 'subscriptionInfo' in provider }) }, [data]) const [updating, setUpdating] = useState(Array(providers.length).fill(false)) + + const onUpdate = (name: string, index: number): void => { + setUpdating((prev) => { + prev[index] = true + return [...prev] + }) + mihomoUpdateProxyProviders(name).finally(() => { + setUpdating((prev) => { + prev[index] = false + return [...prev] + }) + mutate() + }) + } + return ( + + + {providers.map((provider, index) => { return ( - <> + + {provider.proxies?.length || 0} + + } divider={!provider.subscriptionInfo && index !== providers.length - 1} > { -
-
{dayjs(provider.updateAt).fromNow()}
+
+
{dayjs(provider.updatedAt).fromNow()}
+ } > {provider.subscriptionInfo && ( -
-
- {dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')} -
+
+ {dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
)} )} - + ) })} diff --git a/src/renderer/src/components/resources/rule-provider.tsx b/src/renderer/src/components/resources/rule-provider.tsx index 560d43b..08c1f14 100644 --- a/src/renderer/src/components/resources/rule-provider.tsx +++ b/src/renderer/src/components/resources/rule-provider.tsx @@ -1,5 +1,89 @@ +import { mihomoRuleProviders, mihomoUpdateRuleProviders } from '@renderer/utils/ipc' +import { Fragment, useMemo, useState } from 'react' +import useSWR from 'swr' +import SettingCard from '../base/base-setting-card' +import SettingItem from '../base/base-setting-item' +import { Button, Chip } from '@nextui-org/react' +import { IoMdRefresh } from 'react-icons/io' +import dayjs from 'dayjs' + const RuleProvider: React.FC = () => { - return <> + const { data, mutate } = useSWR('mihomoRuleProviders', mihomoRuleProviders) + const providers = useMemo(() => { + if (!data) return [] + return Object.keys(data.providers).map((key) => data.providers[key]) + }, [data]) + const [updating, setUpdating] = useState(Array(providers.length).fill(false)) + + const onUpdate = (name: string, index: number): void => { + setUpdating((prev) => { + prev[index] = true + return [...prev] + }) + mihomoUpdateRuleProviders(name).finally(() => { + setUpdating((prev) => { + prev[index] = false + return [...prev] + }) + mutate() + }) + } + + return ( + + + + + {providers.map((provider, index) => { + return ( + + + {provider.ruleCount} + + } + > + { +
+
{dayjs(provider.updatedAt).fromNow()}
+ +
+ } +
+ {provider.format}
} + divider={index !== providers.length - 1} + > +
+ {provider.vehicleType}::{provider.behavior} +
+ + + ) + })} + + ) } export default RuleProvider diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index 94ef8d6..59b8844 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -26,6 +26,14 @@ export async function mihomoUpdateProxyProviders(name: string): Promise { return await window.electron.ipcRenderer.invoke('mihomoUpdateProxyProviders', name) } +export async function mihomoRuleProviders(): Promise { + return await window.electron.ipcRenderer.invoke('mihomoRuleProviders') +} + +export async function mihomoUpdateRuleProviders(name: string): Promise { + return await window.electron.ipcRenderer.invoke('mihomoUpdateRuleProviders', name) +} + export async function mihomoChangeProxy(group: string, proxy: string): Promise { return await window.electron.ipcRenderer.invoke('mihomoChangeProxy', group, proxy) } diff --git a/src/shared/types.d.ts b/src/shared/types.d.ts index 64a6bb6..01795f4 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -143,6 +143,20 @@ interface IMihomoProxies { proxies: Record } +interface IMihomoRuleProviders { + providers: Record +} + +interface IMihomoRuleProvider { + behavior: string + format: string + name: string + ruleCount: number + type: string + updatedAt: string + vehicleType: string +} + interface IMihomoProxyProviders { providers: Record } @@ -161,7 +175,7 @@ interface IMihomoProxyProvider { subscriptionInfo?: ISubscriptionUserInfoUpper expectedStatus: string testUrl?: string - updateAt?: string + updatedAt?: string vehicleType: string }