-
Notifications
You must be signed in to change notification settings - Fork 297
/
meta_merge.py
244 lines (214 loc) · 9.19 KB
/
meta_merge.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
import yaml
import codecs
import json
import urllib.request
# 定义一个空列表用于存储合并后的代理配置
merged_proxies = []
# 获取clash文本中的内容
try:
with open('./urls/clash_urls.txt', 'r') as file:
urls = file.read().splitlines()
# 遍历每个网址
for url in urls:
try:
# 使用适当的方法从网址中获取内容,这里使用urllib库示例
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
# 解析YAML格式的内容
content = yaml.safe_load(data)
# 提取proxies部分并合并到merged_proxies中
proxies = content.get('proxies', [])
merged_proxies.extend(proxies)
except Exception as e:
print(f"Error processing URL {url}: {e}")
except Exception as e:
print(f"Error reading file: {e}")
# shadowtls singbox节点处理
# try:
# with open("./urls/shadowtls_urls.txt", "r") as file:
# urls = file.read().splitlines()
# # 遍历每个网址
# for index, url in enumerate(urls):
# try:
# # 使用适当的方法从网址中获取内容,这里使用urllib库示例
# response = urllib.request.urlopen(url)
# data = response.read().decode("utf-8")
# json_data = json.loads(data)
# # 提取所需字段
# method = json_data["outbounds"][0]["method"]
# password = json_data["outbounds"][0]["password"]
# server = json_data["outbounds"][1]["server"]
# server_port = json_data["outbounds"][1]["server_port"]
# server_name = json_data["outbounds"][1]["tls"]["server_name"]
# name = f"shadowtls{index}"
# # 创建当前网址的proxy字典
# proxy = {
# "name": name,
# "type": "ss",
# "server": server,
# "port": server_port,
# "cipher": method,
# "password": password,
# "plugin": "shadow-tls",
# "client-fingerprint": "chrome",
# "plugin-opts": {
# "host": server_name,
# "password": "",
# "version": 1
# }
# }
# # 将当前proxy字典添加到所有proxies列表中
# merged_proxies.append(proxy)
# except Exception as e:
# print(f"Error processing URL {url}: {e}")
# except Exception as e:
# print(f"Error reading file: {e}")
#歇斯底里节点 json处理
try:
with open("./urls/hysteria_urls.txt", "r") as file:
urls = file.read().splitlines()
# 遍历每个网址
for index, url in enumerate(urls):
try:
# 使用适当的方法从网址中获取内容,这里使用urllib库示例
response = urllib.request.urlopen(url)
data = response.read().decode("utf-8")
json_data = json.loads(data)
# 提取所需字段
auth = json_data["auth_str"]
server_ports = json_data["server"]
server_ports_slt = server_ports.split(":")
server = server_ports_slt[0]
ports = server_ports_slt[1]
ports_slt = ports.split(",")
server_port = int(ports_slt[0])
if len(ports_slt) > 1:
mport = ports_slt[1]
else:
mport = server_port
server_name = json_data["server_name"]
alpn = json_data["alpn"]
protocol = json_data["protocol"]
name = f"hysteria{index}"
# 创建当前网址的proxy字典
proxy = {
"name": name,
"type": "hysteria",
"server": server,
"port": server_port,
"ports": mport,
"auth_str": auth,
"up": 11,
"down": 55,
"fast-open": True,
"protocol": protocol,
"sni": server_name,
"skip-cert-verify": True,
"alpn": [alpn]
}
# 将当前proxy字典添加到所有proxies列表中
merged_proxies.append(proxy)
except Exception as e:
print(f"Error processing URL {url}: {e}")
except Exception as e:
print(f"Error reading file: {e}")
# xray json reality节点处理
try:
with open("./urls/reality_urls.txt", "r") as file:
urls = file.read().splitlines()
# 遍历每个网址
for index, url in enumerate(urls):
try:
# 使用适当的方法从网址中获取内容,这里使用urllib库示例
response = urllib.request.urlopen(url)
data = response.read().decode("utf-8")
json_data = json.loads(data)
# 提取所需字段
protocol = json_data["outbounds"][0]["protocol"]
server = json_data["outbounds"][0]["settings"]["vnext"][0]["address"]
port = json_data["outbounds"][0]["settings"]["vnext"][0]["port"]
uuid = json_data["outbounds"][0]["settings"]["vnext"][0]["users"][0]["id"]
istls = True
flow = json_data["outbounds"][0]["settings"]["vnext"][0]["users"][0]["flow"]
# 传输方式
network = json_data["outbounds"][0]["streamSettings"]["network"]
publicKey = json_data["outbounds"][0]["streamSettings"]["realitySettings"]["publicKey"]
shortId = json_data["outbounds"][0]["streamSettings"]["realitySettings"]["shortId"]
serverName = json_data["outbounds"][0]["streamSettings"]["realitySettings"]["serverName"]
fingerprint = json_data["outbounds"][0]["streamSettings"]["realitySettings"]["fingerprint"]
# udp转发
isudp = True
name = f"reality{index}"
# 根据network判断tcp
if network == "tcp":
proxy = {
"name": name,
"type": protocol,
"server": server,
"port": port,
"uuid": uuid,
"network": network,
"tls": istls,
"udp": isudp,
"flow": flow,
"client-fingerprint": fingerprint,
"servername": serverName,
"reality-opts":{
"public-key": publicKey,
"short-id": shortId}
}
# 根据network判断grpc
elif network == "grpc":
serviceName = json_data["outbounds"][0]["streamSettings"]["grpcSettings"]["serviceName"]
# 创建当前网址的proxy字典
proxy = {
"name": name,
"type": protocol,
"server": server,
"port": port,
"uuid": uuid,
"network": network,
"tls": istls,
"udp": isudp,
"flow": flow,
"client-fingerprint": fingerprint,
"servername": serverName,
"grpc-opts":{
"grpc-service-name": serviceName
},
"reality-opts":{
"public-key": publicKey,
"short-id": shortId}
}
else:
print(f"其他协议还未支持 URL {url}")
proxy={}
continue
# 将当前proxy字典添加到所有proxies列表中
merged_proxies.append(proxy)
except Exception as e:
print(f"Error processing URL {url}: {e}")
except Exception as e:
print(f"Error reading file: {e}")
# 读取普通的配置文件内容
with codecs.open('./templates/clash_template.yaml', 'r', encoding='utf-8') as file:
config_data = yaml.safe_load(file)
# 读取warp配置文件内容
with codecs.open('./templates/clash_warp_template.yaml', 'r', encoding='utf-8') as file:
config_warp_data = yaml.safe_load(file)
# 添加合并后的代理到proxies部分
config_data['proxies'].extend(merged_proxies)
config_warp_data['proxies'].extend(merged_proxies)
# 更新自动选择和节点选择的proxies的name部分
for group in config_data['proxy-groups']:
if group['name'] == '自动选择' or group['name'] == '节点选择':
group['proxies'].extend(proxy['name'] for proxy in merged_proxies)
for group in config_warp_data['proxy-groups']:
if group['name'] == '自动选择' or group['name'] == '手动选择' or group['name'] == '负载均衡':
group['proxies'].extend(proxy['name'] for proxy in merged_proxies)
# 将更新后的数据写入到一个YAML文件中,并指定编码格式为UTF-8
with codecs.open('./sub/merged_proxies.yaml', 'w', encoding='utf-8') as file:
yaml.dump(config_data, file, sort_keys=False, allow_unicode=True)
with codecs.open('./sub/merged_warp_proxies.yaml', 'w', encoding='utf-8') as file:
yaml.dump(config_warp_data, file, sort_keys=False, allow_unicode=True)
print("聚合成狗")