forked from qwer123369/convert2clash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert2clash.py
310 lines (294 loc) · 11 KB
/
convert2clash.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
import requests
import yaml
import base64
import json
import datetime
import sys
import urllib.parse
import re
def log(msg):
time = datetime.datetime.now()
print('[' + time.strftime('%Y.%m.%d-%H:%M:%S') + ']:' + msg)
# Save to files
def save_to_file(file_name, content):
with open(file_name, 'wb') as f:
f.write(content)
# base64 decode for URL
def safe_decode(s):
num = len(s) % 4
if num:
s += '=' * (4 - num)
return base64.urlsafe_b64decode(s)
# Decode for vmess
def decode_v2ray_node(nodes):
proxy_list = []
for node in nodes:
decode_proxy = node.decode('utf-8')[8:]
decode_proxy = f"{decode_proxy}{'='*(3-len(decode_proxy)%3)}"
proxy_str = base64.b64decode(decode_proxy).decode('utf-8')
proxy_dict = json.loads(proxy_str)
proxy_list.append(proxy_dict)
return proxy_list
# Decode for SS
def decode_ss_node(nodes):
proxy_list = []
for node in nodes:
decode_proxy = node.decode('utf-8')[5:]
if not decode_proxy or decode_proxy.isspace():
log('[Warning] Empty Node, Ignore')
continue
info = dict()
param = decode_proxy
if param.find('#') > -1:
remark = urllib.parse.unquote(param[param.find('#')+1:])
info['name'] = remark
param = param[:param.find('#')]
if param.find('/?') > -1:
plugin = urllib.parse.unquote(param[param.find('/?') + 2:])
param = param[:param.find('/?')]
for p in plugin.split(';'):
key_value = p.split('=')
info[key_value[0]] = key_value[1]
if param.find('@') > -1:
matcher = re.match(r'(.*?)@(.*):(.*)', param)
if matcher:
param = matcher.group(1)
info['server'] = matcher.group(2)
info['port'] = matcher.group(3)
else:
continue
matcher = re.match(r'(.*?):(.*)', safe_decode(param).decode('utf-8'))
if matcher:
info['method'] = matcher.group(1)
info['password'] = matcher.group(2)
else:
continue
else:
matcher = re.match(r'(.*?):(.*)@(.*):(.*)', safe_decode(param).decode('utf-8'))
if matcher:
info['method'] = matcher.group(1)
info['password'] = matcher.group(2)
info['server'] = matcher.group(3)
info['port'] = matcher.group(4)
else:
continue
proxy_list.append(info)
return proxy_list
# Decode for SSR
def decode_ssr_node(nodes):
proxy_list = []
for node in nodes:
decode_proxy = node.decode('utf-8')[6:]
proxy_str = safe_decode(decode_proxy).decode('utf-8')
parts = proxy_str.split(':')
if len(parts) != 6:
print('Fail to analyse the SSR node, link:{}'.format(node))
continue
info = {
'server': parts[0],
'port': parts[1],
'protocol': parts[2],
'method': parts[3],
'obfs': parts[4]
}
password_params = parts[5].split('/?')
info['password'] = safe_decode(password_params[0]).decode('utf-8')
params = password_params[1].split('&')
for p in params:
key_value = p.split('=')
info[key_value[0]] = safe_decode(key_value[1]).decode('utf-8')
proxy_list.append(info)
return proxy_list
# Obtain the information in subscription link
def get_proxies(urls):
url_list = urls.split(';')
headers = {
'User-Agent': 'Rule2Clash'
}
proxy_list = {
'proxy_list': [],
'proxy_names': []
}
# Access to the subscription link
for url in url_list:
response = requests.get(url, headers=headers,timeout=5000).text
try:
raw = base64.b64decode(response)
except Exception as r:
log('[Warning] base64 decode failed {}'.format(r))
log('[Info] Get Clash Node Information')
yml = yaml.load(response, Loader=yaml.FullLoader)
nodes_list = []
for node in yml.get('proxies'):
node['name'] = node['name'].strip() if node.get('name') else None
if node.get('protocolparam'):
node['protocol-param'] = node['protocolparam']
del node['protocolparam']
if node.get('obfsparam'):
node['obfs-param'] = node['obfsparam']
del node['obfsparam']
node['udp'] = True
nodes_list.append(node)
node_names = [node.get('name') for node in nodes_list]
log('[Info] Clash Node Num: {}'.format(len(node_names)))
proxy_list['proxy_list'].extend(nodes_list)
proxy_list['proxy_names'].extend(node_names)
continue
nodes_list = raw.splitlines()
clash_node = []
if nodes_list[0].startswith(b'vmess://'):
decode_proxy = decode_v2ray_node(nodes_list)
clash_node = v2ray_to_clash(decode_proxy)
elif nodes_list[0].startswith(b'ss://'):
decode_proxy = decode_ss_node(nodes_list)
clash_node = ss_to_clash(decode_proxy)
elif nodes_list[0].startswith(b'ssr://'):
decode_proxy = decode_ssr_node(nodes_list)
clash_node = ssr_to_clash(decode_proxy)
else:
pass
proxy_list['proxy_list'].extend(clash_node['proxy_list'])
proxy_list['proxy_names'].extend(clash_node['proxy_names'])
log('[Info] Total Nodes Number: {}'.format(len(proxy_list['proxy_names'])))
return proxy_list
# Convert v2ray to clash
def v2ray_to_clash(arr):
log('[Info] Converting v2ray to clash')
proxies = {
'proxy_list': [],
'proxy_names': []
}
for item in arr:
if item.get('ps') is None and item.get('add') is None and item.get('port') is None \
and item.get('id') is None and item.get('aid') is None:
continue
obj = {
'name': item.get('ps').strip() if item.get('ps') else None,
'type': 'vmess',
'server': item.get('add'),
'port': int(item.get('port')),
'uuid': item.get('id'),
'alterId': item.get('aid'),
'cipher': 'auto',
'udp': True,
# 'network': item['net'] if item['net'] and item['net'] != 'tcp' else None,
'network': item.get('net'),
'tls': True if item.get('tls') == 'tls' else None,
'skip-cert-verify': True if item.get('tls') == 'tls' else None,
'servername': item.get('sni') if item.get('tls') == 'tls' else None,
'ws-path': item.get('path'),
'ws-headers': {'Host': item.get('host')} if item.get('host') else None
}
for key in list(obj.keys()):
if obj.get(key) is None:
del obj[key]
if obj.get('alterId') is not None:
proxies['proxy_list'].append(obj)
proxies['proxy_names'].append(obj['name'])
log('[Info] Total number of available v2ray nodes: {}'.format(len(proxies['proxy_names'])))
return proxies
# Convert ss to clash
def ss_to_clash(arr):
log('[Info] Converting ss to clash')
proxies = {
'proxy_list': [],
'proxy_names': []
}
for item in arr:
obj = {
'name': item.get('name').strip() if item.get('name') else None,
'type': 'ss',
'server': item.get('server'),
'port': int(item.get('port')),
'cipher': item.get('method'),
'password': item.get('password'),
'plugin': 'obfs' if item.get('plugin') and item.get('plugin').startswith('obfs') else None,
'plugin-opts': {} if item.get('plugin') else None
}
if item.get('obfs'):
obj['plugin-opts']['mode'] = item.get('obfs')
if item.get('obfs-host'):
obj['plugin-opts']['host'] = item.get('obfs-host')
for key in list(obj.keys()):
if obj.get(key) is None:
del obj[key]
proxies['proxy_list'].append(obj)
proxies['proxy_names'].append(obj['name'])
log('[Info] Total number of available ss nodes: {}'.format(len(proxies['proxy_names'])))
return proxies
# Convert ssr to clash
def ssr_to_clash(arr):
log('[Info] Converting ssr to clash')
proxies = {
'proxy_list': [],
'proxy_names': []
}
for item in arr:
obj = {
'name': item.get('remarks').strip() if item.get('remarks') else None,
'type': 'ssr',
'server': item.get('server'),
'port': int(item.get('port')),
'cipher': item.get('method'),
'password': item.get('password'),
'obfs': item.get('obfs'),
'protocol': item.get('protocol'),
'obfs-param': item.get('obfsparam'),
'protocol-param': item.get('protoparam'),
'udp': True
}
for key in list(obj.keys()):
if obj.get(key) is None:
del obj[key]
if obj.get('name'):
proxies['proxy_list'].append(obj)
proxies['proxy_names'].append(obj['name'])
log('[Info] Total number of available ssr nodes: {}'.format(len(proxies['proxy_names'])))
return proxies
# Save all servers with same mark
def unique_name(data):
name = data.get('proxy_list')
names = data.get('proxy_names')
n = 0
for i in name:
i['name'] = names[n]= f"{i['name']}_{n}"
n += 1
data['proxy_list'] = name
data['proxy_names'] = names
return data
# Loading local configuration file
def load_local_config(path):
try:
f = open(path, 'r', encoding="utf-8")
local_config = yaml.load(f.read(), Loader=yaml.FullLoader)
f.close()
return local_config
except FileNotFoundError:
log('[Warning] Fail to loading configuration')
sys.exit()
# Add proxy into configuration
def add_proxies_to_model(data, model):
model['proxies'] = data.get('proxy_list')
for group in model.get('proxy-groups'):
if group.get('proxies') is None:
group['proxies'] = data.get('proxy_names')
else:
group['proxies'].extend(data.get('proxy_names'))
return model
# Save updated configuration file
def save_config(path, data):
config = yaml.dump(data, sort_keys=False, default_flow_style=False, encoding='utf-8', allow_unicode=True)
save_to_file(path, config)
log('[Info] Number of Nodes Updated: {}'.format(len(data['proxies'])))
if __name__ == '__main__':
# use ';' to separate multiple addresses
sub_url = 'http://sub_links_1;http://sub_links_2'
# path to local configuration file
config_path = './template.yaml'
# output path
output_path = './config.yaml'
node_list = get_proxies(sub_url)
node_list = unique_name(node_list) # Save all servers with the same mark
default_config = load_local_config(config_path)
final_config = add_proxies_to_model(node_list, default_config)
save_config(output_path, final_config)