-
Notifications
You must be signed in to change notification settings - Fork 2
/
WXPatch.py
110 lines (103 loc) · 5.15 KB
/
WXPatch.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
# -*- coding: utf-8 -*-
"""
winXray配置丢失补丁
"""
import requests
from os import path, getenv, startfile
from sys import exc_info, executable
from base64 import b64decode
from json import loads
from re import split, search, sub
from urllib3 import disable_warnings
from urllib.request import unquote
#from time import sleep
disable_warnings()
app_root = path.dirname(path.realpath(executable))
wx_exe = (app_root if path.isfile(app_root + r'\WinXray.exe') else path.dirname(app_root)) + r'\WinXray.exe'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
}
def decode_b64(data):
from base64 import urlsafe_b64decode
missing_padding = len(data) % 4
if missing_padding != 0:
data += '=' * (4 - missing_padding)
return urlsafe_b64decode(data).decode()
def getProxy(rawurl, base64=True):
urlst = [rawurl]+['https://raw.iqiq.io/lon91ong/python/master/sslink',
'https://raw.fastgit.org/lon91ong/python/master/sslink',
'https://fastly.jsdelivr.net/gh/lon91ong/python@master/sslink',
'https://cdn.staticaly.com/gh/lon91ong/python/master/sslink',
'https://gcore.jsdelivr.net/gh/lon91ong/python@master/sslink']
proxies = []
for ral in urlst:
try:
ral = ral[:-2] if ral[-2:]=='64' else ral
resp = requests.get(ral, headers=headers, verify=False, timeout=6)
if base64:
proxies = [ral + '64'] + b64decode(resp.content).decode().split('\n')
else:
proxies = [ral + '64'] + resp.text.split('\n')[:-1]
except:
print(f'{ral} get failed with error:', exc_info()[0])
continue
else:
break
return proxies
with open(path.join(app_root, 'proxy_bak.table'), 'r', encoding='utf-8-sig') as f:
prtab = f.readlines()
f.close()
try:
ustr = search('http[\w,:/\.\-\@]+', prtab[4])[0]
proxies = getProxy(ustr, ustr[-2:]=='64')
prtab[4] = sub('http[\w,:/\.\-\@]+', proxies[0], prtab[4], 1)
n = 1
result = ''
for locp in proxies[1:]:
if locp[:3] == 'ssr':
ssrl = split(':|&|/|=', decode_b64(locp[6:]))
result += f'[{n}]=' + '{protocol="ssr";' + 'id="{}";'.format(decode_b64(ssrl[5])) + \
f'obfsParam="";port="{ssrl[1]}";' + f'subscribeUrl="{proxies[0]}";' + \
f'address="{ssrl[0]}";obfs="{ssrl[4]}";' + 'ps="{}";'.format(decode_b64(ssrl[-3])) + \
f'network="{ssrl[2]}";security="{ssrl[3]}"' + '};'
elif locp[:5] == 'vmess':
ssrl = loads(decode_b64(locp[8:]))
result += f'[{n}' + ']={' + f'tls="{ssrl["tls"]}";path="{ssrl["path"]}";protocol="vmess";alterId={ssrl["aid"]};host="{ssrl["host"]}";' + \
f'port="{ssrl["port"]}";alpn="{ssrl["alpn"]}";scy="{ssrl["scy"]}";["type"]="{ssrl["type"]}";network="{ssrl["net"]}";' + \
f'subscribeUrl="{proxies[0]}";security="auto";address="{ssrl["add"]}";id="{ssrl["id"]}";sni="{ssrl["sni"]}";ps="{ssrl["ps"]}"'+ '};'
elif locp[:5] == 'ss://':
strs = split('@',locp[5:])
ssrl = split(':', decode_b64(strs[0])) + split(':|#',strs[1])
result += f'[{n}' + ']={' + f'port={ssrl[3]};network="tcp";address="{ssrl[2]}";id="{ssrl[1]}";' + \
f'ps="{ssrl[4]}";protocol="shadowsocks";security="{ssrl[0]}"' + '};'
elif locp[:6] == 'trojan':
ssrl = split('#|\?', locp[9:])
ssrl[0] = split('@|:', ssrl[0])
ssrl[1] = loads('{"' + ssrl[1].replace('&','","').replace('=','":"') + '"}')
result += f'[{n}' + ']={' + f'tls="{ssrl[1]["security"]}";port={ssrl[0][2]};protocol="trojan";address="{ssrl[0][1]}";id="{ssrl[0][0]}";' + \
f'ps="{ssrl[-1]}";subscribeUrl="{proxies[0]}";'
result += f'sni="{ssrl[1]["sni"]}";' if "sni" in ssrl[1] else ''
result += f'host="{ssrl[1]["host"]}";' if "host" in ssrl[1] else ''
result += f'network="{ssrl[1]["type"]}' + '"};'
elif locp[:5] == 'vless':
ssrl = split('#|\?', locp[8:])
ssrl[0] = split('@|:', ssrl[0])
ssrl[1] = loads('{"' + ssrl[1].replace('&','","').replace('=','":"') + '"}')
result += f'[{n}' + ']={' + f'tls="{ssrl[1]["security"]}";path="{unquote(ssrl[1]["path"])}";' + \
f'network="{ssrl[1]["type"]}";id="{ssrl[0][0]}";subscribeUrl="{proxies[0]}";'
result += f'sni="{ssrl[1]["sni"]}";' if "sni" in ssrl[1] else ''
result += f'host="{ssrl[1]["host"]}";' if "host" in ssrl[1] else ''
result += f'port={ssrl[0][2]};address="{ssrl[0][1]}";ps="{ssrl[2]}";protocol="vless";security="{ssrl[1]["encryption"]}' + '"};'
else:
continue
n += 1
print(f'Total of {n - 1} nodes written!')
prtab[5] = 'outbounds={' + result[:-1] + '};'
with open(getenv('LocalAppData') + '/winXray/proxy.table', 'w', encoding='utf-8-sig') as f:
f.writelines(sl for sl in prtab)
except:
print("Unexpected error:", exc_info()[0:2])
pass
finally:
startfile(wx_exe)
#sleep(5)