This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·443 lines (345 loc) · 12 KB
/
run.py
File metadata and controls
executable file
·443 lines (345 loc) · 12 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env python3
"""
Litecord
Copyright (C) 2018-2021 Luna Mendes and Litecord Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import asyncio
import ssl
import sys
from typing import Any
import asyncpg
import logbook
import logging
import websockets
from quart import Response, jsonify
from logbook import StreamHandler, Logger
from logbook.compat import redirect_logging
from litecord.blueprints import (
gateway,
auth,
users,
guilds,
channels,
webhooks,
misc,
voice,
invites,
relationships,
dms,
icons,
static,
attachments,
dm_channels,
read_states,
stickers,
applications,
store,
)
# those blueprints are separated from the "main" ones
# for code readability if people want to dig through
# the codebase.
from litecord.blueprints.guild import (
guild_roles,
guild_members,
guild_channels,
guild_mod,
guild_emoji,
)
from litecord.blueprints.channel import (
channel_messages,
channel_reactions,
channel_pins,
)
from litecord.blueprints.user import user_settings, user_billing, fake_store
from litecord.blueprints.user.billing_job import payment_job
from litecord.blueprints.admin_api import (
voice as voice_admin,
guilds as guilds_admin,
users as users_admin,
channels as channels_admin,
info as info_admin,
instance_invites,
)
from litecord.blueprints.admin_api.voice import guild_region_check
from litecord.ratelimits.handler import ratelimit_handler
from litecord.ratelimits.bucket import RatelimitBucket
from litecord.errors import BadRequest, LitecordError
from litecord.gateway.state_manager import StateManager
from litecord.storage import Storage
from litecord.user_storage import UserStorage
from litecord.dispatcher import EventDispatcher
from litecord.presence import PresenceManager
from litecord.images import IconManager
from litecord.jobs import JobManager
from litecord.voice.manager import VoiceManager
from litecord.guild_memory_store import GuildMemoryStore
from litecord.pubsub.lazy_guild import LazyGuildManager
from litecord.gateway.gateway import websocket_handler
from litecord.json import LitecordJSONProvider
from litecord.typing_hax import LitecordApp, request
# == HACKY PATCH ==
# this MUST be removed once Hypercorn gets py3.10 support.
from asyncio import start_server as _start_server
asyncio.start_server = lambda *args, loop=None, **kwargs: _start_server(*args, **kwargs)
# setup logbook
handler = StreamHandler(sys.stdout, level=logbook.INFO)
handler.push_application()
log = Logger("litecord.boot")
redirect_logging()
def make_app():
app = LitecordApp(__name__)
if app.is_debug:
log.info("on debug")
handler.level = logbook.DEBUG
app.logger.level = logbook.DEBUG
# always keep websockets on INFO
logging.getLogger("websockets").setLevel(logbook.INFO)
# use our custom json encoder for custom data types
# do not move this anywhere else
json_provider_class = LitecordJSONProvider
return app
PREFIXES = ("/api", "/api/v5", "/api/v6", "/api/v7", "/api/v8", "/api/v9", "/api/v10")
def set_blueprints(app_: LitecordApp):
"""Set the blueprints for a given app instance"""
bps = {
gateway: None,
auth: "/auth",
users: "/users",
user_settings: "/users",
user_billing: "/users",
relationships: "/users",
guilds: "/guilds",
guild_roles: "/guilds",
guild_members: "/guilds",
guild_channels: "/guilds",
guild_mod: "/guilds",
guild_emoji: "/guilds",
channels: "/channels",
channel_messages: "/channels",
channel_reactions: "/channels",
channel_pins: "/channels",
webhooks: None,
misc: None,
voice: "/voice",
invites: None,
dms: "/users",
dm_channels: "/channels",
fake_store: None,
icons: -1,
attachments: -1,
static: -1,
info_admin: "/admin",
voice_admin: "/admin/voice",
guilds_admin: "/admin/guilds",
users_admin: "/admin/users",
channels_admin: "/admin/channels",
instance_invites: "/admin/instance-invites",
read_states: "",
stickers: "",
applications: "/applications",
store: "/store",
}
for bp, suffix in bps.items():
for prefix in PREFIXES:
url_prefix = f'{prefix}{suffix or ""}'
if suffix == -1:
url_prefix = ""
app_.register_blueprint(bp, url_prefix=url_prefix)
app = make_app()
set_blueprints(app)
@app.before_request
async def app_before_request():
"""Functions to call before the request actually
takes place."""
try:
if not request.url_rule:
raise ValueError
request.discord_api_version = int(request.url_rule.rule.split("/api/v")[1].split("/")[0])
except Exception: # Default to 5 for ancient clients
request.discord_api_version = 5
finally:
# check if api version is smaller than 5 or bigger than 10
if 10 < request.discord_api_version < 5:
raise BadRequest(50041)
await ratelimit_handler()
@app.after_request
async def app_after_request(resp: Response):
"""Handle CORS headers."""
origin = request.headers.get("Origin", "*")
resp.headers["Access-Control-Allow-Origin"] = origin
resp.headers["Access-Control-Allow-Headers"] = (
"*, X-Super-Properties, "
"X-Fingerprint, "
"X-Context-Properties, "
"X-Failed-Requests, "
"X-Debug-Options, "
"Content-Type, "
"Authorization, "
"Origin, "
"If-None-Match"
)
resp.headers["Access-Control-Allow-Methods"] = resp.headers.get("allow", "*")
return resp
def _set_rtl_reset(bucket: RatelimitBucket, resp: Response):
reset = bucket._window + bucket.second
precision = request.headers.get("x-ratelimit-precision", "millisecond")
if precision == "second":
resp.headers["X-RateLimit-Reset"] = str(round(reset))
elif precision == "millisecond":
resp.headers["X-RateLimit-Reset"] = str(reset)
else:
resp.headers["X-RateLimit-Reset"] = str(reset)
@app.after_request
async def app_set_ratelimit_headers(resp: Response):
"""Set the specific ratelimit headers."""
try:
bucket = request.bucket
if bucket is None:
return resp
resp.headers["X-RateLimit-Limit"] = str(bucket.requests)
resp.headers["X-RateLimit-Remaining"] = str(bucket._tokens)
resp.headers["X-RateLimit-Global"] = str(request.bucket_global).lower()
_set_rtl_reset(bucket, resp)
# only add Retry-After if we actually hit a ratelimit
retry_after = request.retry_after
if request.retry_after:
resp.headers["Retry-After"] = str(retry_after)
except AttributeError:
pass
return resp
async def init_app_db(app_: LitecordApp):
"""Connect to databases.
Also spawns the job scheduler.
"""
log.info("db connect")
pool = await asyncpg.create_pool(**app.config["POSTGRES"])
assert pool is not None
app_.db = pool
app_.sched = JobManager(context_func=app.app_context)
app.init_managers()
async def api_index(app_: LitecordApp):
to_find = {}
found = []
with open("discord_endpoints.txt") as fd:
for line in fd.readlines():
components = line.split(" ")
components = list(filter(bool, components))
name, method, path = components
path = f"/api/v6{path.strip()}"
method = method.strip()
to_find[(path, method)] = name
for rule in app_.url_map._rules:
path = rule.rule
# convert the path to the discord_endpoints file's style
path = path.replace("_", ".")
path = path.replace("<", "{")
path = path.replace(">", "}")
path = path.replace("int:", "")
# change our parameters into user.id
path = path.replace("member.id", "user.id")
path = path.replace("banned.id", "user.id")
path = path.replace("target.id", "user.id")
path = path.replace("other.id", "user.id")
path = path.replace("peer.id", "user.id")
methods = rule.methods
if not methods:
continue
for method in methods:
pathname = to_find.get((path, method))
if pathname:
found.append(pathname)
found = set(found)
api = set(to_find.values())
missing = api - found
percentage = (len(found) / len(api)) * 100
percentage = round(percentage, 2)
log.debug(
"API compliance: {} out of {} ({} missing), {}% compliant",
len(found),
len(api),
len(missing),
percentage,
)
log.debug("missing: {}", missing)
async def post_app_start(app_: LitecordApp):
# we'll need to start a billing job
app_.sched.spawn(payment_job())
app_.sched.spawn(api_index(app_))
app_.sched.spawn(guild_region_check())
def start_websocket(host, port, ws_handler) -> asyncio.Future:
"""Start a websocket. Returns the websocket future"""
log.info(f"starting websocket at {host} {port}")
async def _wrapper(ws, url):
# We wrap the main websocket_handler
# so we can pass quart's app object.
await ws_handler(app, ws, url)
kwargs = {"ws_handler": _wrapper, "host": host, "port": port}
tls_cert_path = getattr(app.config, "WEBSOCKET_TLS_CERT_PATH", None)
tls_key_path = getattr(app.config, "WEBSOCKET_TLS_CERT_PATH", None)
if tls_cert_path:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(tls_cert_path, tls_key_path)
kwargs["ssl"] = context
return websockets.serve(**kwargs)
@app.before_serving
async def app_before_serving():
"""Callback for variable setup.
Also sets up the websocket handlers.
"""
log.info("opening db")
await init_app_db(app)
await post_app_start(app)
# start gateway websocket
# voice websocket is handled by the voice server
ws_fut = start_websocket(
app.config["WS_HOST"], app.config["WS_PORT"], websocket_handler
)
await ws_fut
@app.after_serving
async def app_after_serving():
"""Shutdown tasks for the server."""
# first close all clients, then close db
tasks = app.state_manager.gen_close_tasks()
if tasks:
await asyncio.wait(tasks)
app.state_manager.close()
app.sched.close()
log.info("closing db")
await app.db.close()
@app.errorhandler(LitecordError)
async def handle_litecord_err(error: Exception):
assert isinstance(error, LitecordError)
try:
ejson = error.json
except IndexError:
ejson = {}
log.warning("error: {} {!r}", error.status_code, error.message)
data = {"code": error.error_code, "message": error.message, **ejson}
if data["code"] == -1:
data.pop("code")
return jsonify(data), error.status_code
@app.errorhandler(404)
def handle_404(_):
if request.path.startswith("/api"):
return jsonify({"message": "404: Not Found", "code": 0}), 404
return "Not Found", 404
@app.errorhandler(405)
def handle_405(_):
return jsonify({"message": "405: Method Not Allowed", "code": 0}), 405
@app.errorhandler(413)
def handle_413(_):
return jsonify({"message": "Request entity too large", "code": 40005}), 413
@app.errorhandler(500)
async def handle_500(_):
return jsonify({"message": "500: Internal Server Error", "code": 0}), 500
if __name__ == "__main__":
app.run()