forked from Vyxal/Vyxal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
168 lines (137 loc) · 4.4 KB
/
flask_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
from hashlib import sha256
from hmac import compare_digest
import multiprocessing
import os
import secrets
import shutil
import sys
from flask import Flask, render_template, request
from flask_cors import CORS
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) + "/.."
sys.path.insert(1, THIS_FOLDER)
from vyxal.main import execute_vyxal
app = Flask(__name__)
CORS(app)
FUNKY_PASSWORD_HASH = (
"411b514435eaffc4fc36b25b40347761af7cbf644c1e92e1fe190e6ebcf4b2d2"
)
shutil.rmtree("sessions", ignore_errors=True)
os.system("mkdir sessions")
sessions = {}
terminated = set()
@app.route("/", methods=("POST", "GET"))
def index():
session = secrets.token_hex(64)
sessions[session] = None
return render_template("index.html", session=session, codepage_info="No.")
@app.route("/session", methods=("POST", "GET"))
def new_session():
session = secrets.token_hex(64)
sessions[session] = None
return session
@app.route("/execute", methods=("POST",))
def execute():
print(sessions, request.json)
if request.json is None:
return {
"stdout": "",
"stderr": "",
}
flags = request.json["flags"] + "e"
code = request.json["code"].replace("\r", "")
input_list = request.json["inputs"].replace("\r", "")
header = request.json["header"].replace("\r", "")
footer = request.json["footer"].replace("\r", "")
session = request.json["session"]
print(code)
if session not in sessions:
return {
"stdout": "",
"stderr": "The session was invalid! You may need to reload your tab.",
}
shutil.rmtree(f"sessions/{session}", ignore_errors=True)
os.mkdir(f"sessions/{session}")
with open(f"sessions/{session}/.stdin", "w", encoding="utf-8") as f:
f.write(input_list)
with (
open(f"sessions/{session}/.stdin", "r", encoding="utf-8") as x,
open(f"sessions/{session}/.stdout", "w", encoding="utf-8") as y,
open(f"sessions/{session}/.stderr", "w", encoding="utf-8") as z,
):
manager = multiprocessing.Manager()
ret = manager.dict()
if "5" in flags:
time = 5
elif "T" in flags:
time = 60
elif "b" in flags:
time = 15
elif "B" in flags:
time = 30
else:
time = 10
ret[1] = ""
ret[2] = ""
fcode = (
(header and (header + "\n")) + code + (footer and ("\n" + footer))
)
sessions[session] = multiprocessing.Process(
target=execute_vyxal,
args=(fcode, flags, input_list, ret, True),
)
sessions[session].start()
sessions[session].join(time)
if session in terminated:
terminated.remove(session)
ret[2] += "\nSession terminated upon user request"
if sessions[session].is_alive():
sessions[session].kill()
if 2 in ret:
ret[2] += "\n" + f"Code timed out after {time} seconds"
y.write(ret[1])
z.write(ret[2])
with open(f"sessions/{session}/.stdout", "r", encoding="utf-8") as x, open(
f"sessions/{session}/.stderr", "r", encoding="utf-8"
) as y:
val = {"stdout": x.read(), "stderr": y.read()}
shutil.rmtree(f"sessions/{session}", ignore_errors=True)
return val
@app.route("/kill", methods=("POST",))
def kill():
if request.json is None:
return ""
session = request.json["session"]
if sessions.get(session) is None:
return ""
sessions[session].kill()
terminated.add(session)
return ""
@app.route("/update", methods=("POST",))
def update():
key = request.headers.get("X-funky-password", "")
if compare_digest(sha256(key.encode()).hexdigest(), FUNKY_PASSWORD_HASH):
if os.fork() == 0:
os.system("/home/Vyxal/mysite/funky_upgrade.sh")
os._exit()
return "updated successfully", 200
else:
return "incorrect or missing X-funky-password header", 403
@app.route("/version", methods=("GET",))
def version():
import subprocess
VERSION = (
subprocess.check_output(
[
"git",
"--git-dir",
"/home/Vyxal/mysite/.git",
"--work-tree",
"/home/Vyxal/mysite",
"rev-parse",
"HEAD",
]
)
.decode()
.strip()
)
return VERSION