proxy provider

This commit is contained in:
pompurin404 2024-08-09 11:22:48 +08:00
parent 6d954a5986
commit 0fda9d0c09
No known key found for this signature in database
5 changed files with 128 additions and 1 deletions

View File

@ -68,6 +68,20 @@ export const mihomoProxies = async (): Promise<IMihomoProxies> => {
})) as IMihomoProxies
}
export const mihomoProxyProviders = async (): Promise<IMihomoProxyProviders> => {
const instance = await getAxios()
return (await instance.get('/providers/proxies').catch(() => {
return { providers: {} }
})) as IMihomoProxyProviders
}
export const mihomoUpdateProxyProviders = async (name: string): Promise<void> => {
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<IMihomoProxy> => {
const instance = await getAxios()
return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => {

View File

@ -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))

View File

@ -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 (
<SettingCard>
{providers.map((provider, index) => {
return (
<>
<SettingItem
title={provider.name}
key={provider.name}
divider={!provider.subscriptionInfo && index !== providers.length - 1}
>
{
<div className="flex h-[32px] leading-[32px]">
<div>{dayjs(provider.updateAt).fromNow()}</div>
<Button
isIconOnly
className="ml-2"
size="sm"
onPress={() => {
setUpdating((prev) => {
prev[index] = true
return [...prev]
})
mihomoUpdateProxyProviders(provider.name).finally(() => {
setUpdating((prev) => {
prev[index] = false
return [...prev]
})
mutate()
})
}}
>
<IoMdRefresh className={`text-lg ${updating[index] ? 'animate-spin' : ''}`} />
</Button>
</div>
}
</SettingItem>
{provider.subscriptionInfo && (
<SettingItem
divider={index !== providers.length - 1}
title={`${calcTraffic(
provider.subscriptionInfo.Upload + provider.subscriptionInfo.Download
)}
/${calcTraffic(provider.subscriptionInfo.Total)}`}
key={provider.name}
>
{provider.subscriptionInfo && (
<div className="flex h-[32px] leading-[32px]">
<div className="ml-2">
{dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
</div>
</div>
)}
</SettingItem>
)}
</>
)
})}
</SettingCard>
)
}
export default ProxyProvider

View File

@ -18,6 +18,14 @@ export async function mihomoProxies(): Promise<IMihomoProxies> {
return await window.electron.ipcRenderer.invoke('mihomoProxies')
}
export async function mihomoProxyProviders(): Promise<IMihomoProxyProviders> {
return await window.electron.ipcRenderer.invoke('mihomoProxyProviders')
}
export async function mihomoUpdateProxyProviders(name: string): Promise<void> {
return await window.electron.ipcRenderer.invoke('mihomoUpdateProxyProviders', name)
}
export async function mihomoChangeProxy(group: string, proxy: string): Promise<IMihomoProxy> {
return await window.electron.ipcRenderer.invoke('mihomoChangeProxy', group, proxy)
}

22
src/shared/types.d.ts vendored
View File

@ -143,6 +143,28 @@ interface IMihomoProxies {
proxies: Record<string, IMihomoProxy | IMihomoGroup>
}
interface IMihomoProxyProviders {
providers: Record<string, IMihomoProxyProvider>
}
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