Skip to content

Commit

Permalink
chore: add ClashConfig.rule-providers? define
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Oct 17, 2023
1 parent 8ab9cf5 commit c6d1078
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
24 changes: 22 additions & 2 deletions packages/ui/src/define/ClashConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +22,6 @@ export interface ClashConfig {
'interface-name': string
'hosts': null
'dns': DNS
'rules': string[]
}

export interface DNS {
Expand Down Expand Up @@ -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
}
24 changes: 19 additions & 5 deletions packages/ui/src/util/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ export default async function genConfig({ forceUpdate = false }: { forceUpdate?:
let config: Partial<ClashConfig> = {}

// 值为 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<ClashConfig>) => {
const arrayValuedKeys: ClashConfigKeysWithArrayValue[] = ['rules', 'proxies', 'proxy-groups']
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c6d1078

Please sign in to comment.