rule provider

This commit is contained in:
pompurin404 2024-08-09 11:52:17 +08:00
parent 0fda9d0c09
commit 88f7d4ee0b
No known key found for this signature in database
7 changed files with 174 additions and 31 deletions

View File

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

View File

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

View File

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

View File

@ -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 (
<SettingCard>
<SettingItem title="代理集合" divider>
<Button
size="sm"
color="primary"
onPress={() => {
providers.forEach((provider, index) => {
onUpdate(provider.name, index)
})
}}
>
</Button>
</SettingItem>
{providers.map((provider, index) => {
return (
<>
<Fragment key={provider.name}>
<SettingItem
title={provider.name}
key={provider.name}
actions={
<Chip className="ml-2" size="sm">
{provider.proxies?.length || 0}
</Chip>
}
divider={!provider.subscriptionInfo && index !== providers.length - 1}
>
{
<div className="flex h-[32px] leading-[32px]">
<div>{dayjs(provider.updateAt).fromNow()}</div>
<div className="flex select-none h-[32px] leading-[32px] text-default-500">
<div>{dayjs(provider.updatedAt).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()
})
onUpdate(provider.name, index)
}}
>
<IoMdRefresh className={`text-lg ${updating[index] ? 'animate-spin' : ''}`} />
@ -59,22 +80,21 @@ const ProxyProvider: React.FC = () => {
{provider.subscriptionInfo && (
<SettingItem
divider={index !== providers.length - 1}
title={`${calcTraffic(
provider.subscriptionInfo.Upload + provider.subscriptionInfo.Download
)}
/${calcTraffic(provider.subscriptionInfo.Total)}`}
key={provider.name}
title={
<div className="text-default-500">{`${calcTraffic(
provider.subscriptionInfo.Upload + provider.subscriptionInfo.Download
)}
/${calcTraffic(provider.subscriptionInfo.Total)}`}</div>
}
>
{provider.subscriptionInfo && (
<div className="flex h-[32px] leading-[32px]">
<div className="ml-2">
{dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
</div>
<div className="select-none h-[32px] leading-[32px] text-default-500">
{dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
</div>
)}
</SettingItem>
)}
</>
</Fragment>
)
})}
</SettingCard>

View File

@ -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 (
<SettingCard>
<SettingItem title="规则集合" divider>
<Button
size="sm"
color="primary"
onPress={() => {
providers.forEach((provider, index) => {
onUpdate(provider.name, index)
})
}}
>
</Button>
</SettingItem>
{providers.map((provider, index) => {
return (
<Fragment key={provider.name}>
<SettingItem
title={provider.name}
actions={
<Chip className="ml-2" size="sm">
{provider.ruleCount}
</Chip>
}
>
{
<div className="flex select-none h-[32px] leading-[32px] text-default-500">
<div>{dayjs(provider.updatedAt).fromNow()}</div>
<Button
isIconOnly
className="ml-2"
size="sm"
onPress={() => {
onUpdate(provider.name, index)
}}
>
<IoMdRefresh className={`text-lg ${updating[index] ? 'animate-spin' : ''}`} />
</Button>
</div>
}
</SettingItem>
<SettingItem
title={<div className="text-default-500">{provider.format}</div>}
divider={index !== providers.length - 1}
>
<div className="select-none h-[32px] leading-[32px] text-default-500">
{provider.vehicleType}::{provider.behavior}
</div>
</SettingItem>
</Fragment>
)
})}
</SettingCard>
)
}
export default RuleProvider

View File

@ -26,6 +26,14 @@ export async function mihomoUpdateProxyProviders(name: string): Promise<void> {
return await window.electron.ipcRenderer.invoke('mihomoUpdateProxyProviders', name)
}
export async function mihomoRuleProviders(): Promise<IMihomoRuleProviders> {
return await window.electron.ipcRenderer.invoke('mihomoRuleProviders')
}
export async function mihomoUpdateRuleProviders(name: string): Promise<void> {
return await window.electron.ipcRenderer.invoke('mihomoUpdateRuleProviders', name)
}
export async function mihomoChangeProxy(group: string, proxy: string): Promise<IMihomoProxy> {
return await window.electron.ipcRenderer.invoke('mihomoChangeProxy', group, proxy)
}

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

@ -143,6 +143,20 @@ interface IMihomoProxies {
proxies: Record<string, IMihomoProxy | IMihomoGroup>
}
interface IMihomoRuleProviders {
providers: Record<string, IMihomoRuleProvider>
}
interface IMihomoRuleProvider {
behavior: string
format: string
name: string
ruleCount: number
type: string
updatedAt: string
vehicleType: string
}
interface IMihomoProxyProviders {
providers: Record<string, IMihomoProxyProvider>
}
@ -161,7 +175,7 @@ interface IMihomoProxyProvider {
subscriptionInfo?: ISubscriptionUserInfoUpper
expectedStatus: string
testUrl?: string
updateAt?: string
updatedAt?: string
vehicleType: string
}