-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
367 lines (279 loc) · 10.8 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
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
import datetime
import os
import queue
import traceback
from functools import wraps
import markdown
import requests
from flask import Flask, Response, abort, flash, redirect, render_template, request, send_from_directory, session
from flask_sqlalchemy import SQLAlchemy
from flask_sse import sse
from requests_oauthlib import OAuth2Session
app = Flask(__name__, static_url_path='/static')
app.debug = os.environ.get('DEBUG', '').lower() == 'true'
with open('secret', 'rb') as f:
app.config['SECRET_KEY'] = f.read()
if app.debug:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
else:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://admin:cybercyber@database/project'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config["REDIS_URL"] = "redis://redis"
app.register_blueprint(sse, url_prefix='/notifications')
db = SQLAlchemy(app)
MARKDOWN_EXTENSIONS = ['pymdownx.magiclink', 'pymdownx.tabbed', 'pymdownx.arithmatex', 'pymdownx.details',
'pymdownx.emoji', 'pymdownx.highlight', 'pymdownx.inlinehilite', 'pymdownx.keys',
'pymdownx.progressbar', 'pymdownx.smartsymbols', 'pymdownx.snippets', 'pymdownx.tabbed',
'pymdownx.tasklist', 'pymdownx.tilde']
### DB ###
class SSHKey(db.Model):
id = db.Column(db.BigInteger().with_variant(db.Integer, 'sqlite'), primary_key=True)
uid = db.Column(db.BigInteger, nullable=False)
name = db.Column(db.String(200), nullable=False)
key = db.Column(db.String(1000), nullable=False)
def __init__(self, uid, name, key):
self.uid = uid
self.name = name
self.key = key
class Wireguard(db.Model):
filename = db.Column(db.String(100), primary_key=True)
uid = db.Column(db.BigInteger, nullable=False)
def __init__(self, filename, uid):
self.filename = filename
self.uid = uid
class HomeMessage(db.Model):
id = db.Column(db.BigInteger().with_variant(db.Integer, 'sqlite'), primary_key=True)
message = db.Column(db.Text, nullable=False)
username = db.Column(db.Text, nullable=False)
def __init__(self, message, username):
self.message = message
self.username = username
class Notification(db.Model):
id = db.Column(db.BigInteger().with_variant(db.Integer, 'sqlite'), primary_key=True)
notification = db.Column(db.Text, nullable=False)
time = db.Column(db.DateTime, nullable=False)
def __init__(self, notification):
self.notification = notification
self.time = datetime.datetime.now()
### Discord OAUTH ###
OAUTH2_CLIENT_ID = os.environ['OAUTH2_CLIENT_ID']
OAUTH2_CLIENT_SECRET = os.environ['OAUTH2_CLIENT_SECRET']
OAUTH2_REDIRECT_URI = os.environ['OAUTH2_REDIRECT_URI']
API_BASE_URL = os.environ.get('API_BASE_URL', 'https://discordapp.com/api')
AUTHORIZATION_BASE_URL = API_BASE_URL + '/oauth2/authorize'
TOKEN_URL = API_BASE_URL + '/oauth2/token'
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'
def token_updater(token):
session['oauth2_token'] = token
def make_session(token=None, state=None, scope=None):
return OAuth2Session(
client_id=OAUTH2_CLIENT_ID,
token=token,
state=state,
scope=scope,
redirect_uri=OAUTH2_REDIRECT_URI,
auto_refresh_kwargs={
'client_id': OAUTH2_CLIENT_ID,
'client_secret': OAUTH2_CLIENT_SECRET,
},
auto_refresh_url=TOKEN_URL,
token_updater=token_updater)
#####################
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'uid' in session:
return f(*args, **kwargs)
else:
flash('You have to log in to perform this action', 'danger')
return redirect('/')
return wrap
@app.route('/')
def index():
if 'username' in session:
return redirect('/home')
return render_template('index.html')
@app.route('/login')
def login():
if 'oauth2_token' not in session:
scope = request.args.get(
'scope',
'identify guilds guilds.members.read')
discord = make_session(scope=scope.split(' '))
authorization_url, state = discord.authorization_url(AUTHORIZATION_BASE_URL)
session['oauth2_state'] = state
return redirect(authorization_url)
else:
discord = make_session(token=session.get('oauth2_token'))
me = discord.get(API_BASE_URL + '/users/@me').json()
guilds = discord.get(API_BASE_URL + '/users/@me/guilds').json()
allowed = False
for guild in guilds:
if guild['id'] == os.environ['GUILD_ID']:
if os.environ["ROLE_ID"]:
user_guild = discord.get(API_BASE_URL + f'/users/@me/guilds/{os.environ["GUILD_ID"]}/member').json()
roles = set(os.environ["ROLE_ID"].split(','))
if len(roles.intersection(set(user_guild["roles"]))) > 0:
allowed = True
else:
allowed = True
if allowed:
session['uid'] = int(me['id'])
session['username'] = me['username']
return redirect('/home')
else:
flash('You are not allowed to log in', 'danger')
return redirect('/')
@app.route('/debug')
def debug():
if app.debug:
session['uid'] = 1
session['username'] = 'Debug'
return redirect('/home')
abort(403)
@app.route('/logout')
def logout():
session.clear()
return redirect('/')
@app.route('/discord/callback')
def callback():
if request.values.get('error'):
return request.values['error']
discord = make_session(state=session.get('oauth2_state'))
token = discord.fetch_token(
TOKEN_URL,
client_secret=OAUTH2_CLIENT_SECRET,
authorization_response=request.url)
session['oauth2_token'] = token
return redirect('/login')
@app.route('/home', methods=['GET', 'POST'])
@is_logged_in
def home():
if request.method == "GET":
messages = HomeMessage.query.order_by(HomeMessage.id.asc()).all()
for m in messages:
m.message = markdown.markdown(m.message, extensions=MARKDOWN_EXTENSIONS)
return render_template('home.html', messages=messages, spam_token=os.environ['SPAM_TOKEN'])
try:
message_id = request.form['id']
message = HomeMessage.query.filter_by(id=message_id).first()
if message:
db.session.delete(message)
db.session.commit()
except:
flash('Something went wrong', 'danger')
return redirect('/home')
flash('Message deleted', 'success')
return redirect('/home')
@app.route('/add_ssh', methods=['GET', 'POST'])
@is_logged_in
def add_ssh():
if request.method == "GET":
return render_template('add_ssh.html')
try:
key = request.form['key'].strip()
if '\r' in key or '\n' in key:
flash('Your key is not allowed to contain newlines', 'danger')
return redirect('/add_ssh')
ssh_key = SSHKey(session['uid'], session['username'], key)
db.session.add(ssh_key)
db.session.commit()
except:
flash('Something went wrong', 'danger')
return redirect('/add_ssh')
flash('SSH key added', 'success')
return redirect('/manage_ssh')
@app.route('/manage_ssh', methods=['GET', 'POST'])
@is_logged_in
def manage_ssh():
if request.method == 'GET':
return render_template('manage_ssh.html', keys=SSHKey.query.filter_by(uid=session['uid']).all())
try:
key_id = request.form['id']
ssh_key = SSHKey.query.filter_by(id=key_id).first()
if ssh_key and ssh_key.uid == session['uid']:
db.session.delete(ssh_key)
db.session.commit()
except:
flash('Something went wrong', 'danger')
return redirect('/manage_ssh')
flash('SSH key deleted', 'success')
return redirect('/manage_ssh')
@app.route('/get_ssh')
def get_ssh():
ret = ""
for key in SSHKey.query.all():
ret += f"{key.key} {key.name}\n"
return ret, 200, {'Content-Type': 'text/plain; charset=utf-8'}
@app.route('/add_message', methods=['GET', 'POST'])
@is_logged_in
def add_message():
if request.method == "GET":
return render_template('add_message.html')
try:
message_text = request.form['message']
message = HomeMessage(message_text, session['username'])
db.session.add(message)
db.session.commit()
except:
traceback.print_exc()
flash('Something went wrong', 'danger')
return redirect('/home')
flash('Message added', 'success')
return redirect('/home')
@app.route('/get_wireguard')
@is_logged_in
def get_wireguard():
config: Wireguard = Wireguard.query.filter_by(uid=session['uid']).first()
if config:
return send_from_directory('wireguard', config.filename)
config_files = os.listdir('/wireguard')
for file in config_files:
try:
config = Wireguard(file, session['uid'])
db.session.add(config)
db.session.commit()
return send_from_directory('wireguard', file)
except:
db.session.rollback()
else:
flash('No more wireguard configs left', 'danger')
return redirect('/home')
@app.route('/notify', methods=['POST'])
def ping():
if not os.environ['SPAM_TOKEN'] or request.headers.get('X-ALLOW-SPAM', None) != os.environ['SPAM_TOKEN']:
return {'err': 'invalid spam token'}, 401
msg = request.form.get('msg', None)
if not msg:
return {'err': 'msg missing'}, 400
try:
notification = Notification(msg)
db.session.add(notification)
db.session.commit()
print(notification.time)
except:
return {'err': 'something went wrong'}, 500
sse.publish({'msg': notification.notification, 'time': notification.time.strftime('%a, %d %b %Y %H:%M:%S %Z')}, channel='notifications')
if os.environ['DISCORD_WEBHOOK_URL']:
# Trigger Discord webhook
resp = requests.post(
os.environ['DISCORD_WEBHOOK_URL'] + '?wait=true',
data={"content": msg},
)
if resp.status_code != 200:
return {'err': f'Discord returned code {resp.status_code}: {resp.text}'}, 500
return {'ok': ''}, 200
@app.route('/view_notifications', methods=['GET'])
@is_logged_in
def view_notifications():
def time_to_str(n):
n.time = n.time.strftime('%a, %d %b %Y %H:%M:%S %Z')
return n
notifications = Notification.query.order_by(Notification.time.desc()).all()
notifications = map(lambda n: time_to_str(n), notifications)
return render_template('view_notifications.html', notifications=notifications)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1234, threaded=True)