Skip to content

Instantly share code, notes, and snippets.

@Justsoos
Created February 26, 2018 10:02
Show Gist options
  • Save Justsoos/f0f33c2552f26e0dcbb830e5a64d5c56 to your computer and use it in GitHub Desktop.
Save Justsoos/f0f33c2552f26e0dcbb830e5a64d5c56 to your computer and use it in GitHub Desktop.
def link2json(single_link):
if not single_link:
return None
link = to_str(single_link).strip()
try:
if link[:5] == 'ss://':
t = link[5:]
elif link[:6] == 'ssr://':
raise ValueError('Not SS, but SSR address: {}'.format(link))
elif len(link) < 6:
return None
else:
t = link
if '#' in t:
s = t.split('#',1)
t = s[0]
remarks = s[1]
if '@' not in t:
t = to_str(b64decode(t))
elif ':' not in (t.split('@',1)[0]):
odd_ss = to_str(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['remarks'] = remarks
result['timeout'] = 5
elif len(d) >= 4:
print('!!! THERE IS A PLUGIN SS URI ??!!{} {}'.format(d,link))
return None
else:
pass
return result
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
def b64decode(data):
if type(data) == str:
return base64.b64decode(data+'='*(4-len(data)%4))
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment