This repository was archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
389 lines (291 loc) · 14.7 KB
/
api.py
File metadata and controls
389 lines (291 loc) · 14.7 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#type: ignore
import json
import os
import shutil
import flask
import requests
import werkzeug
import authenticator
import tokens
import uuids
from mojang import API, errors
import urllib.request, urllib.error
from flask_cors import CORS
from ratelimit import limits
from ratelimit.exception import RateLimitException
import news
import psutil
import time
start_time = time.time()
class Server:
app = flask.Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 8 * 1000 * 1000 # The first number is the maximum upload size in megabyte
CORS(app)
CLOAKS_PLUS_URL = "https://server.cloaksplus.com"
PORT = 443
MAX_MODELS = 5
def __init__(self, debug):
self.DEBUG = debug
self.tokens = tokens.Tokens()
self.uuids = uuids.UUIDs()
self.api = API()
@self.app.route("/")
def home(): return flask.render_template("index-https.html", users=self.uuids.get_user_count(), uptime=int( ( time.time()-psutil.boot_time() ) / 60 / 60 ), uptime_program=int( ( time.time()-start_time ) / 60 / 60 ))
@self.app.route("/authenticate/server_code")
@limits(calls=5, period=120)
def authenticate_server_code(): return self.authenticate_server_code()
@self.app.route("/authenticate/ms_login")
@limits(calls=5, period=120)
def authenticate_microsoft(): return self.authenticate_microsoft()
@self.app.route("/authenticate/check_token")
@limits(calls=30, period=60)
def check_token(): return self.check_token()
@self.app.route('/account/set_cape', methods=['POST'])
@limits(calls=3, period=60)
def set_cape(): return self.set_cape()
@self.app.route('/account/get_cape')
@limits(calls=15, period=60)
def get_cape(): return self.get_cape()
@self.app.route('/account/upload_cosmetic', methods=['POST'])
@limits(calls=3, period=60)
def upload_cosmetic(): return self.upload_cosmetic()
@self.app.route('/account/delete_cosmetic')
@limits(calls=3, period=60)
def delete_cosmetic(): return self.delete_cosmetic()
@self.app.route('/account/get_config')
@limits(calls=15, period=60)
def get_config(): return self.get_config()
@self.app.route('/account/set_config', methods=['POST'])
@limits(calls=3, period=60)
def set_config(): return self.set_config()
@self.app.route("/account/get_cosmetic_list")
@limits(calls=15, period=60)
def get_cosmetic_list(): return self.get_cosmetic_list()
@self.app.route('/account/get_cosmetic_cfg')
@limits(calls=15, period=60)
def get_cosmetic_cfg(): return self.get_cosmetic_cfg()
@self.app.route('/account/get_cosmetic_texture')
@limits(calls=15, period=60)
def get_cosmetic_texture(): return self.get_cosmetic_texture()
@self.app.route("/news/get")
@limits(calls=15, period=60)
def get_news(): return self.get_news()
@self.app.route("/news/get_image")
@limits(calls=15, period=60)
def get_news_image(): return self.get_news_image()
@self.app.errorhandler(RateLimitException)
def rate_limited(error):
return {"status": "failure", "error": self.string(["api", "errors", "rate_limit"])}
@self.app.errorhandler(500)
def server_error(error):
return {"status": "failure", "error": self.string(["api", "errors", "500"])}
with open("english.json", "r+") as lang:
self.strings = json.loads(lang.read())
def string(self, path):
cur = self.strings
for seg in path:
cur = cur[seg]
return cur
def create_auth(self, uuid, username, source="Unknown"):
token = self.tokens.generate_token()
self.tokens.register_token(token, uuid, source)
self.uuids.register(uuid, username)
return token
def authenticate_server_code(self):
code = flask.request.headers.get("code")
username = flask.request.headers.get("username", "")
source = flask.request.headers.get("source", "Unknown")
try:
uuid = self.api.get_uuid(username)
except errors.NotFound:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_username"])}
except Exception as e:
return {"status": "failure", "error": str(e)}
if authenticator.verify_by_code(code, uuid):
return {"status": "success", "token": self.create_auth(uuid, username, source), "uuid": uuid, "username": username}
else:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_auth_code"])}
def authenticate_microsoft(self):
email = flask.request.headers.get("email")
password = flask.request.headers.get("password")
username = flask.request.headers.get("username", "")
source = flask.request.headers.get("source", "Unknown")
try:
uuid = self.api.get_uuid(username)
except errors.NotFound:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_username"])}
if authenticator.verify_by_ms_account(email, password, username):
return {"status": "success", "token": self.create_auth(uuid, username, source), "uuid": uuid, "username": username}
else:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_ms_credentials"])}
def check_token(self):
token = flask.request.headers.get("token")
uuid = flask.request.headers.get("uuid")
username = flask.request.headers.get("username")
self.uuids.register(uuid, username)
if self.tokens.verify(uuid, token):
return {"status": "success", "result": "valid"}
return {"status": "success", "result": "invalid"}
def set_cape(self):
capetype = flask.request.headers.get("capetype")
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
if capetype == "none":
try:
shutil.copy("static/capes/none.png", "static/capes/" + uuid.replace("-", "") + ".png")
except Exception as e:
return {"status": "failure", "error": e}
return {"status": "success"}
elif capetype == "cloaksplus":
username = self.uuids.get_username(uuid)
if username == False:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_username"])}
try:
r = requests.get(self.CLOAKS_PLUS_URL + "/capes/" + username + ".png")
with open("static/capes/" + uuid.replace("-", "") + ".png", "wb") as file:
file.write(r.content)
return {"status": "success"}
except urllib.error.HTTPError as e:
return {"status": "failure", "error": self.string(["api", "errors", "invalid_cloaksplus_username"])}
if 'file' not in flask.request.files:
return {"status": "failure", "error": self.string(["api", "errors", "no_file"])}
file = flask.request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '' or file.filename == None:
return {"status": "failure", "error": self.string(["api", "errors", "empty_file"])}
if file and file.filename.endswith(".png"):
filename = uuid.replace("-", "") + ".png"
file.save(os.path.join("static/capes/", filename))
return {"status": "success"}
def get_cape(self):
uuid = flask.request.headers.get("uuid", "")
file = flask.send_from_directory("static/capes", uuid + ".png")
if file.status_code == 404:
username = str(self.api.get_username(uuid))
file = flask.redirect(self.CLOAKS_PLUS_URL + "/capes/" + username + ".png", code=302)
return file
def get_cosmetic_cfg(self):
uuid = flask.request.headers.get("uuid", "")
model = flask.request.headers.get("model", "")
file = flask.send_from_directory("static/models/" + uuid + "/" + model, "model.cfg")
return file
def get_cosmetic_texture(self):
uuid = flask.request.headers.get("uuid", "")
model = flask.request.headers.get("model", "")
file = flask.send_from_directory("static/models/" + uuid + "/" + model, "texture.png")
return file
def get_news(self):
count = int(flask.request.headers.get("count", "0"))
return {"status": "success", "articles": news.get_news_articles(count)}
def get_news_image(self):
title = flask.request.headers.get("title", "")
file = flask.send_from_directory("news/" + title, "image.png")
return file
def get_config(self):
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
try:
if not os.path.exists("static/models/" + uuid):
os.mkdir("static/models/" + uuid)
if not os.path.exists("static/models/" + uuid + "/config.json"):
with open("static/models/" + uuid + "/config.json", "w+") as file:
file.write(json.dumps({}))
with open("static/models/" + uuid + "/config.json", "r+") as file:
return json.loads(file.read())
except Exception as e:
return {"status": "failure", "error": str(e)}
def set_config(self):
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
data = json.loads(flask.request.headers.get("config", "{}"))
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
try:
with open("static/models/" + uuid + "/config.json", "w+") as file:
file.write(json.dumps(data))
return {"status": "success"}
except Exception as e:
return {"status": "failure", "error": str(e)}
def get_cosmetic_list(self):
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
try:
if not os.path.exists("static/models/" + uuid):
os.mkdir("static/models/" + uuid)
with open("static/models/" + uuid + "/config.json", "w+") as config:
config.write("{}")
models = os.listdir("static/models/" + uuid)
models.remove("config.json")
return {"status": "success", "models": models}
except Exception as e:
return {"status": "failure", "error": str(e)}
def upload_cosmetic(self):
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
if 'model' not in flask.request.files or 'texture' not in flask.request.files:
return {"status": "failure", "error": self.string(["api", "errors", "no_model_files"])}
model = flask.request.files['model']
texture = flask.request.files['texture']
if model.filename == '' or model.filename == None:
return {"status": "failure", "error": self.string(["api", "errors", "empty_model_files"])}
model_name = flask.request.headers.get("model_name", default="".join(model.filename.split(".")[:-1]))
save_dir = "static/models/" + uuid.replace("-", "")
try: os.mkdir(save_dir)
except FileExistsError: pass
if len(os.listdir(save_dir)) > self.MAX_MODELS:
return {"status": "failure", "error": self.string(["api", "errors", "max_models"])}
save_dir += "/" + model_name
try: os.mkdir(save_dir)
except FileExistsError: pass
if model and model.filename.endswith(".cfg"):
model.save(os.path.join(save_dir, "model.cfg"))
texture.save(os.path.join(save_dir, "texture.png"))
data = {}
try:
with open("static/models/" + uuid + "/config.json", "r") as file:
data = json.loads(file.read())
except FileNotFoundError:
pass
with open("static/models/" + uuid + "/config.json", "w+") as file:
data[model_name] = True
file.write(json.dumps(data))
return {"status": "success"}
def delete_cosmetic(self):
token = flask.request.headers.get("token", "")
uuid = flask.request.headers.get("uuid", "")
model = flask.request.headers.get("model", "")
if not self.tokens.verify(uuid, token):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_token"])}
if model == "":
return {"status": "failure", "error": self.string(["api", "errors", "model_not_specified"])}
save_dir = "static/models/" + uuid.replace("-", "")
if not os.path.exists(save_dir) or not os.path.exists(save_dir + "/" + model):
return {"status": "failure", "error": self.string(["api", "errors", "invalid_model"])}
try:
shutil.rmtree(save_dir + "/" + model)
data = {}
with open("static/models/" + uuid + "/config.json", "r+") as file:
data = json.loads(file.read())
if model in data.keys():
del data[model]
with open("static/models/" + uuid + "/config.json", "w+") as file:
file.write(json.dumps(data))
except Exception as e:
print(e)
return {"status": "failure", "error": self.string(["api", "errors", "unknown", "deletion"])}
return {"status": "success"}
def start(self):
# We need to run the API over HTTPS because I don't want to make end-to-end encryption from scratch :)
if self.DEBUG:
self.app.run("0.0.0.0", self.PORT, ssl_context=("ssl/domain.cert.pem", "ssl/private.key.pem"))
else:
raise Exception("API cannot be run in a non-debug environment. You must run with Gunicorn.")