This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
/
app.py
271 lines (210 loc) · 6.71 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask import Flask, render_template, request
from flask_basicauth import BasicAuth
from config_generator import * # noqa
with open("panel.config") as f:
panel_config = json.load(f)
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):
with open("v2ray.config") as f:
old_json = json.load(f)
old_json[str(config)] = str(value)
with open("v2ray.config", "w") as f:
json.dump(old_json, f, indent=2)
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_protocol', methods=['GET', 'POST'])
def set_protocol():
items = request.args.to_dict()
if items['protocol'] == "1":
change_config('protocol', 'vmess')
elif items['protocol'] == "2":
change_config('protocol', 'mtproto')
gen_server()
gen_client()
return "OK"
@app.route('/set_secret', methods=['GET', 'POST'])
def set_secret():
items = request.args.to_dict()
change_config('secret', items['secret'])
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():
with open("v2ray.config") as v2ray_config:
json_content = json.load(v2ray_config)
if (json_content['domain'] != "none"):
json_content['ip'] = json_content['domain']
json_content['status'] = get_status()
json_dump = json.dumps(json_content)
return json_dump
@app.route('/get_access_log')
def get_access_log():
with open('/var/log/v2ray/access.log') as f:
content = f.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('/get_error_log')
def get_error_log():
with open('/var/log/v2ray/error.log') as f:
content = f.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 is True:
return "True"
else:
return "False"
with open("v2ray.config") as f:
data = json.load(f)
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'])