forked from cgrok/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
264 lines (218 loc) · 7.86 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
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
import os
from functools import wraps
from urllib.parse import urlencode
import asyncio
from sanic import Sanic
from sanic.response import html, text, redirect, HTTPResponse
from sanic.exceptions import abort, NotFound
from sanic_session import InMemorySessionInterface
from motor.motor_asyncio import AsyncIOMotorClient
from jinja2 import Environment, PackageLoader
import discord
import aiohttp
import ujson
from utils.user import User
from utils.utils import get_stack_variable, validate_github_payload, json
with open('data/config.json') as f:
CONFIG = ujson.loads(f.read())
dev_mode = CONFIG.get('dev_mode', False)
domain = '127.0.0.1:8000' if dev_mode else 'botsettings.tk'
DEVELOPERS = [
325012556940836864,
271747354472873994,
126321762483830785,
180314310298304512
]
OAUTH2_CLIENT_ID = CONFIG.get('client_id')
OAUTH2_CLIENT_SECRET = CONFIG.get('client_secret')
OAUTH2_REDIRECT_URI = f'http://{domain}/callback'
API_BASE_URL = 'https://discordapp.com/api'
AUTHORIZATION_BASE_URL = API_BASE_URL + '/oauth2/authorize'
TOKEN_URL = API_BASE_URL + '/oauth2/token'
app = Sanic('dash')
app.static('/static', './static')
env = Environment(loader=PackageLoader('app', 'templates'))
def render_template(name, *args, **kwargs):
template = env.get_template(name+'.html')
request = get_stack_variable('request')
user = None
if request['session'].get('logged_in'):
user = get_user(request)
kwargs['request'] = request
kwargs['session'] = request['session']
kwargs['user'] = user
kwargs.update(globals())
return html(template.render(*args, **kwargs))
####################################
# Server backed session middleware #
####################################
session_interface = InMemorySessionInterface(domain=None if dev_mode else domain)
@app.middleware('request')
async def add_session_to_request(request):
await session_interface.open(request)
@app.middleware('response')
async def save_session(request, response):
await session_interface.save(request, response)
async def validate_token(request):
exists = await app.db.admin.find_one({'token': request.token})
return exists is not None
#############################
# Authentication decorators #
#############################
def authrequired(admin=False):
def decorator(func):
@wraps(func)
async def wrapper(request, *args, **kwargs):
valid_token = await validate_token(request)
if valid_token:
return await func(request, *args, **kwargs)
if admin is False and not request['session'].get('logged_in'):
return redirect(app.url_for('login'))
else:
return await func(request, *args, **kwargs)
if admin is True and not valid_token:
return error('Invalid authorization token provided.')
return wrapper
return decorator
def bot_manager():
def decorator(func):
@wraps(func)
async def wrapper(request, code_name, section):
bot = await app.db.metadata.find_one({'code_name': code_name})
if bot is None:
abort(404)
bot.pop('_id')
bot.pop('bot_token', None)
user = get_user(request)
id = user.id
allowed = bot.get('allowed_users', [])
if id in allowed or 'everyone' in allowed or id == bot['owner_id'] or id in DEVELOPERS:
return await func(request, code_name, section, bot, user)
return text('you dont have acces boi')
return wrapper
return decorator
####################
# Server init/stop #
####################
@app.listener('before_server_start')
async def init(app, loop):
'''Initialize app config, database and send the status discord webhook payload.'''
app.session = aiohttp.ClientSession(loop=loop)
app.password = CONFIG.get('password')
app.webhook_url = CONFIG.get('webhook_url')
app.log_url = CONFIG.get('log_url')
mongo_client = AsyncIOMotorClient(CONFIG.get('mongo_url'))
app.db = mongo_client.dash
if app.webhook_url:
await app.session.post(
app.webhook_url,
json=format_embed('deploy')
)
@app.listener('after_server_stop')
async def aexit(app, loop):
'''Close the aiohttp client session'''
app.session.close()
#############
# Endpoints #
#############
@app.get('/')
async def index(request):
return render_template('index')
@app.get('/login')
async def login(request):
if request['session'].get('logged_in'):
request['session'].clear()
data = {
"scope": "identify",
"client_id": OAUTH2_CLIENT_ID,
"response_type": "code",
"redirect_uri": OAUTH2_REDIRECT_URI
}
return redirect(f"{AUTHORIZATION_BASE_URL}?{urlencode(data)}")
async def fetch_token(code):
data = {
"code": code,
"grant_type": "authorization_code",
"redirect_uri": OAUTH2_REDIRECT_URI,
"client_id": OAUTH2_CLIENT_ID,
"client_secret": OAUTH2_CLIENT_SECRET
}
async with app.session.post(f"{TOKEN_URL}?{urlencode(data)}") as resp:
json = await resp.json()
return json
async def get_user_info(token):
headers = {"Authorization": f"Bearer {token}"}
async with app.session.get(f"{API_BASE_URL}/users/@me", headers=headers) as resp:
return await resp.json()
def get_user(request):
data = request['session']['user']
return User(data=data)
@app.get('/callback')
async def oauth_callback(request):
code = request.raw_args.get('code')
token = await fetch_token(code)
access_token = token.get('access_token')
if access_token is not None:
request['session']['access_token'] = access_token
request['session']['logged_in'] = True
request['session']['user'] = await get_user_info(access_token)
return redirect(app.url_for('select_bot'))
return redirect(app.url_for('login'))
@app.get('/logout')
@authrequired()
async def logout(request):
request['session'].clear()
return redirect(app.url_for('index'))
@app.get('/bots')
@authrequired()
async def select_bot(request):
user = get_user(request)
bots = []
query = {
'$or': [
{'owner_id': user.id},
{'allowed_users': user.id},
{'allowed_users': 'everyone'}
]
}
if user.id in DEVELOPERS:
query = {}
async for bot in app.db.metadata.find(query):
bots.append(bot)
return render_template('select-bot', user=user, bots=bots)
@app.get('/bots/<code_name>/<section>')
@authrequired()
@bot_manager()
async def dashboard(request, code_name, section, bot, user):
'''Serve dashboard pages.'''
if f'dash-{section}.html' not in os.listdir('templates'):
abort(404)
return render_template(f'dash-{section}', bot=bot, user=user)
@app.post('/hooks/github')
async def upgrade(request):
if not validate_github_payload(request):
return text('fuck off', 401) # not sent by github
if any('[deploy]' in c['message'] for c in request.json['commits']):
await app.session.post(app.webhook_url, json=format_embed('update'))
app.loop.create_task(restart_later())
return json({'success': True})
def format_embed(event):
em = discord.Embed()
if event == 'update':
em.title = '[Info] Website update and restart started.'
em.color = discord.Color.blue()
elif event == 'deploy':
em.title = '[Success] Website successfully deployed.'
em.color = discord.Color.green()
return {
'embeds': [em.to_dict()]
}
async def restart_later():
app.session.close()
command = 'sh ../dash.sh'
os.system(f'echo {app.password}|sudo -S {command}')
@app.exception(NotFound)
async def handle_not_found(request, exception):
return text("Not found.")
if __name__ == '__main__':
app.run() if dev_mode else app.run(host='botsettings.tk', port=80)