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> => { export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
const instance = await getAxios() const instance = await getAxios()
return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => { return (await instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch(() => {

View File

@ -6,7 +6,6 @@ export function initProfileUpdater(): void {
const { items } = getProfileConfig() const { items } = getProfileConfig()
for (const item of items) { for (const item of items) {
console.log(item.type, item.interval)
if (item.type === 'remote' && item.interval) { if (item.type === 'remote' && item.interval) {
addProfileItem(getProfileItem(item.id)) addProfileItem(getProfileItem(item.id))
intervalPool[item.id] = setInterval( intervalPool[item.id] = setInterval(

View File

@ -6,8 +6,10 @@ import {
mihomoProxies, mihomoProxies,
mihomoProxyDelay, mihomoProxyDelay,
mihomoProxyProviders, mihomoProxyProviders,
mihomoRuleProviders,
mihomoRules, mihomoRules,
mihomoUpdateProxyProviders, mihomoUpdateProxyProviders,
mihomoUpdateRuleProviders,
mihomoUpgradeGeo, mihomoUpgradeGeo,
mihomoVersion, mihomoVersion,
patchMihomoConfig, patchMihomoConfig,
@ -48,6 +50,8 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoProxies', mihomoProxies) ipcMain.handle('mihomoProxies', mihomoProxies)
ipcMain.handle('mihomoProxyProviders', () => mihomoProxyProviders()) ipcMain.handle('mihomoProxyProviders', () => mihomoProxyProviders())
ipcMain.handle('mihomoUpdateProxyProviders', (_e, name) => mihomoUpdateProxyProviders(name)) 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('mihomoChangeProxy', (_e, group, proxy) => mihomoChangeProxy(group, proxy))
ipcMain.handle('mihomoUpgradeGeo', mihomoUpgradeGeo) ipcMain.handle('mihomoUpgradeGeo', mihomoUpgradeGeo)
ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url)) ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url))

View File

@ -1,9 +1,9 @@
import { mihomoProxyProviders, mihomoUpdateProxyProviders } from '@renderer/utils/ipc' import { mihomoProxyProviders, mihomoUpdateProxyProviders } from '@renderer/utils/ipc'
import { useMemo, useState } from 'react' import { Fragment, useMemo, useState } from 'react'
import useSWR from 'swr' import useSWR from 'swr'
import SettingCard from '../base/base-setting-card' import SettingCard from '../base/base-setting-card'
import SettingItem from '../base/base-setting-item' 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 { IoMdRefresh } from 'react-icons/io'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { calcTraffic } from '@renderer/utils/calc' import { calcTraffic } from '@renderer/utils/calc'
@ -15,40 +15,61 @@ const ProxyProvider: React.FC = () => {
return Object.keys(data.providers) return Object.keys(data.providers)
.map((key) => data.providers[key]) .map((key) => data.providers[key])
.filter((provider) => { .filter((provider) => {
console.log(provider)
return 'subscriptionInfo' in provider return 'subscriptionInfo' in provider
}) })
}, [data]) }, [data])
const [updating, setUpdating] = useState(Array(providers.length).fill(false)) 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 ( return (
<SettingCard> <SettingCard>
<SettingItem title="代理集合" divider>
<Button
size="sm"
color="primary"
onPress={() => {
providers.forEach((provider, index) => {
onUpdate(provider.name, index)
})
}}
>
</Button>
</SettingItem>
{providers.map((provider, index) => { {providers.map((provider, index) => {
return ( return (
<> <Fragment key={provider.name}>
<SettingItem <SettingItem
title={provider.name} 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} divider={!provider.subscriptionInfo && index !== providers.length - 1}
> >
{ {
<div className="flex h-[32px] leading-[32px]"> <div className="flex select-none h-[32px] leading-[32px] text-default-500">
<div>{dayjs(provider.updateAt).fromNow()}</div> <div>{dayjs(provider.updatedAt).fromNow()}</div>
<Button <Button
isIconOnly isIconOnly
className="ml-2" className="ml-2"
size="sm" size="sm"
onPress={() => { onPress={() => {
setUpdating((prev) => { onUpdate(provider.name, index)
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' : ''}`} /> <IoMdRefresh className={`text-lg ${updating[index] ? 'animate-spin' : ''}`} />
@ -59,22 +80,21 @@ const ProxyProvider: React.FC = () => {
{provider.subscriptionInfo && ( {provider.subscriptionInfo && (
<SettingItem <SettingItem
divider={index !== providers.length - 1} divider={index !== providers.length - 1}
title={`${calcTraffic( title={
provider.subscriptionInfo.Upload + provider.subscriptionInfo.Download <div className="text-default-500">{`${calcTraffic(
)} provider.subscriptionInfo.Upload + provider.subscriptionInfo.Download
/${calcTraffic(provider.subscriptionInfo.Total)}`} )}
key={provider.name} /${calcTraffic(provider.subscriptionInfo.Total)}`}</div>
}
> >
{provider.subscriptionInfo && ( {provider.subscriptionInfo && (
<div className="flex h-[32px] leading-[32px]"> <div className="select-none h-[32px] leading-[32px] text-default-500">
<div className="ml-2"> {dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
{dayjs(provider.subscriptionInfo.Expire).format('YYYY-MM-DD')}
</div>
</div> </div>
)} )}
</SettingItem> </SettingItem>
)} )}
</> </Fragment>
) )
})} })}
</SettingCard> </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 = () => { 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 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) 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> { export async function mihomoChangeProxy(group: string, proxy: string): Promise<IMihomoProxy> {
return await window.electron.ipcRenderer.invoke('mihomoChangeProxy', group, proxy) 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> 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 { interface IMihomoProxyProviders {
providers: Record<string, IMihomoProxyProvider> providers: Record<string, IMihomoProxyProvider>
} }
@ -161,7 +175,7 @@ interface IMihomoProxyProvider {
subscriptionInfo?: ISubscriptionUserInfoUpper subscriptionInfo?: ISubscriptionUserInfoUpper
expectedStatus: string expectedStatus: string
testUrl?: string testUrl?: string
updateAt?: string updatedAt?: string
vehicleType: string vehicleType: string
} }