-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathss2ssr.py
326 lines (277 loc) · 7.28 KB
/
ss2ssr.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
#! /usr/bin/env python3
__author__ = 'JZ'
__webpage__ = 'https://github.com/Justsoos'
import re
import sys,os
import logging
import json
import time
import argparse
import base64
import traceback
from glob import glob
def remove_dup_jsons(json_list):
l = json_list
if not l:
return
dest_list = []
dup_list = []
dup_times = 0
num = len(l)
bad_records = {}
good_records = []
# Looking for None record
for ie in range(num):
if not ie:
continue
try:
if (
l[ie]['server'] and
l[ie]['server_port'] and
l[ie]['password'] and
l[ie]['method']
):
good_records.append(ie)
l[ie]['server_port'] = int(l[ie]['server_port'])
except KeyError as err:
bad_records[ie] = err
if num <= 1:
return l
#print(bad_records, good_records)
if bad_records and good_records:
n = good_records
else:
n = list(range(num))
# Looking for duplicate
l_max = len(n)
for i in range(l_max):
for j in range((i+1), l_max):
try:
if (
(l[n[i]]['server'] == l[n[j]]['server']) and
(int(l[n[i]]['server_port']) == int(l[n[j]]['server_port'])) and
(l[n[i]]['password'] == l[n[j]]['password']) and
(l[n[i]]['method'] == l[n[j]]['method'])
):
dup_times += 1
#dup_list.append(l[n[i]])
dest_found = False
break
else:
dest_found = True
except:
raise
if dest_found:
dest_list.append(l[n[i]])
if bad_records:
for line, erro in bad_records.items():
print('***The No.{} record seems wrong with {}...'.format(line+1, erro))
#print('There are {} records inputed.'.format(num))
#print('Found {} times of duplicate records.'.format(dup_times))
return dest_list
def remove_dup_links(url_list):
l = list({}.fromkeys(url_list).keys())
dest_list = []
for line in l:
ls = line.strip()
if len(ls) > 10:
dest_list.append(ls)
return dest_list
def sslink2json(single_link):
if not single_link:
return None
remarks = ''
obfs = 'plain'
link = to_str(single_link)
#print(link)
try:
if link[:5] == 'ss://':
t = link[5:]
elif link[:6] == 'ssr://':
raise ValueError('Not SS, but SSR address: {}'.format(link))
else:
return
if '#' in t:
s = t.split('#',1)
t = s[0]
remarks = s[1]
if '@' not in t:
t = b64decode(t)
elif ':' not in (t.split('@',1)[0]):
odd_ss = b64decode(t.split('@',1)[0])
t = '{}@{}'.format(odd_ss, t.split('@',1)[1])
else:
pass
t.strip('/')
except Exception as e:
raise e
result = {}
d = t.split(':')
if len(d) == 3:
result['server'] = d[1].rsplit('@',1)[1]
result['server_port'] = int(d[2])
result['password'] = d[1].rsplit('@',1)[0]
result['method'] = d[0]
result['plugin'] = ''
result['plugin_opts'] = ''
result['obfs'] = 'plain'
result['remarks'] = remarks
result['timeout'] = 5
elif len(d) >= 4:
print('!!! THERE IS A BAD SS URI ??!!{} {}'.format(d, link))
return None
else:
pass
return result
def ssjsons2ssrlinks(jsons):
d = jsons
links = []
try:
if (isinstance(d, dict)) and d['configs']:
j = d['configs']
else:
j = d
for i in j:
server = i.get('server')
server_port = i.get('server_port')
password = i.get('password')
method = i.get('method')
obfs = i.get('obfs', 'plain')
remarks = i.get('remarks')
uri = '{}:{}:origin:{}:{}:{}'.format(server, server_port, method, obfs, b64encode(password))
group = b64encode('SS-2-SSR_{}'.format(time.strftime('%Y.%m.%d_%H.%M')))
stern = '/?obfsparam=&remarks={}&group={}'.format(b64encode(remarks), group)
links.append('ssr://{}'.format(b64encode(('{}{}'.format(uri, stern)))))
except:
raise
return links
def to_bytes(s):
if type(s) == str:
return s.encode('utf-8')
return s
def to_str(s):
if type(s) == bytes:
return s.decode('utf-8')
return s
def b64encode(data):
if type(data) == bytes:
return to_str(base64.urlsafe_b64encode(data)).strip('=')
elif type(data) == str:
return to_str(base64.urlsafe_b64encode(to_bytes(data))).strip('=')
else:
return data
def b64decode(data):
if type(data) == str:
return to_str(base64.urlsafe_b64decode(data+'='*(4-len(data)%4)))
return data
def read_links_file(file):
with open(file, 'rb') as f:
links = f.readlines()
return links
def read_configs_file(file):
try:
with open(file, 'rb') as f:
d = json.load(f)
if (isinstance(d, dict)) and d['configs']:
l = d['configs']
elif (isinstance(d, list)):
l = d
else:
raise Exception('Wrong or Damaged SS config file...Pls check check {}'.format(file))
except KeyError:
pass
return l
def func_1st(configs, links):
if configs:
j0 = configs
t0 = len(j0)
else:
t0 = 0
j0 = []
if links:
l1 = links
l2 = remove_dup_links(l1)
t1 = len(l1)
else:
t1 = 0
l2 = []
t12 = 0
for li in l2:
if sslink2json(li):
j0.append(sslink2json(li))
t2 = len(j0)
#print(j0)
j2 = remove_dup_jsons(j0)
if j2:
t3 = len(j2)
links_result = ssjsons2ssrlinks(j2)
else:
t3 = 0
links_result = None
info = '*** From configs JSON file: {} records, from link URI file: {} records. Total: {}. After json-inside duplicate remove, {} links here. '.format(t0, t1, t2, t3)
print(info)
if links_result:
links_file = 'SS_to_SSR_links_{}.txt'.format(time.strftime('%Y-%m-%d_%H-%M-%S'))
with open(links_file, 'w', encoding='utf-8') as f:
for i in links_result:
print(i, file=f)
print('*** {} SS to SSR Links records saved to {} ...'.format(len(links_result), links_file))
def main_dev():
global json_files
global uri_files
global link_ss
parser = argparse.ArgumentParser( description='de-duplicate, merge, convert SS to SSR uri and backup tools for Shadowsocks, working under Python3')
parser.add_argument('-j', metavar='input JSON filenames', dest='json_files', type=str, nargs='+', help='Input json or SS config filenames')
parser.add_argument('-l', metavar='input Link filenames', dest='uri_files', type=str, nargs='+', help='Input uri link filenames')
parser.add_argument('-s', metavar='ss links', dest='link_ss', type=str, nargs='+', help='Input SS links...single or more')
parser.add_argument('-v',action='version', version='ss to ssr 0.2')
args = parser.parse_args()
if args.json_files:
json_files = (glob(name) for name in args.json_files)
json_files = [i for j in json_files for i in j]
json_files = list(set(json_files))
else:
json_files = None
if args.uri_files:
uri_files = (glob(name) for name in args.uri_files)
uri_files = [i for j in uri_files for i in j]
uri_files = list(set(uri_files))
else:
uri_files = None
if args.link_ss:
sign = set(((x.strip('\''))[:5] == 'ss://' for x in args.link_ss))
if len(sign) == 1 and True in sign:
link_ss = [y.strip('\'') for y in args.link_ss]
else:
parser.print_help()
print('*** Please input correct ss:// link')
sys.exit(1)
elif ((len(sys.argv) < 2) or ((not json_files) and (not uri_files))):
parser.print_help()
sys.exit(1)
else:
link_ss = None
print(args)
print('*** Working on ... ', json_files, uri_files, link_ss)
def main():
global json_files
global uri_files
global link_ss
main_dev()
if link_ss:
for i in link_ss:
js = sslink2json(i)
ls = ssjsons2ssrlinks([js])
print(''.join(ls))
sys.exit(1)
configs = []
links = []
if json_files:
for i in json_files:
configs.extend(read_configs_file(i))
if uri_files:
for j in uri_files:
links.extend(read_links_file(j))
func_1st(configs, links)
if __name__ == '__main__':
main()