This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
339 lines (262 loc) · 9.01 KB
/
index.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
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
import aiohttp
import asyncio
import json
import secrets
import time
from functools import wraps
from postgreslite import PostgresLite
from datetime import timedelta
from quart import Quart, request, abort, render_template, redirect
from utils import default
from utils.api import APIHandler
with open("./config.json", "r", encoding="utf8") as f:
config = json.load(f)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
app = Quart(__name__)
db = PostgresLite("./storage.db", loop=loop).connect_async()
api = APIHandler(config, db)
async def remove_expired_blacklist():
""" Remove expired blacklist entries """
while True:
await db.execute(
"DELETE FROM blacklist WHERE expires_at < ?",
default.legacy_utcnow()
)
await asyncio.sleep(30) # Check every half minute
async def remove_expired_states():
while True:
await db.execute(
"DELETE FROM states WHERE expires_at < ?",
default.legacy_utcnow()
)
await asyncio.sleep(5) # Check every 5 seconds
async def generate_state_key(integration_type: int) -> str:
random_key = f"{secrets.token_hex(8)}-{int(time.time())}"
await db.execute(
"INSERT INTO states (key, integration_type, expires_at) VALUES (?, ?, ?)",
random_key,
integration_type,
default.legacy_utcnow() + timedelta(minutes=1)
)
return random_key
async def invalidate_state_key(key: str):
await db.execute(
"DELETE FROM states WHERE key=?",
key
)
async def _discord_api_request(path: str, data: dict):
try:
session = aiohttp.ClientSession()
async with session.post(f"{config['api_endpoint']}{path}", data=data) as s:
r = await s.json()
await session.close()
except Exception:
abort(500, "Server failed to connect with discord.com API")
return r
async def exchange_code(code: str):
""" Sends request to discord.com to grant invite request """
r = await _discord_api_request("/oauth2/token", {
"client_id": config["client_id"],
"client_secret": config["client_secret"],
"grant_type": "authorization_code",
"redirect_uri": config["redirect_uri"],
"scope": config["scopes"],
"code": code
})
if "error" in r:
abort(400, r["error_description"])
return r
def api_validator(f):
""" Validate if user can use invite API """
@wraps(f)
async def decorator(*args, **kwargs):
auth = request.headers.get("Authorization", None)
content = request.headers.get("Content-Type", None)
if content != "application/json":
abort(400, "Invalid Content-Type. Must be application/json")
if not auth:
abort(400, "Missing Authorization")
if auth != config["backend_api_token"]:
abort(403, "Access denied...")
return await f(*args, **kwargs)
return decorator
@app.route("/")
async def index():
return await render_template(
"index.html",
support=config["support_server"],
permissions=config.get("permissions", 8),
oauth_url=(
f"https://discord.com/oauth2/authorize?client_id={config['client_id']}"
f"&scope=applications.commands&redirect_uri={config['redirect_uri']}"
"&prompt=consent&response_type=code"
)
)
@app.route("/oauth2/guild")
async def oauth2_guild():
state = await generate_state_key(0)
permissions = request.args.get("permissions", 0)
return redirect(
f"https://discord.com/oauth2/authorize?client_id={config['client_id']}"
f"&scope=bot&redirect_uri={config['redirect_uri']}"
f"&prompt=consent&response_type=code&permissions={permissions}&state={state}"
)
@app.route("/oauth2/user")
async def oauth2_user():
state = await generate_state_key(1)
return redirect(
f"https://discord.com/oauth2/authorize?client_id={config['client_id']}"
f"&scope=applications.commands&redirect_uri={config['redirect_uri']}"
f"&prompt=consent&response_type=code&integration_type=1&state={state}"
)
@app.route("/success")
async def success_guild():
guild_name = request.args.get("guild_name")
guild_id = request.args.get("guild_id")
guild_icon = request.args.get("guild_icon")
if not guild_name or not guild_id:
return abort(400)
icon_type = "png"
if guild_icon:
icon_type = "gif" if guild_icon.startswith("a_") else "png"
return await render_template(
"success.html",
name=guild_name, id=guild_id, icon=guild_icon, icon_type=icon_type,
support=config["support_server"], website=config["bot_website"]
)
@app.route("/success_user")
async def success_user():
return await render_template(
"success_user.html",
support=config["support_server"],
website=config["bot_website"]
)
@app.route("/error")
async def error():
guild_id = request.args.get("guild_id", None)
code = request.args.get("code", None)
if not code:
abort(400, "Missing parameter code needed for error page")
return await render_template(
"error.html",
code=code,
guild_id=guild_id or 1337,
support=config["support_server"]
)
@app.route("/callback")
async def callback_discord():
state = request.args.get("state", None)
if not state:
abort(401, "No state granted...")
data_state = await db.fetchrow(
"SELECT * FROM states WHERE key=?",
state
)
if not data_state:
return redirect("/error?code=IS")
# Instantly invalidate the state key
await invalidate_state_key(state)
code = request.args.get("code", None)
guild_id = request.args.get("guild_id", None)
if not code:
abort(401, "No code granted...")
if data_state["integration_type"] == 1:
if guild_id is not None:
return redirect("/error?code=WS")
r = await exchange_code(code) # Tell Discord to grant user bot access
return redirect("/success_user")
if not guild_id:
return redirect("/error?code=WS")
data_whitelist = await db.fetchrow(
"SELECT * FROM whitelist WHERE guild_id=?",
int(guild_id)
)
data_blacklist = await db.fetchrow(
"SELECT * FROM blacklist WHERE guild_id=?",
int(guild_id)
)
error_site = f"/error?guild_id={guild_id}"
if data_blacklist:
return redirect(f"{error_site}&code=BL")
if not data_whitelist:
return redirect(f"{error_site}&code=NF")
if data_whitelist["invited"]:
return redirect(f"{error_site}&code=DUP")
r = await exchange_code(code) # Tell Discord to grant the bot
await db.execute(
"UPDATE whitelist SET invited=true WHERE guild_id=?",
guild_id # Guild can no longer invite bot
)
add_guild_icon = (
f"&guild_icon={r['guild']['icon']}"
if r['guild']['icon'] else ""
)
return redirect(
f"/success?guild_name={r['guild']['name']}"
f"&guild_id={guild_id}{add_guild_icon}"
)
# API Routes
@app.route(
"/api/guilds/<int:guild_id>",
methods=["GET", "POST", "DELETE"]
)
@api_validator
async def api_guild_handler(guild_id: int):
api.discord_id_validator(guild_id, "guild_id")
match request.method:
case "GET":
return await api.api_guild_get(guild_id)
case "POST":
return await api.api_guild_post(guild_id)
case "DELETE":
return await api.api_guild_delete(guild_id)
case _:
abort(405, "Method not allowed")
@app.route("/api/bans", methods=["GET"])
@api_validator
async def api_list_bans():
return await api.api_guild_get_bans()
@app.route(
"/api/guilds/<int:guild_id>/bans",
methods=["PUT", "DELETE"]
)
@api_validator
async def api_guild_ban_handler(guild_id: int):
api.discord_id_validator(guild_id, "guild_id")
match request.method:
case "PUT":
return await api.api_guild_ban(guild_id)
case "DELETE":
return await api.api_guild_unban(guild_id)
case _:
abort(405, "Method not allowed")
@app.route(
"/api/guilds/<int:guild_id>/notes",
methods=["GET", "PUT", "DELETE"]
)
@api_validator
async def api_guild_note_handler(guild_id: int):
api.discord_id_validator(guild_id, "guild_id")
match request.method:
case "GET":
return await api.api_guild_get_notes(guild_id)
case "PUT":
return await api.api_guild_add_note(guild_id)
case "DELETE":
return await api.api_guild_delete_note(guild_id)
case _:
abort(405, "Method not allowed")
@app.errorhandler(Exception)
async def handle_exception(e):
status_code = 404
if hasattr(e, "status_code"):
status_code = e.status_code
return api.json_response(
e.name, e.description, status_code
)
# Run background tasks before boot
loop.create_task(remove_expired_blacklist())
loop.create_task(remove_expired_states())
# Run the app
app.run(port=config["port"], loop=loop)