Last active
November 18, 2019 05:54
-
-
Save RevySR/9ce23f6eb8ebaf8d018d8285390510d1 to your computer and use it in GitHub Desktop.
trans_v2ray_to_surge for surge mac 3.3.1
This file contains hidden or 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
#!/usr/bin/env python3 | |
import requests | |
import base64 | |
import json | |
wdnmd_network_v2ray_sub_url = "https://wdnmd.win/*****" | |
nps_boost_v2ray_sub_url = "https://npsboost.com/*****" | |
def base64Decode(base64_code): | |
return base64.urlsafe_b64decode(base64_code + '====').decode('utf-8') | |
def parse_config(vmess_json): | |
server_name = vmess_json['ps'].replace("[","$").replace("]","$") | |
format_string = "{name} = vmess, {address}, {port}, username={uuid}, tls={tls_status}, ws={ws_status}" | |
server_config = format_string.format( | |
name=server_name, | |
address=vmess_json['add'], | |
port=vmess_json['port'], | |
uuid=vmess_json['id'], | |
tls_status="true" if vmess_json['tls'] == "tls" else "false", | |
ws_status="true" if vmess_json['net'] == "ws" else "false" | |
) | |
if vmess_json['net'] == "ws": | |
extra_ws_format_config = ", ws-path={ws_path}, ws-headers=Host:{host}" | |
extra_ws_config = extra_ws_format_config.format(ws_path=vmess_json['path'], host=vmess_json['host']) | |
server_config += extra_ws_config | |
return server_config | |
def parse_v2ray_subscribe(url): | |
v2ray_sub_base64 = requests.get(url).content.decode('utf-8') | |
v2ray_sub_vmess = base64Decode(v2ray_sub_base64).split('\n') | |
result_servers = [] | |
for vmess in v2ray_sub_vmess: | |
if vmess == "": | |
continue | |
vmess_base64 = vmess.replace("vmess://", "") | |
vmess_json_ascii = base64Decode(vmess_base64) | |
vmess_json = json.loads(vmess_json_ascii) | |
# print(vmess_json) | |
result = parse_config(vmess_json) | |
result_servers.append(result) | |
return result_servers | |
def config_out(all_config): | |
name_server = [] | |
print("[Proxy]") | |
for config in all_config: | |
print(config) | |
name_server.append(config.split(' ')[0]) | |
print() | |
print("[Proxy Group]") | |
print("PROXY = select, DIRECT", end="") | |
for name in name_server: | |
print(", " + name, end=' ') | |
print() | |
if __name__ == "__main__": | |
all_config = [] | |
wdnmd_network_config = parse_v2ray_subscribe(wdnmd_network_v2ray_sub_url) | |
all_config += wdnmd_network_config | |
nps_boost_config = parse_v2ray_subscribe(nps_boost_v2ray_sub_url) | |
all_config += nps_boost_config | |
config_out(all_config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment