forked from FunctionClub/V2ray.Fun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
235 lines (187 loc) · 5.94 KB
/
app.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask,render_template,request
import os
from Config_Generator import *
from flask_basicauth import BasicAuth
panel_config_file = open("panel.config","r")
panel_config = json.loads(panel_config_file.read())
panel_config_file.close()
app = Flask(__name__,static_url_path='/static')
app.config['BASIC_AUTH_USERNAME'] = panel_config['username']
app.config['BASIC_AUTH_PASSWORD'] = panel_config['password']
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)
def change_config(config,value):
old_config = open("v2ray.config", "r")
old_json = json.loads(old_config.read())
old_config.close()
old_json[str(config)] = str(value)
config = open("v2ray.config","w")
string = str(json.dumps(old_json,indent=2))
config.write(string)
config.close()
def get_status():
cmd = """ps -ef | grep "v2ray" | grep -v grep | awk '{print $2}'"""
output = commands.getoutput(cmd)
if output=="":
return "off"
else:
return "on"
@app.route('/start_service')
def start_service():
cmd = "service v2ray start"
commands.getoutput(cmd)
change_config("status","on")
return "OK"
@app.route('/stop_service')
def stop_service():
cmd = "service v2ray stop"
commands.getoutput(cmd)
change_config("status", "off")
return "OK"
@app.route('/restart_service')
def restart_service():
cmd = "service v2ray restart"
commands.getoutput(cmd)
change_config("status", "on")
return "OK"
@app.route('/set_uuid',methods=['GET', 'POST'])
def set_uuid():
items = request.args.to_dict()
change_config("uuid",items['setuuid'])
gen_server()
gen_client()
restart_service()
return "OK"
@app.route('/set_tls',methods=['GET', 'POST'])
def set_tls():
items = request.args.to_dict()
if(items['action'] == "off"):
change_config('tls','off')
change_config('domain','none')
else:
change_config("tls","on")
change_config("domain",items['domain'])
gen_server()
gen_client()
restart_service()
return "OK"
@app.route('/set_mux',methods=['GET', 'POST'])
def set_mux():
items = request.args.to_dict()
change_config("mux",items['action'])
gen_client()
return "OK"
@app.route('/set_port',methods=['GET', 'POST'])
def set_port():
items = request.args.to_dict()
change_config("port",items['setport'])
gen_server()
gen_client()
restart_service()
open_port(items['setport'])
return "OK"
@app.route('/set_encrypt',methods=['GET', 'POST'])
def set_encrypt():
items = request.args.to_dict()
encrypt = str(items['encrypt'])
if encrypt == "1":
change_config("encrypt","auto")
elif encrypt == "2":
change_config("encrypt", "aes-128-cfb")
elif encrypt == "3":
change_config("encrypt", "aes-128-gcm")
elif encrypt == "4":
change_config("encrypt", "chacha20-poly1305")
else:
change_config("encrypt", "none")
gen_server()
gen_client()
restart_service()
return "OK"
@app.route('/set_trans',methods=['GET', 'POST'])
def set_trans():
items = request.args.to_dict()
trans = str(items['trans'])
if trans == "1":
change_config("trans","tcp")
change_config("domain", "none")
elif trans == "2":
change_config("trans", "websocket")
change_config("domain", items['domain'])
elif trans == "3":
change_config("trans", "mkcp")
change_config("domain", "none")
elif trans == "4":
change_config("trans", "mkcp-srtp")
change_config("domain", "none")
elif trans == "5":
change_config("trans", "mkcp-utp")
change_config("domain", "none")
else:
change_config("trans","mkcp-wechat")
change_config("domain", "none")
gen_server()
gen_client()
restart_service()
return "OK"
@app.route('/')
@app.route('/index.html')
def index_page():
return render_template("index.html")
@app.route('/app.html')
def app_page():
return render_template("app.html")
@app.route('/log.html')
def log_page():
return render_template("log.html")
@app.route('/config.html')
def config_page():
return render_template("config.html")
@app.route('/get_info')
def get_info():
v2ray_config = open("v2ray.config","r")
json_content = json.loads(v2ray_config.read())
if(json_content['domain'] != "none"):
json_content['ip'] = json_content['domain']
json_content['status'] = get_status()
json_dump = json.dumps(json_content)
v2ray_config.close()
return json_dump
@app.route('/get_log')
def get_log():
file = open('/var/log/v2ray/access.log',"r")
content = file.read().split("\n")
min_length = min(20,len(content))
content = content[-min_length:]
string = ""
for i in range(min_length):
string = string + content[i] + "<br>"
return string
@app.route('/gen_ssl',methods=['GET', 'POST'])
def gen_ssl():
items = request.args.to_dict()
domain = str(items['domain'])
stop_service()
cmd = "bash /root/.acme.sh/acme.sh --issue -d {0} --standalone".format(domain)
check_acme = """ps -ef | grep "acme.sh" | grep -v grep | awk '{print $2}'"""
commands.getoutput(cmd)
acme_status = commands.getoutput(check_acme)
while acme_status != "":
acme_status = commands.getoutput(check_acme)
result = os.path.exists("/root/.acme.sh/{0}/fullchain.cer".format(domain))
start_service()
if result == True:
return "True"
else:
return "False"
data_file = open("v2ray.config", "r")
data = json.loads(data_file.read())
data_file.close()
if data['tls'] == "on" and panel_config['use_ssl'] == "on":
key_file = "/root/.acme.sh/{0}/{0}.key".format(data['domain'],data['domain'])
crt_file = "/root/.acme.sh/{0}/fullchain.cer".format(data['domain'])
app.run(host='0.0.0.0', port=panel_config['port'],ssl_context=(crt_file, key_file))
else:
app.run(host='0.0.0.0', port=panel_config['port'])