-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.py
99 lines (74 loc) · 2.13 KB
/
main.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
from flask import *
import os
import psutil
from config import creds, vpn
from hashlib import sha256
import logging
username = creds["username"]
password = creds["password"]
log = logging.getLogger("werkzeug")
log.setLevel(logging.ERROR)
app = Flask(
f"{vpn.vpnName} Admin",
static_folder=os.path.abspath("frontend/build/static"),
template_folder=os.path.abspath("frontend/build"),
)
app.logger.disabled = True
log.disabled = True
def isAdmin(reqArgs):
adminUserName = reqArgs.get("username")
adminPassWord = reqArgs.get("password")
hashedInput = sha256(adminPassWord.encode('utf-8')).hexdigest()
return adminUserName == username and hashedInput == password
@app.route("/")
def homePage():
return render_template("index.html")
@app.route("/type")
def vpnType():
return {"type": vpn.vpnName}
@app.route("/login")
def loginCheck():
return {
"success": isAdmin(request.args),
"memory": max(
(psutil.swap_memory().used + psutil.virtual_memory().used)
/ (psutil.swap_memory().total + psutil.virtual_memory().total)
* 100,
5,
),
"cpu": max(psutil.cpu_percent(), 5),
}
@app.route("/list")
def listUsers():
if isAdmin(request.args):
return vpn.listUsers()
else:
return []
@app.route("/create/<path:name>")
def createUser(name):
if isAdmin(request.args):
vpn.createUser(name)
return {"success": True}
else:
return {"success": False}
@app.route("/remove/<path:name>")
def removeUser(name):
if isAdmin(request.args):
vpn.removeUser(name)
return {"success": True}
else:
return {"success": False}
@app.route("/getConfig/<path:name>")
def getConfig(name):
if isAdmin(request.args):
return Response(
vpn.getConfig(name),
mimetype=f"text/x-{vpn.vpnExtension}",
headers={
"Content-Disposition": f"attachment;filename={name}.{vpn.vpnExtension}"
},
)
else:
return {"error": "Incorrect admin credentials!"}
if __name__ == "__main__":
app.run(port=5000)