-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm2obs.py
140 lines (109 loc) · 3.47 KB
/
vm2obs.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import base64
import os
import string
import random
from settings import load_TPL, CONNECTIONS_DIR, check_link
vmscheme = "vmess://"
ret = {"outbounds": [
]}
def fill_tcp_http(_c, _v):
tcps = load_TPL("http")
tcps["header"]["type"] = _v["type"]
if _v["host"] != "":
# multiple host
tcps["header"]["request"]["headers"]["Host"] = _v["host"].split(",")
if _v["path"] != "":
tcps["header"]["request"]["path"] = [_v["path"]]
_c[0]["streamSettings"]["tcpSettings"] = tcps
return _c
def fill_kcp(_c, _v):
kcps = load_TPL("kcp")
kcps["header"]["type"] = _v["type"]
_c[0]["streamSettings"]["kcpSettings"] = kcps
return _c
def fill_ws(_c, _v):
wss = load_TPL("ws")
wss["path"] = _v["path"]
wss["headers"]["Host"] = _v["host"]
_c[0]["streamSettings"]["wsSettings"] = wss
return _c
def fill_h2(_c, _v):
h2s = load_TPL("h2")
h2s["path"] = _v["path"]
h2s["host"] = [_v["host"]]
_c[0]["streamSettings"]["httpSettings"] = h2s
return _c
def fill_quic(_c, _v):
quics = load_TPL("quic")
quics["header"]["type"] = _v["type"]
quics["security"] = _v["host"]
quics["key"] = _v["path"]
_c[0]["streamSettings"]["quicSettings"] = quics
return _c
def fill_basic(_c, _v):
_outbound = _c[0]
_vnext = _outbound["settings"]["vnext"][0]
_vnext["address"] = _v["add"]
_vnext["port"] = int(_v["port"])
_vnext["users"][0]["id"] = _v["id"]
_vnext["users"][0]["alterId"] = int(_v["aid"])
_outbound["streamSettings"]["network"] = _v["net"]
if _v["tls"] == "tls":
_outbound["streamSettings"]["security"] = "tls"
_outbound["streamSettings"]["tlsSettings"] = {"allowInsecure": True}
if _v["host"] != "":
_outbound["streamSettings"]["tlsSettings"]["serverName"] = _v["host"]
return _c
def vmess2outbounds(_t, _v):
_net = _v["net"]
_type = _v["type"]
_c = fill_basic(_t, _v)
if _net == "kcp":
return fill_kcp(_c, _v)
elif _net == "ws":
return fill_ws(_c, _v)
elif _net == "h2":
return fill_h2(_c, _v)
elif _net == "quic":
return fill_quic(_c, _v)
elif _net == "tcp":
if _type == "http":
return fill_tcp_http(_c, _v)
return _c
else:
raise Exception(
"this link seem invalid to the script.")
def parsevmess(vmesslink):
if vmesslink.startswith(vmscheme):
bs = vmesslink[len(vmscheme):]
blen = len(bs)
if blen % 4 > 0:
bs += "=" * (4 - blen % 4)
vms = base64.b64decode(bs).decode()
return json.loads(vms)
else:
raise Exception("vmess link invalid")
def parseLink(link):
if link.startswith(vmscheme):
return parsevmess(link)
else:
return None
def convert(vmess_link: str):
vc = parseLink(vmess_link)
if vc is None:
return "", ""
if not check_link(vc):
return "", ""
ret['outbounds'] = vmess2outbounds(load_TPL("outbounds"), vc)
for item in ret['outbounds']:
if vmess_link.startswith(vmscheme) and 'vnext' in item['settings']:
item['protocol'] = 'vmess'
if not os.path.exists(CONNECTIONS_DIR):
os.makedirs(CONNECTIONS_DIR)
ran_str = ''.join(random.sample(string.ascii_lowercase, 12))
node_name = vc["ps"]
out_path = CONNECTIONS_DIR+("{}.json".format(ran_str))
with open(out_path, 'w') as f:
f.write(json.dumps(ret, indent=4))
return ran_str, node_name