Organizing mailserver code better, fixing default flask server issues, and fixing modal issues

This commit is contained in:
Kevin Chung
2016-07-29 12:53:32 -04:00
parent 431fab0520
commit afbd4b5f8a
5 changed files with 25 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
from flask import render_template, request, redirect, abort, jsonify, url_for, session, Blueprint from flask import render_template, request, redirect, abort, jsonify, url_for, session, Blueprint
from CTFd.utils import sha512, is_safe_url, authed, mailserver, sendmail, can_register, get_config, verify_email from CTFd.utils import sha512, is_safe_url, authed, can_send_mail, sendmail, can_register, get_config, verify_email
from CTFd.models import db, Teams from CTFd.models import db, Teams
from itsdangerous import TimedSerializer, BadTimeSignature, Signer, BadSignature from itsdangerous import TimedSerializer, BadTimeSignature, Signer, BadSignature
@@ -121,10 +121,10 @@ def register():
session['admin'] = team.admin session['admin'] = team.admin
session['nonce'] = sha512(os.urandom(10)) session['nonce'] = sha512(os.urandom(10))
if mailserver() and get_config('verify_emails'): if can_send_mail() and get_config('verify_emails'):
verify_email(team.email) verify_email(team.email)
else: else:
if mailserver(): if can_send_mail():
sendmail(request.form['email'], "You've successfully registered for {}".format(get_config('ctf_name'))) sendmail(request.form['email'], "You've successfully registered for {}".format(get_config('ctf_name')))
db.session.close() db.session.close()

View File

@@ -18,6 +18,7 @@ SESSION_FILE_DIR = "/tmp/flask_session"
SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True
PERMANENT_SESSION_LIFETIME = 604800 # 7 days in seconds PERMANENT_SESSION_LIFETIME = 604800 # 7 days in seconds
HOST = ".ctfd.io" HOST = ".ctfd.io"
MAILFROM_ADDR = "noreply@ctfd.io"
UPLOAD_FOLDER = os.path.normpath('static/uploads') UPLOAD_FOLDER = os.path.normpath('static/uploads')
TEMPLATES_AUTO_RELOAD = True TEMPLATES_AUTO_RELOAD = True
TRUSTED_PROXIES = [ TRUSTED_PROXIES = [

View File

@@ -139,7 +139,7 @@ input[type="checkbox"] { margin: 0px !important; position: relative; top: 5px; }
</td> </td>
<td class="text-center"><span> <td class="text-center"><span>
<i class="fa fa-pencil-square-o"></i> <i class="fa fa-pencil-square-o"></i>
{% if mailserver() %}<i class="fa fa-envelope"></i>{% endif %} {% if can_send_mail() %}<i class="fa fa-envelope"></i>{% endif %}
<i class="fa fa-times"></i> <i class="fa fa-times"></i>
</span> </span>
</td> </td>
@@ -199,7 +199,7 @@ $('#update-user').click(function(e){
row.find('.team-affiliation').text( $.grep(user_data, function(e){ return e.name == 'affiliation'; })[0]['value'] ); row.find('.team-affiliation').text( $.grep(user_data, function(e){ return e.name == 'affiliation'; })[0]['value'] );
row.find('.team-country').text( $.grep(user_data, function(e){ return e.name == 'country'; })[0]['value'] ); row.find('.team-country').text( $.grep(user_data, function(e){ return e.name == 'country'; })[0]['value'] );
$('#user').modal(); $('#user').modal('hide');
} }
else{ else{
$('#results').append($('p').text( data['data'][i] )) $('#results').append($('p').text( data['data'][i] ))
@@ -224,7 +224,7 @@ $('#send-user-email').click(function(e){
var email_data = $('#email-user form').serializeArray(); var email_data = $('#email-user form').serializeArray();
$.post($('#email-user form').attr('action'), $('#email-user form').serialize(), function(data){ $.post($('#email-user form').attr('action'), $('#email-user form').serialize(), function(data){
if (data == "1"){ if (data == "1"){
$('#email-user').modal(); $('#email-user').modal('hide');
} }
else{ else{
$('#email-user-errors').append("<b>Failed to send email</b>"); $('#email-user-errors').append("<b>Failed to send email</b>");

View File

@@ -92,7 +92,7 @@ def init_utils(app):
app.jinja_env.filters['long2ip'] = long2ip app.jinja_env.filters['long2ip'] = long2ip
app.jinja_env.globals.update(pages=pages) app.jinja_env.globals.update(pages=pages)
app.jinja_env.globals.update(can_register=can_register) app.jinja_env.globals.update(can_register=can_register)
app.jinja_env.globals.update(mailserver=mailserver) app.jinja_env.globals.update(can_send_mail=can_send_mail)
app.jinja_env.globals.update(ctf_name=ctf_name) app.jinja_env.globals.update(ctf_name=ctf_name)
app.jinja_env.globals.update(ctf_theme=ctf_theme) app.jinja_env.globals.update(ctf_theme=ctf_theme)
app.jinja_env.globals.update(can_create_container=can_create_container) app.jinja_env.globals.update(can_create_container=can_create_container)
@@ -315,10 +315,19 @@ def set_config(key, value):
return config return config
def mailserver(): def can_send_mail():
return mailgun() or mailserver()
def mailgun():
if app.config.get('MAILGUN_API_KEY') and app.config.get('MAILGUN_BASE_URL'): if app.config.get('MAILGUN_API_KEY') and app.config.get('MAILGUN_BASE_URL'):
return True return True
if (get_config('mg_api_key') and get_config('mg_base_url')) or (get_config('mail_server') and get_config('mail_port')): if (get_config('mg_api_key') and get_config('mg_base_url')):
return True
return False
def mailserver():
if (get_config('mail_server') and get_config('mail_port')):
return True return True
return False return False
@@ -334,14 +343,15 @@ def get_smtp(host, port, username=None, password=None, TLS=None, SSL=None):
def sendmail(addr, text): def sendmail(addr, text):
if mailserver(): if mailgun():
ctf_name = get_config('ctf_name') ctf_name = get_config('ctf_name')
mg_api_key = get_config('mg_api_key') or app.config.get('MAILGUN_API_KEY') mg_api_key = get_config('mg_api_key') or app.config.get('MAILGUN_API_KEY')
mg_base_url = get_config('mg_base_url') or app.config.get('MAILGUN_BASE_URL') mg_base_url = get_config('mg_base_url') or app.config.get('MAILGUN_BASE_URL')
mailfrom_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
r = requests.post( r = requests.post(
mg_base_url + '/messages', mg_base_url + '/messages',
auth=("api", mg_api_key), auth=("api", mg_api_key),
data={"from": "{} Admin <{}>".format(ctf_name, 'noreply@ctfd.io'), data={"from": "{} Admin <{}>".format(ctf_name, mailfrom_addr),
"to": [addr], "to": [addr],
"subject": "Message from {0}".format(ctf_name), "subject": "Message from {0}".format(ctf_name),
"text": text}) "text": text})
@@ -349,7 +359,7 @@ def sendmail(addr, text):
return True return True
else: else:
return False return False
elif get_config('mail_server') and get_config('mail_port'): elif mailserver():
data = { data = {
'host': get_config('mail_server'), 'host': get_config('mail_server'),
'port': int(get_config('mail_port')) 'port': int(get_config('mail_port'))
@@ -529,4 +539,4 @@ def container_ports(name, verbose=False):
ports = [int(re.sub('[A-Za-z/]+', '', port)) for port in ports_asked] ports = [int(re.sub('[A-Za-z/]+', '', port)) for port in ports_asked]
return ports return ports
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return [] return []

View File

@@ -1,3 +1,3 @@
from CTFd import create_app from CTFd import create_app
app = create_app() app = create_app()
app.run(debug=True, host="0.0.0.0", port=4000) app.run(debug=True, threaded=True, host="0.0.0.0", port=4000)