Skip to content

Commit c27fc52

Browse files
committed
Update clash.ts
1 parent 94b3f6a commit c27fc52

File tree

1 file changed

+68
-25
lines changed

1 file changed

+68
-25
lines changed

src/clash.ts

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
import yaml from 'js-yaml'
22
import { defaultClashConfig } from "./variables"
3-
import { Config } from './interfaces';
3+
import { Config, ClashConfig } from './interfaces';
44

55
export function ToYamlSubscription(configList: Array<Config>): string {
66
let clash = defaultClashConfig
7-
clash.proxies = configList.map((conf: any) => (({merged, ...others}) => others)(conf))
8-
const groupedConfigs: any = configList.reduce((group: {[key: string]: any}, conf: any) => {
9-
if (!group[conf?.merged ? 'Worker' : 'Original']) {
10-
group[conf?.merged ? 'Worker' : 'Original'] = [];
7+
clash.proxies = configList.map((conf: Config) => {
8+
let {
9+
configType,
10+
type,
11+
remarks,
12+
address,
13+
tls,
14+
alpn,
15+
merged,
16+
...rest
17+
} = conf
18+
if (conf.type) {
19+
rest.network = conf.type
1120
}
12-
group[conf?.merged ? 'Worker' : 'Original'].push(conf);
13-
return group;
14-
}, {});
15-
let proxyTiers: any = []
16-
for (const worker in groupedConfigs) {
17-
proxyTiers[worker] = groupedConfigs[worker]
21+
let config: ClashConfig = {
22+
name: conf.remarks,
23+
server: conf.address,
24+
type: conf.configType,
25+
tls: conf.tls == "tls",
26+
cipher: "auto",
27+
...rest
28+
}
29+
return config
30+
})
31+
let proxyTiers: {
32+
"All": Array<string>,
33+
"Built-in": Array<string>,
34+
"Merged": Array<string>,
35+
"Original": Array<string>,
36+
} = {
37+
"All": [],
38+
"Built-in": [],
39+
"Merged": [],
40+
"Original": [],
1841
}
19-
let proxyGroups = [
42+
configList.forEach((conf: Config) => {
43+
const grp = ["vless-ws", "trojan-ws"].includes(conf.path.split("?")[0].split("/")[0]) ? "Built-in" : (conf?.merged ? 'Merged' : 'Original')
44+
proxyTiers[grp].push(conf.remarks)
45+
proxyTiers["All"].push(conf.remarks)
46+
});
47+
48+
clash['proxy-groups'] = [
2049
{
2150
name: "All",
2251
type: "select",
@@ -25,49 +54,63 @@ export function ToYamlSubscription(configList: Array<Config>): string {
2554
"All - Fallback",
2655
"All - LoadBalance(ch)",
2756
"All - LoadBalance(rr)",
28-
].concat(Object.keys(proxyTiers)),
57+
"Built-in - UrlTest",
58+
"Merged - UrlTest",
59+
"Original - UrlTest",
60+
].concat(proxyTiers["All"]),
2961
},
3062
{
3163
name: "All - UrlTest",
3264
type: "url-test",
3365
url: "http://clients3.google.com/generate_204",
3466
interval: 600,
35-
proxies: Object.keys(proxyTiers),
67+
proxies: proxyTiers["All"],
3668
},
3769
{
3870
name: "All - Fallback",
3971
type: "fallback",
4072
url: "http://clients3.google.com/generate_204",
4173
interval: 600,
42-
proxies: Object.keys(proxyTiers),
74+
proxies: proxyTiers["All"],
4375
},
4476
{
4577
name: "All - LoadBalance(ch)",
4678
type: "load-balance",
4779
strategy: "consistent-hashing",
4880
url: "http://clients3.google.com/generate_204",
4981
interval: 600,
50-
proxies: Object.keys(proxyTiers),
82+
proxies: proxyTiers["All"],
5183
},
5284
{
5385
name: "All - LoadBalance(rr)",
5486
type: "load-balance",
5587
strategy: "round-robin",
5688
url: "http://clients3.google.com/generate_204",
5789
interval: 600,
58-
proxies: Object.keys(proxyTiers),
90+
proxies: proxyTiers["All"],
5991
},
60-
]
61-
for (const tier in proxyTiers) {
62-
proxyGroups.push({
63-
name: tier,
92+
{
93+
name: "Built-in - UrlTest",
6494
type: "url-test",
6595
url: "http://clients3.google.com/generate_204",
6696
interval: 600,
67-
proxies: proxyTiers[tier],
68-
})
69-
}
97+
proxies: proxyTiers["Built-in"],
98+
},
99+
{
100+
name: "Merged - UrlTest",
101+
type: "url-test",
102+
url: "http://clients3.google.com/generate_204",
103+
interval: 600,
104+
proxies: proxyTiers["Merged"],
105+
},
106+
{
107+
name: "Original - UrlTest",
108+
type: "url-test",
109+
url: "http://clients3.google.com/generate_204",
110+
interval: 600,
111+
proxies: proxyTiers["Original"],
112+
},
113+
]
70114

71-
clash['proxy-groups'] = proxyGroups
72115
return yaml.dump(clash)
73116
}

0 commit comments

Comments
 (0)