Created
September 22, 2020 06:53
-
-
Save p0we7/87d0ccdab38d273441a6b9aa67ce5145 to your computer and use it in GitHub Desktop.
Decode SSR subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import base64 | |
import re | |
def base64decode(text): | |
i = len(text) % 4 | |
if i == 1: | |
text = text + '===' | |
elif i == 2: | |
text = text + '==' | |
elif i == 3: | |
text = text + '=' | |
text = re.sub(r'_', '/', text) | |
text = re.sub(r'-', '+', text) | |
return base64.urlsafe_b64decode(text).decode() | |
def get_ssr_list(url): | |
print('Being parsed the ssr url:', url) | |
print('It will take a moment,Please be patient~~') | |
result = requests.get(url, headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3742.400 QQBrowser/10.5.3864.400'}) | |
try: | |
ssr_result = base64decode(result.text) | |
except: | |
print("ssr subscribe url parsed failed,please check the ssr subscribe url~~") | |
return None | |
else: | |
ssr_list = ssr_result.split('\n') | |
ssr_real_list = list() | |
for ssr in ssr_list: | |
if ssr: | |
ssr_real_list.append(ssr[6:]) | |
return ssr_real_list | |
# 解析ssr url链接 | |
def analysis_ssr_url(ssr_url): | |
try: | |
ssr_url = base64decode(ssr_url) | |
except: | |
pass | |
else: | |
ssr_dict = dict() | |
param_list = ssr_url.split(':') | |
if len(param_list) == 6: | |
server = param_list[0] | |
port = param_list[1] | |
protocol = param_list[2] | |
method = param_list[3] | |
obfs = param_list[4] | |
second_encryption_param_list = param_list[-1].split('/?') | |
password = base64decode(second_encryption_param_list[0]) | |
encryption_param_list = second_encryption_param_list[-1].split('&') | |
key_list = ['obfs_param','protocol_param','remarks','group'] | |
for params in encryption_param_list: | |
key = params.split('=')[0] | |
value = params.split('=')[1] | |
if key == 'obfsparam': | |
key = 'obfs_param' | |
if key == 'protoparam': | |
key = 'protocol_param' | |
if key in key_list: | |
ssr_dict[key] = base64decode(value) | |
ssr_dict['server'] = server | |
ssr_dict['server_port'] = int(port) | |
ssr_dict['method'] = method | |
ssr_dict['obfs'] = obfs | |
ssr_dict['password'] = password | |
ssr_dict['protocol'] = protocol | |
return ssr_dict | |
else: | |
print("Sorry, Not support ipv6 node~~") | |
return None | |
# 生成ssr 信息列表字典 | |
def generate_ssr_info_dict_list(ssr_url_list): | |
ssr_info_dict_list = list() | |
for ssr_url in ssr_url_list: | |
ssr_info_dict = analysis_ssr_url(ssr_url) | |
if ssr_info_dict: | |
ssr_info_dict_list.append(ssr_info_dict) | |
return ssr_info_dict_list | |
# url链接 | |
def combine_ssr_url(ssr_url_list): | |
ssr_dict = [] | |
return ssr_decoded_list | |
if __name__ == "__main__": | |
url = 'https://[replace-your-subscription-url]' | |
ssr_url_list = get_ssr_list(url) | |
ssr_dict = generate_ssr_info_dict_list(ssr_url_list) | |
ssr_urls = list() | |
for ssr_json in ssr_dict[2:]: | |
# example.com:1111:auth_aes128_md5:rc4-md5:tls1.2_ticket_auth:dkownSKLio/?obfsparam=bing.com&protoparam=18181:xkwWEo&remarks=US-NODE&group=Github | |
ssr_url = 'ssr://{method}:{password}@{server}:{server_port}?protocol={protocol}&protocol_param={protocol_param}&obfs={obfs}&obfs_param={obfs_param}&remarks={remarks}'.format( | |
method=ssr_json['method'], | |
password=ssr_json['password'], | |
server=ssr_json['server'], | |
server_port=ssr_json['server_port'], | |
protocol=ssr_json['protocol'], | |
protocol_param=ssr_json['protocol_param'], | |
obfs=ssr_json['obfs'], | |
obfs_param=ssr_json['obfs_param'], | |
remarks=ssr_json['remarks'] | |
) | |
ssr_urls.append(ssr_url) | |
print(ssr_url) | |
with open('ssr_urls.txt', 'w') as f: | |
for url in ssr_urls: | |
f.write(url) | |
f.write('\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Part of the code from xiaoliang8006