From c6d1078a3a39d5076c9a768827026894a0b2d6c6 Mon Sep 17 00:00:00 2001 From: Tao Date: Tue, 17 Oct 2023 19:55:10 +0800 Subject: [PATCH] chore: add ClashConfig.rule-providers? define --- packages/ui/src/define/ClashConfig.ts | 24 ++++++++++++++++++++++-- packages/ui/src/util/gen.ts | 24 +++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/ui/src/define/ClashConfig.ts b/packages/ui/src/define/ClashConfig.ts index 819d38b..25f2edb 100644 --- a/packages/ui/src/define/ClashConfig.ts +++ b/packages/ui/src/define/ClashConfig.ts @@ -5,7 +5,11 @@ import { ClashProxyItem } from '$clash-utils' export interface ClashConfig { 'proxies': ClashProxyItem[] 'proxy-groups': ProxyGroup[] - 'proxy-providers': ProxyProviders | undefined + 'proxy-providers'?: ProxyProviders + + 'rules': string[] + 'rule-providers'?: RuleProviders + 'port': number 'socks-port': number 'mixed-port': number @@ -18,7 +22,6 @@ export interface ClashConfig { 'interface-name': string 'hosts': null 'dns': DNS - 'rules': string[] } export interface DNS { @@ -68,3 +71,20 @@ export interface ProxyProviderHealthCheck { enable: boolean url: string } + +export interface RuleProviders { + [name: string]: RuleProvider +} + +/** + * @see https://dreamacro.github.io/clash/premium/rule-providers.html + * @see https://wiki.metacubex.one/config/rules/rule-provider/ + */ +export interface RuleProvider { + behavior: 'domain' | 'ipcidr' | 'classical' + type: 'file' | 'http' + path: string + url?: string + format?: 'yaml' | 'text' + interval?: number +} diff --git a/packages/ui/src/util/gen.ts b/packages/ui/src/util/gen.ts index a27d8a3..80489de 100644 --- a/packages/ui/src/util/gen.ts +++ b/packages/ui/src/util/gen.ts @@ -61,9 +61,12 @@ export default async function genConfig({ forceUpdate = false }: { forceUpdate?: let config: Partial = {} // 值为 array 的 key 集合 - type ClashConfigKeysWithArrayValue = { - [k in keyof ClashConfig]: ClashConfig[k] extends any[] ? k : never - }[keyof ClashConfig] + type ClashConfigKeysWithArrayValue = Exclude< + { + [k in keyof ClashConfig]: ClashConfig[k] extends any[] ? k : never + }[keyof ClashConfig], + undefined + > const updateConfig = (partial: Partial) => { const arrayValuedKeys: ClashConfigKeysWithArrayValue[] = ['rules', 'proxies', 'proxy-groups'] @@ -279,11 +282,22 @@ export default async function genConfig({ forceUpdate = false }: { forceUpdate?: /* #endregion */ - // final rules - // 未匹配使用 DIRECT + /* #region premium only feature */ + if (!Object.keys(config['proxy-providers'] || {}).length) { + delete config['proxy-providers'] + } + + if (!Object.keys(config['rule-providers'] || {}).length) { + delete config['rule-providers'] + } + /* #endregion */ + + /* #region final rules */ if (!config.rules?.at(-1)?.startsWith('MATCH,')) { + // 未匹配使用 DIRECT config.rules?.push('MATCH,DIRECT') } + /* #endregion */ const configYaml = YAML.dump(config) const file = getConfigFile(name, clashMeta)