-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
85 lines (67 loc) · 2.04 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
import os
from flask import Flask
from flask import request
from flask import render_template
import string
import random
import sqlite3
TOKEN = os.getenv(
"SECRET_TOKEN", "".join(random.choices(string.ascii_letters + string.digits, k=25))
)
print("++++++++++++++ TOKEN ++++++++++++++")
print(" {} ".format(TOKEN))
print("+++++++++++++++++++++++++++++++++++")
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS command (id INTEGER PRIMARY KEY AUTOINCREMENT, command text)"
)
connection.commit()
connection.close()
app = Flask(__name__)
@app.route("/")
def ui():
return render_template("index.html")
@app.route("/command", methods=["POST"])
def command():
if "token" not in request.headers:
return {"message": "no"}, 401
else:
if request.headers["token"] != TOKEN:
return {"message": "no"}, 401
data = request.get_json()
if data.get("type") == "python":
cmd = f'>>> {data["command"]}'
else:
cmd = data["command"]
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("INSERT INTO command VALUES (NULL, ?)", (cmd,))
connection.commit()
connection.close()
return {"message": "ok"}
@app.route("/commands", methods=["GET"])
def get_commands():
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("SELECT command FROM command")
rows = cursor.fetchall()
connection.commit()
connection.close()
data = []
for row in rows:
data.append(row[0])
return {"commands": data}
@app.route("/reset", methods=["GET"])
def reset():
if "token" not in request.headers:
return {"message": "no"}, 401
else:
if request.headers["token"] != TOKEN:
return {"message": "no"}, 401
connection = sqlite3.connect("database.db")
cursor = connection.cursor()
cursor.execute("DELETE FROM command")
connection.commit()
connection.close()
return {"message": "ok"}