forked from THRAEXS/ssr-subscribe-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocesser.py
194 lines (151 loc) · 5.7 KB
/
processer.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
# -*- coding: utf-8 -*-
import json, base64, argparse, printc
from server import Start
props = {
'encoding': 'UTF-8',
'version': '2.0.0'
}
class Processer(object):
def __init__(self):
super(Processer, self).__init__()
'''
Please refer to: https://github.com/shadowsocksrr/shadowsocks-rss/wiki/SSR-QRcode-scheme
ssr://base64(host:port:protocol:method:obfs:base64pass/?
obfsparam=base64param&protoparam=base64param&remarks=base64remarks
&group=base64group&udpport=0&uot=0)
'''
self.link = '{server}:{server_port}:{protocol}:{method}:{obfs}:{password}/?obfsparam={obfs_param}&protoparam={protocol_param}&remarks={remarks}&group={group}'
self.params = [
{ 'key': 'server' },
{ 'key': 'server_port' },
{ 'key': 'protocol' },
{ 'key': 'method' },
{ 'key': 'obfs' },
{ 'key': 'password', 'b64': True },
{ 'key': 'obfs_param', 'b64': True },
{ 'key': 'protocol_param', 'b64': True },
{ 'key': 'remarks', 'b64': True, 'default': 'THRAEX-NODE', },
{ 'key': 'group', 'b64': True, 'default': 'THRAEX' }
]
def run_old(self):
with open('configs/config.json') as f:
configs = json.load(f)
target = 'configs/ssr-links'
with open(target, 'w') as f:
for cfg in configs:
b64_pass = self.b64encode(cfg['password'])
b64_obfsparam = self.b64encode(cfg['obfs_param'])
b64_protoparam = self.b64encode(cfg['protocol_param'])
b64_group = self.b64encode(cfg.get('group', ''))
b64_remarks = self.b64encode(cfg.get('remarks', ''))
b64_link = self.b64encode(self.link % (cfg.get('server'), cfg.get('server_port'),
cfg.get('protocol'), cfg.get('method'), cfg.get('obfs'), b64_pass, b64_obfsparam,
b64_protoparam, b64_remarks, b64_group))
final_link = ('ssr://%s' % b64_link)
print(final_link)
f.write(final_link)
f.write('\n')
print('******************************************')
with open(target) as f:
data = f.read().encode(props['encoding'])
print('%s\n' % data)
final = base64.b64encode(data).decode(props['encoding'])
print(final)
with open('configs/ssr-dist', 'w') as fo:
fo.write(final)
Start().UP()
def run(self):
printc.infoln('Loading configuration file...')
printc.info('-------------------< %s >------------------' %
printc.cyan('ssr-subscribe-generator'))
printc.info('Subscription Generator of SSR')
printc.infoln('---------------------------[ %s ]---------------------------' %
printc.cyan('Base64'))
cfgs = self.configs()
links = self.links(cfgs)
self.coding(links)
def configs(self):
cfg_path = 'configs/config.json'
printc.info('---------------------< %s >--------------------' %
printc.cyan(cfg_path))
configs = None
try:
with open(cfg_path) as f: configs = json.load(f)
total = len(configs)
if total > 0:
printc.infoln('Total: %s. The original information is:' % total)
for i in range(total):
printc.info('Server %d:' % (i + 1))
printc.info(printc.green((configs[i])))
printc.info()
else:
printc.warning('No server node information.')
except Exception as e:
printc.error(e)
return configs
def links(self, cfgs):
if not cfgs: return
printc.info('---------------------< %s >--------------------' %
printc.cyan('Generate SSR links'))
ssr_links = []
for i in range(len(cfgs)):
ind, item, b64_item = i + 1, {}, {}
for p in self.params:
key, def_val = p['key'], ''
if 'default' in p:
if key == 'remarks':
def_val = '%s-%d' % (p.get('default'), ind)
else:
def_val = p.get('default')
item[key] = cfgs[i].get(key, def_val)
bval = cfgs[i].get(key, def_val)
if p.get('b64'): bval = self.b64encode(bval)
b64_item[key] = bval
printc.info('Server %d:' % ind)
print(' ==> %s' % printc.green(self.link.format(**item)))
b64_link = self.link.format(**b64_item)
print(' ==> %s' % printc.green(b64_link))
ssr_link = 'ssr://%s' % self.b64encode(b64_link)
print(' ==> %s' % printc.green(ssr_link))
ssr_links.append(ssr_link)
printc.info()
# printc.info('---------------------< %s >--------------------' %
# printc.cyan('Output SSR links'))
# printc.info(printc.green('Output SSR links'))
ssr_links = '\n'.join(ssr_links)
printc.info('---------------------------------------------------------------')
print(printc.green(ssr_links))
printc.infoln('---------------------------------------------------------------')
return ssr_links
def coding(sefl, links):
if not links: return
printc.infoln('---------------------< %s >---------------------' %
printc.cyan('Encoding SSR link'))
target = 'configs/ssr-dist'
printc.info('Contents are written to %s:' % printc.green(target))
data = links.encode(props['encoding'])
final = base64.b64encode(data).decode(props['encoding'])
printc.info('---------------------------------------------------------------')
print(printc.green(final))
printc.infoln('---------------------------------------------------------------')
with open(target, 'w') as fo: fo.write(final)
printc.info('Execute %s to access %s.' %
(printc.green('server.py'), printc.green('ssr-dist')))
def b64encode(self, s):
return base64.urlsafe_b64encode(s.encode(props['encoding'])).decode(props['encoding']).rstrip('=')
if __name__ == '__main__':
# Processer().run()
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help = 'Increase output verbosity', action = 'store_true')
parser.add_argument('-V', '--version', help = 'Display version information', action = 'store_true')
parser.add_argument('--serve', help = 'Open web server', action = 'store_true')
args = parser.parse_args()
print(args.serve)
if args.version:
print(props['version'])
elif args.verbose:
pass
elif args.serve:
pass
else:
print('Process....')