-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathss-subscribe.py
69 lines (60 loc) · 2.29 KB
/
ss-subscribe.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
import base64
import json
import re
import requests
from urllib.parse import unquote_plus, quote_plus
def print_dict(d):
res = ''
for key in d:
res += '{}:{}\n'.format(key, d[key])
return res[:-1]
def save(servers):
ss_client_config = json.load(open('gui-config.json', 'r', encoding='utf-8'))
# print(ss_client_config)
configs = ss_client_config['configs']
# print(configs)
new_addrs = list(map(lambda x: x['server'], servers))
configs = list(filter(lambda x: x['server'] not in new_addrs, configs))
print('\n清除旧线路(地址与新线路相同)成功!\n')
configs.extend(servers)
ss_client_config['configs'] = configs
json.dump(ss_client_config, open('gui-config.json', 'w', encoding='utf-8'), indent=2)
print('保存成功,请重启ss客户端!')
def read_config():
subscribe_config = json.load(open("ss-subscribe.json", 'r'))
servers = subscribe_config.get('servers', [])
res = []
for s in servers:
url = s.get('url')
resp = requests.get(url).text
links = base64.b64decode(resp).decode('utf-8').split('\n')
# print(links)
print('解析链接({})成功,此URL包含{}条ss://链接:'.format(url, len(links)))
for l in links:
server = {}
tag = unquote_plus(re.findall(r'#(.*?)$', l)[0])
server['remarks'] = '{}'.format(tag)
server['timeout'] = 5
if '/?' in l:
plugin = re.findall(r'/\?(.*?)#', l)[0][7:]
server['plugin'], server['plugin_opts'] = unquote_plus(plugin).split(';')
password = re.findall(r'ss://(.*?)@', l)[0]
server['method'], server['password'] = base64.b64decode(password).decode('utf-8').split(':')
address = re.findall(r'@(.*?)/\?', l)[0]
server['server'] = address.split(':')[0]
server['server_port'] = int(address.split(':')[1])
# print(server)
else:
address = re.findall(r'ss://(.*?)#', l)[0]
address_c = base64.b64decode(address).decode('utf-8')
params = re.split(':|@', address_c)
server['method'], server['password'], server['server'] = params[:-1]
server['server_port'] = int(params[-1])
server['plugin'] = server['plugin_opts'] = ''
# print(server)
print('\n链接({})信息如下:\n{}'.format(l, print_dict(server)))
res.append(server)
return res
if __name__ == '__main__':
# print(read_config())
save(read_config())