From c27fc5264fae4e8bc831d4514667d5117939c283 Mon Sep 17 00:00:00 2001 From: Vahid Farid Date: Mon, 3 Jun 2024 19:21:17 +0330 Subject: [PATCH] Update clash.ts --- src/clash.ts | 93 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/src/clash.ts b/src/clash.ts index bd3ccf87..942586d4 100644 --- a/src/clash.ts +++ b/src/clash.ts @@ -1,22 +1,51 @@ import yaml from 'js-yaml' import { defaultClashConfig } from "./variables" -import { Config } from './interfaces'; +import { Config, ClashConfig } from './interfaces'; export function ToYamlSubscription(configList: Array): string { let clash = defaultClashConfig - clash.proxies = configList.map((conf: any) => (({merged, ...others}) => others)(conf)) - const groupedConfigs: any = configList.reduce((group: {[key: string]: any}, conf: any) => { - if (!group[conf?.merged ? 'Worker' : 'Original']) { - group[conf?.merged ? 'Worker' : 'Original'] = []; + clash.proxies = configList.map((conf: Config) => { + let { + configType, + type, + remarks, + address, + tls, + alpn, + merged, + ...rest + } = conf + if (conf.type) { + rest.network = conf.type } - group[conf?.merged ? 'Worker' : 'Original'].push(conf); - return group; - }, {}); - let proxyTiers: any = [] - for (const worker in groupedConfigs) { - proxyTiers[worker] = groupedConfigs[worker] + let config: ClashConfig = { + name: conf.remarks, + server: conf.address, + type: conf.configType, + tls: conf.tls == "tls", + cipher: "auto", + ...rest + } + return config + }) + let proxyTiers: { + "All": Array, + "Built-in": Array, + "Merged": Array, + "Original": Array, + } = { + "All": [], + "Built-in": [], + "Merged": [], + "Original": [], } - let proxyGroups = [ + configList.forEach((conf: Config) => { + const grp = ["vless-ws", "trojan-ws"].includes(conf.path.split("?")[0].split("/")[0]) ? "Built-in" : (conf?.merged ? 'Merged' : 'Original') + proxyTiers[grp].push(conf.remarks) + proxyTiers["All"].push(conf.remarks) + }); + + clash['proxy-groups'] = [ { name: "All", type: "select", @@ -25,21 +54,24 @@ export function ToYamlSubscription(configList: Array): string { "All - Fallback", "All - LoadBalance(ch)", "All - LoadBalance(rr)", - ].concat(Object.keys(proxyTiers)), + "Built-in - UrlTest", + "Merged - UrlTest", + "Original - UrlTest", + ].concat(proxyTiers["All"]), }, { name: "All - UrlTest", type: "url-test", url: "http://clients3.google.com/generate_204", interval: 600, - proxies: Object.keys(proxyTiers), + proxies: proxyTiers["All"], }, { name: "All - Fallback", type: "fallback", url: "http://clients3.google.com/generate_204", interval: 600, - proxies: Object.keys(proxyTiers), + proxies: proxyTiers["All"], }, { name: "All - LoadBalance(ch)", @@ -47,7 +79,7 @@ export function ToYamlSubscription(configList: Array): string { strategy: "consistent-hashing", url: "http://clients3.google.com/generate_204", interval: 600, - proxies: Object.keys(proxyTiers), + proxies: proxyTiers["All"], }, { name: "All - LoadBalance(rr)", @@ -55,19 +87,30 @@ export function ToYamlSubscription(configList: Array): string { strategy: "round-robin", url: "http://clients3.google.com/generate_204", interval: 600, - proxies: Object.keys(proxyTiers), + proxies: proxyTiers["All"], }, - ] - for (const tier in proxyTiers) { - proxyGroups.push({ - name: tier, + { + name: "Built-in - UrlTest", type: "url-test", url: "http://clients3.google.com/generate_204", interval: 600, - proxies: proxyTiers[tier], - }) - } + proxies: proxyTiers["Built-in"], + }, + { + name: "Merged - UrlTest", + type: "url-test", + url: "http://clients3.google.com/generate_204", + interval: 600, + proxies: proxyTiers["Merged"], + }, + { + name: "Original - UrlTest", + type: "url-test", + url: "http://clients3.google.com/generate_204", + interval: 600, + proxies: proxyTiers["Original"], + }, + ] - clash['proxy-groups'] = proxyGroups return yaml.dump(clash) } \ No newline at end of file