forked from Jrohy/multi-v2ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
57 lines (48 loc) · 1.87 KB
/
converter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
config_path = "/etc/v2ray/config.json"
class ConfigConverter:
def __init__(self):
self.config = self.load()
def load(self):
'''
load v2ray profile
'''
with open(config_path, 'r') as json_file:
config = json.load(json_file)
return config
def save(self):
'''
save v2ray config.json
'''
json_dump=json.dumps(self.config, indent=2)
with open(config_path, 'w') as writer:
writer.writelines(json_dump)
def transform(self):
inbound_list, outbound_list = [], []
if "inbound" in self.config:
inbound_list.append(self.config["inbound"])
del self.config["inbound"]
if "inboundDetour" in self.config and self.config["inboundDetour"]:
inbound_list.extend([ x for x in self.config["inboundDetour"] ])
del self.config["inboundDetour"]
if inbound_list:
self.config["inbounds"] = inbound_list
if "outbound" in self.config:
outbound_list.append(self.config["outbound"])
del self.config["outbound"]
if "outboundDetour" in self.config and self.config["outboundDetour"]:
outbound_list.extend([ x for x in self.config["outboundDetour"] ])
del self.config["outboundDetour"]
if outbound_list:
self.config["outbounds"] = outbound_list
# 转换路由
if "routing" in self.config and "settings" in self.config["routing"]:
self.config["routing"]["rules"] = self.config["routing"]["settings"]["rules"]
del self.config["routing"]["strategy"]
del self.config["routing"]["settings"]
self.save()
if __name__ == '__main__':
print("转换配置文件格式中..")
ConfigConverter().transform()