forked from SubConv/SubConv-4-ZJU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.py
333 lines (307 loc) · 12.1 KB
/
pack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
This module is to general a complete config for Clash
"""
from modules import parse
from modules import head
import re
import config
import yaml
import cache
async def pack(url: list, urlstandalone: list, urlstandby:list, urlstandbystandalone: list, content: str, interval, domain, short):
regionDict, total, providerProxyNames = await parse.mkList(content, urlstandalone) # regions available and corresponding group name
result = {}
# create a snippet containing region groups
regionGroups = []
for i in total.values():
regionGroups.append(i[1])
if short is None:
# head of config
result.update(head.HEAD)
# dns
result.update(head.DNS)
# proxies
proxies = {
"proxies": []
}
proxiesName = []
proxiesStandbyName = []
if urlstandalone or urlstandbystandalone:
if urlstandalone:
for i in urlstandalone:
proxies["proxies"].append(
i
)
proxiesName.append(i["name"])
proxiesStandbyName.append(i["name"])
if urlstandbystandalone:
for i in urlstandbystandalone:
proxies["proxies"].append(
i
)
proxiesStandbyName.append(i["name"])
if len(proxies["proxies"]) == 0:
proxies = None
if len(proxiesName) == 0:
proxiesName = None
if len(proxiesStandbyName) == 0:
proxiesStandbyName = None
if proxies:
result.update(proxies)
# proxy providers
providers = {
"proxy-providers": {}
}
if url or urlstandby:
if url:
for u in range(len(url)):
providers["proxy-providers"].update({
"subscription{}".format(u): {
"type": "http",
"url": url[u],
"interval": int(interval),
"path": "./sub/subscription{}.yaml".format(u),
"health-check": {
"enable": True,
"interval": 60,
# "lazy": True,
"url": config.test_url
}
}
})
if urlstandby:
for u in range(len(urlstandby)):
providers["proxy-providers"].update({
"subscription{}".format("sub"+str(u)): {
"type": "http",
"url": urlstandby[u],
"interval": int(interval),
"path": "./sub/subscription{}.yaml".format("sub"+str(u)),
"health-check": {
"enable": True,
"interval": 60,
# "lazy": True,
"url": config.test_url
}
}
})
if len(providers["proxy-providers"]) == 0:
providers = None
if providers:
result.update(providers)
# result += head.PROXY_GROUP_HEAD
proxyGroups = {
"proxy-groups": []
}
# add proxy select
proxySelect = {
"name": "🚀 节点选择",
"type": "select",
"proxies": []
}
for group in config.custom_proxy_group:
if group.get("rule") == False:
proxySelect["proxies"].append(group["name"])
proxySelect["proxies"].extend(regionGroups)
proxySelect["proxies"].append("DIRECT")
proxyGroups["proxy-groups"].append(proxySelect)
# generate subscriptions and standby subscriptions list
subscriptions = []
if url:
for u in range(len(url)):
subscriptions.append("subscription{}".format(u))
standby = subscriptions.copy()
if urlstandby:
for u in range(len(urlstandby)):
standby.append("subscriptionsub{}".format(u))
if len(subscriptions) == 0:
subscriptions = None
if len(standby) == 0:
standby = None
# add proxy groups
for group in config.custom_proxy_group:
type = group["type"]
region = group.get("region")
regex = group.get("regex")
rule = group.get("rule")
if rule is None:
rule = True
if type == "select" and rule:
prior = group["prior"]
if prior == "DIRECT":
proxyGroups["proxy-groups"].append({
"name": group["name"],
"type": "select",
"proxies": [
"DIRECT",
"REJECT",
"🚀 节点选择",
*regionGroups,
*[_group["name"] for _group in config.custom_proxy_group if _group.get("rule") == False]
]
})
elif prior == "REJECT":
proxyGroups["proxy-groups"].append({
"name": group["name"],
"type": "select",
"proxies": [
"REJECT",
"DIRECT",
"🚀 节点选择",
*regionGroups,
*[_group["name"] for _group in config.custom_proxy_group if _group.get("rule") == False]
]
})
else:
proxyGroups["proxy-groups"].append({
"name": group["name"],
"type": "select",
"proxies": [
"🚀 节点选择",
*regionGroups,
*[_group["name"] for _group in config.custom_proxy_group if _group.get("rule") == False],
"DIRECT",
"REJECT"
]
})
elif type == "load-balance" or type == "select" or type == "fallback" or type == "url-test":
# init
proxyGroup = {
"name": group["name"],
"type": type
}
# add proxies
if regex is not None or region is not None:
if regex is not None:
tmp = [regex]
else:
tmp = []
for i in region:
if i in total:
tmp.append(total[i][0])
if len(tmp) > 0:
providerProxies = []
proxyGroupProxies = []
proxyGroup["filter"] = "|".join(tmp)
# check if the proxy is in the subscription match the regex
# check if the standalone proxy match the regex
if group.get("manual"):
if standby:
for p in standby:
if re.search(
proxyGroup["filter"],
p,
re.I
) is not None:
providerProxies.append(p)
break
if len(providerProxies) > 0:
proxyGroup["use"] = standby
if proxiesStandbyName:
for p in proxiesStandbyName:
if re.search(
proxyGroup["filter"],
p,
re.I
) is not None:
proxyGroupProxies.append(p)
if len(proxyGroupProxies) > 0:
proxyGroup["proxies"] = proxyGroupProxies
else:
if subscriptions:
for p in providerProxyNames:
if re.search(
proxyGroup["filter"],
p,
re.I
) is not None:
providerProxies.append(p)
break
if len(providerProxies) > 0:
proxyGroup["use"] = subscriptions
if proxiesName:
for p in proxiesName:
if re.search(
proxyGroup["filter"],
p,
re.I
) is not None:
proxyGroupProxies.append(p)
if len(proxyGroupProxies) > 0:
proxyGroup["proxies"] = proxyGroupProxies
# if no proxy match the regex, remove the name in the first group
if len(providerProxies) + len(proxyGroupProxies) == 0:
proxyGroups["proxy-groups"][0]["proxies"].remove(group["name"])
proxyGroup = None
else:
proxyGroups["proxy-groups"][0]["proxies"].remove(group["name"])
proxyGroup = None
if proxyGroup is not None:
if type == "load-balance":
proxyGroup["strategy"] = "consistent-hashing"
proxyGroup["url"] = config.test_url
proxyGroup["interval"] = 60
proxyGroup["tolerance"] = 50
elif type == "fallback":
proxyGroup["url"] = config.test_url
proxyGroup["interval"] = 60
proxyGroup["tolerance"] = 50
elif type == "url-test":
proxyGroup["url"] = config.test_url
proxyGroup["interval"] = 60
proxyGroup["tolerance"] = 50
else:
if group.get("manual"):
if standby:
proxyGroup["use"] = standby
if proxiesStandbyName:
proxyGroup["proxies"] = proxiesStandbyName
else:
if subscriptions:
proxyGroup["use"] = subscriptions
if proxiesName:
proxyGroup["proxies"] = proxiesName
if proxyGroup is not None:
proxyGroups["proxy-groups"].append(proxyGroup)
# add region groups
for i in total:
urlTest = {
"name": total[i][1],
"type": "url-test",
"url": config.test_url,
"interval": 60,
"tolerance": 50,
"filter": total[i][0]
}
if subscriptions:
urlTest["use"] = subscriptions
if proxiesName:
urlTestProxies = []
for p in proxiesName:
if re.search(
total[i][0],
p,
re.I
) is not None:
urlTestProxies.append(p)
if len(urlTestProxies) > 0:
urlTest["proxies"] = urlTestProxies
else:
urlTestProxies = None
proxyGroups["proxy-groups"].append(urlTest)
# remove proxies that do not exist in any proxy group
proxyGroupAndProxyList = (["DIRECT", "REJECT"])
proxyGroupAndProxyList.extend([i["name"] for i in proxyGroups["proxy-groups"]])
if proxiesStandbyName is not None:
proxyGroupAndProxyList.extend(proxiesStandbyName)
for proxygroup in proxyGroups["proxy-groups"]:
if "proxies" not in proxygroup:
continue
for proxy in proxygroup["proxies"]:
if proxy not in proxyGroupAndProxyList:
proxygroup["proxies"].remove(proxy)
result.update(proxyGroups)
# rules
yaml.SafeDumper.ignore_aliases = lambda *args : True
result = yaml.safe_dump(result, allow_unicode=True, sort_keys=False)
result += ("rules:\n - DOMAIN,{},DIRECT\n".format(domain) + cache.cache)
return result