This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathtrojan.py
61 lines (60 loc) · 1.95 KB
/
trojan.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
import tool,json,re,urllib,sys
def parse(data):
node = {
'tag':None,
'type':'trojan',
'server':None,
'server_port':None,
'password':None
}
if isinstance(data,bytes):
data = bytes.decode(data)
m = re.search(r'://(.+?)@(.+?):(\d+)',data)
if m:
node['password'] = urllib.parse.unquote(m.group(1))
node['server'] =m.group(2)
node['server_port'] =int(m.group(3))
else:
return None
m = re.search(r'/?\?(.+?)#',data)
if m:
params = m.group(1)
palist = params.split('&')
opts = {}
for kv in palist:
k = kv.split('=')[0]
v = urllib.parse.unquote(kv.split('=')[1])
opts[k] = v
node['tls']={}
if opts.get('allowInsecure'):
node['tls']={
'enabled' : True,
'insecure' : False
}
if(opts['allowInsecure']=='0'):
node['tls']['insecure']=False
else:
node['tls']['insecure']=True
if opts.get('sni'):
node['tls']['enabled'] = True
node['tls']['disable_sni'] = False
node['tls']['server_name'] = opts['sni']
if opts.get('type'):
if opts['type'] == 'h2':
node['transport']={
'type':'http',
'host':opts['host'] if opts.get['host'] else node['server'],
'path':opts['path'] if opts.get('path') else '/'
}
if opts['type'] == 'ws':
node['transport']={
'type':'ws',
'path':opts['path'] if opts.get('path') else '/'
}
if data.find('#')>-1:
name = urllib.parse.unquote(data[data.find('#')+1:])
name = name.strip()
else:
name = tool.genName()
node['tag'] = name
return node