This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
165 lines (142 loc) · 4.58 KB
/
app.py
File metadata and controls
165 lines (142 loc) · 4.58 KB
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
#!/usr/bin/env python3
from flask import Flask, send_from_directory, request
from functools import wraps
from werkzeug import secure_filename
from tempfile import mkdtemp
import os, psutil, json, shutil, zipfile
HOME = os.path.expanduser('~')
DOWNLOAD_FOLDER = HOME + "/download"
VERSION_FILE = DOWNLOAD_FOLDER + "/latest-version.json"
USER_TOKEN = "qzuxmrLGwDs3KPKX5V5KyA=="
ADMIN_TOKEN = "o4S9qPk284HLuC8mx8qz2rtJNJFAX2Mnqg=="
FILE_EXCEPTIONS = ["testfolder"]
app = Flask(__name__)
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
givenToken = None
if 'x-access-token' in request.headers:
givenToken = request.headers['x-access-token']
if not givenToken == USER_TOKEN:
return 'Unauthorized Access!', 401
if not givenToken:
return 'Unauthorized Access!', 401
return f(*args, **kwargs)
return decorated
def admin_token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
givenToken = None
if 'x-access-token' in request.headers:
givenToken = request.headers['x-access-token']
if not givenToken == ADMIN_TOKEN:
return 'Unauthorized Access!', 401
if not givenToken:
return 'Unauthorized Access!', 401
return f(*args, **kwargs)
return decorated
@app.route("/version/<path:path>", methods=['GET'])
@token_required
def versionGet(path):
if path == "latest":
loc = VERSION_FILE
else:
loc = DOWNLOAD_FOLDER + f"/{path}/content.json"
try:
with open(loc) as version:
return json.loads(version.read())['version'], 200
except FileNotFoundError:
return "File with the version number was not found", 404
@app.route("/content/<path:path>", methods=['GET'])
@token_required
def contentGet(path):
if path == "latest":
loc = VERSION_FILE
else:
loc = DOWNLOAD_FOLDER + f"/{path}/content.json"
try:
with open(loc) as version:
return str(json.loads(version.read())['content']), 200
except FileNotFoundError:
return "File with the version number was not found", 404
@app.route("/count/<path:path>", methods=['GET'])
@token_required
def countGet(path):
if path == "latest":
loc = VERSION_FILE
else:
loc = DOWNLOAD_FOLDER + f"/{path}/content.json"
try:
with open(loc) as version:
return str(json.loads(version.read())['count']), 200
except FileNotFoundError:
return "File with the version number was not found", 404
@app.route("/versionFile", methods=['GET'])
@token_required
def versionFileGet():
try:
with open(VERSION_FILE) as version:
return str(version.read()), 200
except FileNotFoundError:
return "File with the version number was not found", 404
@app.route("/packages/<path:path>", methods=['GET'])
@token_required
def packagesGet(path):
for exception in FILE_EXCEPTIONS:
if exception in path:
return "Access denied", 403
return send_from_directory(DOWNLOAD_FOLDER, path, as_attachment=True)
@app.route("/status", methods=['GET'])
def statusGet():
for proc in psutil.process_iter():
try:
if "java" in proc.name().lower():
return "online", 200
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return "offline", 418
# @app.route("/pushVersion", methods=['PUT'])
# @admin_token_required
# def pushVersionFile():
# try:
# message = request.form['version']
# with open(VERSION_FILE, "rw", encoding="UTF-8") as version:
# original = json.loads(version.read())
# original['version'] = message
# version.write(json.dumps(original))
# return {"success":200}, 200
# except:
# return {"error":304}, 304
@app.route('/pushUpdate', methods = ['POST'])
@admin_token_required
def pushUpdate():
if request.method == 'POST':
try:
f = request.files['file']
if not ".zip" in f.filename:
return {"Error: Content isn't a zip file":422}, 422
fileName=secure_filename(f.filename)
tmp = mkdtemp()
f.save(fileName)
with zipfile.ZipFile(fileName, 'r') as zip_ref:
zip_ref.extractall(tmp)
if os.path.isfile(tmp+"/latest-version.json"):
shutil.copyfile(tmp+"/latest-version.json", DOWNLOAD_FOLDER)
with open(tmp+"/latest-version.json", "r", encoding="UTF-8") as version:
original = json.loads(version.read())
latestVersion = DOWNLOAD_FOLDER+f"""/{original["version"]}"""
os.makedirs(latestVersion)
shutil.rmtree(tmp+"/latest-version.json")
else:
return {"Error: Zip didn't contain any version file":422}, 422
shutil.copytree(tmp,latestVersion)
shutil.rmtree(fileName)
shutil.rmtree(tmp)
return {"success":200}, 200
except:
return {"error":400}, 400
@app.route("/teapot", methods=["GET"])
def teapot():
return {"I'm a teapot",418}, 418
if __name__ == '__main__':
app.run(port=25566)