forked from Jrohy/multi-v2ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
88 lines (72 loc) · 3.13 KB
/
client.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json
from config import Config
from utils import get_ip
from group import Vmess, Socks, SS, Mtproto
from selector import ClientSelector
class ClientWriter:
def __init__(self, group, client_index):
self.config_factory = Config()
with open(self.config_factory.get_path("config_path"), 'r') as json_file:
self.config = json.load(json_file)
self.write_path = self.config_factory.get_path("write_client_path")
self.template_path = self.config_factory.get_path("template_path")
self.group = group
self.client_index = client_index
self.node = group.node_list[client_index]
def load_template(self, template_name):
'''
load special template
'''
with open(self.template_path + "/" + template_name, 'r') as stream_file:
template = json.load(stream_file)
return template
def transform(self):
user_json = None
if type(self.node) == Vmess:
self.client_config = self.load_template('client.json')
user_json = self.client_config["outbounds"][0]["settings"]["vnext"][0]
user_json["users"][0]["id"] = self.node.password
user_json["users"][0]["alterId"] = self.node.alter_id
elif type(self.node) == Socks:
self.client_config = self.load_template('client_socks.json')
user_json = self.client_config["outbounds"][0]["settings"]["servers"][0]
user_json["users"][0]["user"] = self.node.user_info
user_json["users"][0]["pass"] = self.node.password
elif type(self.node) == SS:
self.client_config = self.load_template('client_ss.json')
user_json = self.client_config["outbounds"][0]["settings"]["servers"][0]
user_json["method"] = self.node.method
user_json["password"] = self.node.password
elif type(self.node) == Mtproto:
print("\nMTProto协议只支持Telegram通信, 所以无法生成配置文件!\n")
exit(-1)
user_json["port"] = int(self.group.port)
if type(self.node) != SS:
self.client_config["outbounds"][0]["streamSettings"] = self.config["inbounds"][group.index]["streamSettings"]
if group.tls == 'tls':
content = self.config_factory.get_data("domain")
user_json["address"] = str(content)
self.client_config["outbounds"][0]["streamSettings"]["tlsSettings"] = {}
else:
user_json["address"] = str(get_ip())
def write(self):
'''
写客户端配置文件函数
'''
json_dump = json.dumps(self.client_config,indent=1)
with open(self.write_path, 'w') as write_json_file:
write_json_file.writelines(json_dump)
print("保存成功!({})\n".format(self.write_path))
if __name__ == '__main__':
cs = ClientSelector('生成客户端json')
client_index = cs.client_index
group = cs.group
if group == None:
exit(-1)
else:
cw = ClientWriter(group, client_index)
cw.transform()
cw.write()