From 0fda9d0c090cc3630c04601e9c93633d063d757a Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Fri, 9 Aug 2024 11:22:48 +0800 Subject: [PATCH] proxy provider --- src/main/core/mihomoApi.ts | 14 ++++ src/main/utils/ipc.ts | 4 + .../components/resources/proxy-provider.tsx | 81 ++++++++++++++++++- src/renderer/src/utils/ipc.ts | 8 ++ src/shared/types.d.ts | 22 +++++ 5 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 8c1bed5..7bfa0bb 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -68,6 +68,20 @@ export const mihomoProxies = async (): Promise => { })) as IMihomoProxies } +export const mihomoProxyProviders = async (): Promise => { + const instance = await getAxios() + return (await instance.get('/providers/proxies').catch(() => { + return { providers: {} } + })) as IMihomoProxyProviders +} + +export const mihomoUpdateProxyProviders = async (name: string): Promise => { + const instance = await getAxios() + return instance.put(`/providers/proxies/${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/utils/ipc.ts b/src/main/utils/ipc.ts index b7fc7db..8c2da80 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -5,7 +5,9 @@ import { mihomoCloseConnection, mihomoProxies, mihomoProxyDelay, + mihomoProxyProviders, mihomoRules, + mihomoUpdateProxyProviders, mihomoUpgradeGeo, mihomoVersion, patchMihomoConfig, @@ -44,6 +46,8 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('mihomoCloseAllConnections', mihomoCloseAllConnections) ipcMain.handle('mihomoRules', mihomoRules) ipcMain.handle('mihomoProxies', mihomoProxies) + ipcMain.handle('mihomoProxyProviders', () => mihomoProxyProviders()) + ipcMain.handle('mihomoUpdateProxyProviders', (_e, name) => mihomoUpdateProxyProviders(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 8760e49..e3e3f0e 100644 --- a/src/renderer/src/components/resources/proxy-provider.tsx +++ b/src/renderer/src/components/resources/proxy-provider.tsx @@ -1,5 +1,84 @@ +import { mihomoProxyProviders, mihomoUpdateProxyProviders } from '@renderer/utils/ipc' +import { 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 { IoMdRefresh } from 'react-icons/io' +import dayjs from 'dayjs' +import { calcTraffic } from '@renderer/utils/calc' + const ProxyProvider: React.FC = () => { - return <> + const { data, mutate } = useSWR('mihomoProxyProviders', mihomoProxyProviders) + const providers = useMemo(() => { + if (!data) return [] + 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)) + return ( + + {providers.map((provider, index) => { + return ( + <> + + { +
+
{dayjs(provider.updateAt).fromNow()}
+ +
+ } +
+ {provider.subscriptionInfo && ( + + {provider.subscriptionInfo && ( +
+
+ {dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')} +
+
+ )} +
+ )} + + ) + })} +
+ ) } export default ProxyProvider diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index 442ca07..94ef8d6 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -18,6 +18,14 @@ export async function mihomoProxies(): Promise { return await window.electron.ipcRenderer.invoke('mihomoProxies') } +export async function mihomoProxyProviders(): Promise { + return await window.electron.ipcRenderer.invoke('mihomoProxyProviders') +} + +export async function mihomoUpdateProxyProviders(name: string): Promise { + return await window.electron.ipcRenderer.invoke('mihomoUpdateProxyProviders', 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 0c9c290..64a6bb6 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -143,6 +143,28 @@ interface IMihomoProxies { proxies: Record } +interface IMihomoProxyProviders { + providers: Record +} + +interface ISubscriptionUserInfoUpper { + Upload: number + Download: number + Total: number + Expire: number +} + +interface IMihomoProxyProvider { + name: string + type: string + proxies?: IMihomoProxy[] + subscriptionInfo?: ISubscriptionUserInfoUpper + expectedStatus: string + testUrl?: string + updateAt?: string + vehicleType: string +} + interface ISysProxyConfig { enable: boolean host?: string