From d09d0a9678d66e570416390525f9093619336ae7 Mon Sep 17 00:00:00 2001 From: CodeKevin Date: Sun, 15 Mar 2015 04:18:39 -0400 Subject: [PATCH] Closes #21 --- CTFd/admin.py | 15 +++++++++++++-- CTFd/config.py | 2 +- CTFd/utils.py | 28 +++++++++++++++++++++------- CTFd/views.py | 4 ++++ requirements.txt | 3 ++- templates/admin/config.html | 10 ++++++++++ templates/setup.html | 3 +++ 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CTFd/admin.py b/CTFd/admin.py index 22b1def..fddad97 100644 --- a/CTFd/admin.py +++ b/CTFd/admin.py @@ -62,6 +62,8 @@ def init_admin(app): view_challenges_unregistered = None prevent_registration = None + ctf_name = set_config("ctf_name", request.form.get('ctf_name', None)) + mg_api_key = set_config("mg_api_key", request.form.get('mg_api_key', None)) do_api_key = set_config("do_api_key", request.form.get('do_api_key', None)) db_start = Config.query.filter_by(key='start').first() @@ -84,6 +86,14 @@ def init_admin(app): db.session.commit() return redirect('/admin/config') + ctf_name = get_config('ctf_name') + if not ctf_name: + set_config('do_api_key', None) + + mg_api_key = get_config('do_api_key') + if not mg_api_key: + set_config('do_api_key', None) + do_api_key = get_config('do_api_key') if not do_api_key: set_config('do_api_key', None) @@ -107,8 +117,9 @@ def init_admin(app): db.session.commit() db.session.close() - return render_template('admin/config.html', start=start, end=end, view_challenges_unregistered=view_challenges_unregistered, - prevent_registration=prevent_registration, do_api_key=do_api_key) + return render_template('admin/config.html', ctf_name=ctf_name, start=start, end=end, + view_challenges_unregistered=view_challenges_unregistered, + prevent_registration=prevent_registration, do_api_key=do_api_key, mg_api_key=mg_api_key) @app.route('/admin/pages', defaults={'route': None}, methods=['GET', 'POST']) @app.route('/admin/pages/', methods=['GET', 'POST']) diff --git a/CTFd/config.py b/CTFd/config.py index aeee457..4f154ee 100644 --- a/CTFd/config.py +++ b/CTFd/config.py @@ -8,7 +8,7 @@ SESSION_COOKIE_HTTPONLY = True HOST = ".ctfd.io" UPLOAD_FOLDER = os.path.normpath('static/uploads') -##### EMAIL ##### +##### EMAIL (if not using Mailgun) ##### CTF_NAME = '' MAIL_SERVER = '' MAIL_PORT = 0 diff --git a/CTFd/utils.py b/CTFd/utils.py index b98c630..b0412c4 100644 --- a/CTFd/utils.py +++ b/CTFd/utils.py @@ -13,6 +13,7 @@ import datetime import hashlib import digitalocean import shutil +import requests def init_utils(app): @@ -147,18 +148,31 @@ def set_config(key, value): def mailserver(): - if app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']: + if get_config('mg_api_key') or (app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']): return True return False def sendmail(addr, text): - try: - msg = Message("Message from {0}".format(app.config['CTF_NAME']), sender = app.config['ADMINS'][0], recipients = [addr]) - msg.body = text - mail.send(msg) - return True - except: + if get_config('mg_api_key'): + ctf_name = get_config('ctf_name') + mg_api_key = get_config('mg_api_key') + return requests.post( + "https://api.mailgun.net/v2/mail"+app.config['HOST']+"/messages", + auth=("api", mg_api_key), + data={"from": "{} Admin ".format(ctf_name), + "to": [addr], + "subject": "Message from {0}".format(ctf_name), + "text": text}) + elif app.config['MAIL_SERVER'] and app.config['MAIL_PORT'] and app.config['ADMINS']: + try: + msg = Message("Message from {0}".format(get_config('ctf_name')), sender=app.config['ADMINS'][0], recipients=[addr]) + msg.body = text + mail.send(msg) + return True + except: + return False + else: return False diff --git a/CTFd/views.py b/CTFd/views.py index c0b9425..ed11c34 100644 --- a/CTFd/views.py +++ b/CTFd/views.py @@ -48,6 +48,9 @@ def init_views(app): if not is_setup(): if request.method == 'POST': + ctf_name = request.form['ctf_name'] + ctf_name = Config('ctf_name', ctf_name) + ## Admin user name = request.form['name'] email = request.form['email'] @@ -71,6 +74,7 @@ def init_views(app): setup = Config('setup', True) + db.session.add(ctf_name) db.session.add(admin) db.session.add(page) db.session.add(start) diff --git a/requirements.txt b/requirements.txt index a9b8efc..9fda22d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ passlib==1.6.2 bcrypt six==1.8.0 itsdangerous -python-digitalocean \ No newline at end of file +python-digitalocean +requests diff --git a/templates/admin/config.html b/templates/admin/config.html index 87174d9..e8951d6 100644 --- a/templates/admin/config.html +++ b/templates/admin/config.html @@ -7,6 +7,16 @@
+
+ + +
+ +
+ + +
+
diff --git a/templates/setup.html b/templates/setup.html index 36fad60..0c272d2 100644 --- a/templates/setup.html +++ b/templates/setup.html @@ -17,6 +17,9 @@
+ CTF Name +
+ Username