1
1
import yaml from 'js-yaml'
2
2
import { defaultClashConfig } from "./variables"
3
- import { Config } from './interfaces' ;
3
+ import { Config , ClashConfig } from './interfaces' ;
4
4
5
5
export function ToYamlSubscription ( configList : Array < Config > ) : string {
6
6
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
11
20
}
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" : [ ] ,
18
41
}
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' ] = [
20
49
{
21
50
name : "All" ,
22
51
type : "select" ,
@@ -25,49 +54,63 @@ export function ToYamlSubscription(configList: Array<Config>): string {
25
54
"All - Fallback" ,
26
55
"All - LoadBalance(ch)" ,
27
56
"All - LoadBalance(rr)" ,
28
- ] . concat ( Object . keys ( proxyTiers ) ) ,
57
+ "Built-in - UrlTest" ,
58
+ "Merged - UrlTest" ,
59
+ "Original - UrlTest" ,
60
+ ] . concat ( proxyTiers [ "All" ] ) ,
29
61
} ,
30
62
{
31
63
name : "All - UrlTest" ,
32
64
type : "url-test" ,
33
65
url : "http://clients3.google.com/generate_204" ,
34
66
interval : 600 ,
35
- proxies : Object . keys ( proxyTiers ) ,
67
+ proxies : proxyTiers [ "All" ] ,
36
68
} ,
37
69
{
38
70
name : "All - Fallback" ,
39
71
type : "fallback" ,
40
72
url : "http://clients3.google.com/generate_204" ,
41
73
interval : 600 ,
42
- proxies : Object . keys ( proxyTiers ) ,
74
+ proxies : proxyTiers [ "All" ] ,
43
75
} ,
44
76
{
45
77
name : "All - LoadBalance(ch)" ,
46
78
type : "load-balance" ,
47
79
strategy : "consistent-hashing" ,
48
80
url : "http://clients3.google.com/generate_204" ,
49
81
interval : 600 ,
50
- proxies : Object . keys ( proxyTiers ) ,
82
+ proxies : proxyTiers [ "All" ] ,
51
83
} ,
52
84
{
53
85
name : "All - LoadBalance(rr)" ,
54
86
type : "load-balance" ,
55
87
strategy : "round-robin" ,
56
88
url : "http://clients3.google.com/generate_204" ,
57
89
interval : 600 ,
58
- proxies : Object . keys ( proxyTiers ) ,
90
+ proxies : proxyTiers [ "All" ] ,
59
91
} ,
60
- ]
61
- for ( const tier in proxyTiers ) {
62
- proxyGroups . push ( {
63
- name : tier ,
92
+ {
93
+ name : "Built-in - UrlTest" ,
64
94
type : "url-test" ,
65
95
url : "http://clients3.google.com/generate_204" ,
66
96
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
+ ]
70
114
71
- clash [ 'proxy-groups' ] = proxyGroups
72
115
return yaml . dump ( clash )
73
116
}
0 commit comments