Skip to content

Instantly share code, notes, and snippets.

@Justsoos
Last active February 27, 2018 15:15
Show Gist options
  • Save Justsoos/90051bcdc37465f7bdff7007967a064e to your computer and use it in GitHub Desktop.
Save Justsoos/90051bcdc37465f7bdff7007967a064e to your computer and use it in GitHub Desktop.
def json2link(configs_json):
d = configs_json
link = []
try:
if (isinstance(d, dict)) and d['configs']:
j = d['configs']
else:
j = d
for i in j:
server = i['server']
server_port = i['server_port']
password = i['password']
method = i['method']
remarks = i['remarks']
uri = to_bytes('{}:{}@{}:{}'.format(method, password, server, server_port))
link.append('ss://{}#{}'.format(b64encode(uri), remarks))
except:
raise
return link
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('=')
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment