forked from Toperlock/sing-box-subscribe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrojan.py
72 lines (72 loc) · 2.78 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
62
63
64
65
66
67
68
69
70
71
72
import tool,re
from urllib.parse import urlparse, parse_qs, unquote
def parse(data):
info = data[:]
server_info = urlparse(info)
if server_info.path:
server_info = server_info._replace(netloc=server_info.netloc + server_info.path, path="")
if '@' in server_info.netloc:
_netloc = server_info.netloc.rsplit("@", 1)
else:
return None
netquery = dict(
(k, v if len(v) > 1 else v[0])
for k, v in parse_qs(server_info.query).items()
)
node = {
'tag': unquote(server_info.fragment) or tool.genName()+'_trojan',
'type': 'trojan',
'server': re.sub(r"\[|\]", "", _netloc[1].rsplit(":", 1)[0]),
'server_port': int(_netloc[1].rsplit(":", 1)[1].split("/")[0]),
'password': _netloc[0],
'tls': {
'enabled': True,
'insecure': False
}
}
if netquery.get('allowInsecure') == '1':
node['tls']['insecure'] = True
if netquery.get('alpn'):
node['tls']['alpn'] = netquery.get('alpn').strip('{}').split(',')
if netquery.get('sni'):
node['tls']['server_name'] = netquery.get('sni', '')
if netquery.get('fp'):
node['tls']['utls'] = {
'enabled': True,
'fingerprint': netquery.get('fp')
}
if netquery.get('type'):
if netquery['type'] == 'h2':
node['transport'] = {
'type':'http',
'host':netquery.get('host', node['server']),
'path':netquery.get('path', '/')
}
if netquery['type'] == 'ws':
matches = re.search(r'\?ed=(\d+)$', netquery.get('path', '/'))
if netquery.get('host'):
node['transport'] = {
'type':'ws',
'path':netquery.get('path', '/').rsplit("?ed=", 1)[0] if matches else netquery.get('path', '/'),
'headers': {
'Host': netquery.get('host')
}
}
elif netquery['type'] == 'grpc':
node['transport'] = {
'type':'grpc',
'service_name':netquery.get('serviceName', '')
}
if netquery.get('protocol') in ['smux', 'yamux', 'h2mux']:
node['multiplex'] = {
'enabled': True,
'protocol': netquery['protocol']
}
if netquery.get('max-streams'):
node['multiplex']['max_streams'] = int(netquery['max-streams'])
else:
node['multiplex']['max_connections'] = int(netquery['max-connections'])
node['multiplex']['min_streams'] = int(netquery['min-streams'])
if netquery.get('padding') == 'True':
node['multiplex']['padding'] = True
return node