easyctf-2017/server/api/problem.py
2016-04-07 22:54:50 -04:00

180 lines
5.5 KiB
Python

import hashlib
import logger
import os
import shutil
import utils
from flask import Blueprint, jsonify, session, request
from flask import current_app as app
from werkzeug import secure_filename
from models import db, Files, Problems, Solves, Teams
from decorators import admins_only, api_wrapper, login_required, InternalException, WebException
blueprint = Blueprint("problem", __name__)
@blueprint.route("/add", methods=["POST"])
@admins_only
@api_wrapper
def problem_add():
title = request.form["title"]
category = request.form["category"]
description = request.form["description"]
hint = request.form["hint"]
value = request.form["value"]
grader_contents = request.form["grader_contents"]
pid = utils.generate_string()
while Problems.query.filter_by(pid=pid).first():
pid = utils.generate_string()
title_exist = Problems.query.filter_by(title=title).first()
if title_exist:
raise WebException("Problem name already taken.")
problem = Problems(pid, title, category, description, value, hint=hint)
db.session.add(problem)
db.session.commit()
files = request.files.getlist("files[]")
for _file in files:
filename = secure_filename(_file.filename)
if len(filename) == 0:
continue
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
_file.save(file_path)
db_file = Files(problem.pid, "/".join(file_path.split("/")[2:]))
db.session.add(db_file)
grader_folder = os.path.join(app.config["GRADER_FOLDER"], pid)
if not os.path.exists(grader_folder):
os.makedirs(grader_folder)
grader_path = os.path.join(grader_folder, "grader.py")
grader_file = open(grader_path, "w")
grader_file.write(grader_contents)
grader_file.close()
problem.grader = grader_path
db.session.commit()
return { "success": 1, "message": "Success!" }
@blueprint.route("/delete", methods=["POST"])
@admins_only
@api_wrapper
def problem_delete():
pid = request.form["pid"]
problem = Problems.query.filter_by(pid=pid).first()
if problem:
Solves.query.filter_by(pid=pid).delete()
Problems.query.filter_by(pid=pid).delete()
grader_folder = "/".join(problem.grader.split("/")[:-1])
shutil.rmtree(grader_folder)
db.session.commit()
return { "success": 1, "message": "Success!" }
raise WebException("Problem does not exist!")
@blueprint.route("/update", methods=["POST"])
@admins_only
@api_wrapper
def problem_update():
pid = request.form["pid"]
title = request.form["title"]
category = request.form["category"]
description = request.form["description"]
hint = request.form["hint"]
value = request.form["value"]
grader_contents = request.form["grader_contents"]
problem = Problems.query.filter_by(pid=pid).first()
if problem:
problem.title = title
problem.category = category
problem.description = description
problem.hint = hint
problem.value = value
grader = open(problem.grader, "w")
grader.write(grader_contents)
grader.close()
db.session.add(problem)
db.session.commit()
return { "success": 1, "message": "Success!" }
raise WebException("Problem does not exist!")
@blueprint.route("/submit", methods=["POST"])
@api_wrapper
@login_required
def problem_submit():
pid = request.form["pid"]
flag = request.form["flag"]
tid = session["tid"]
problem = Problems.query.filter_by(pid=pid).first()
team = Teams.query.filter_by(tid=tid).first()
if problem:
if flag == problem.flag:
solve = Solves(pid, tid)
team.score += problem.value
problem.solves += 1
db.session.add(solve)
db.session.add(team)
db.session.add(problem)
db.session.commit()
logger.log(__name__, logger.WARNING, "%s has solved %s by submitting %s" % (team.name, problem.title, flag))
return { "success": 1, "message": "Correct!" }
else:
logger.log(__name__, logger.WARNING, "%s has incorrectly submitted %s to %s" % (team.name, flag, problem.title))
raise WebException("Incorrect.")
else:
raise WebException("Problem does not exist!")
@blueprint.route("/data", methods=["POST"])
#@api_wrapper # Disable atm due to json serialization issues: will fix
@login_required
def problem_data():
problems = Problems.query.add_columns("pid", "name", "category", "description", "hint", "value", "solves").order_by(Problems.value).filter_by(disabled=False).all()
jason = []
for problem in problems:
problem_files = [ str(_file.location) for _file in Files.query.filter_by(pid=int(problem.pid)).all() ]
jason.append({"pid": problem[1], "title": problem[2] ,"category": problem[3], "description": problem[4], "hint": problem[5], "value": problem[6], "solves": problem[7], "files": problem_files})
return jsonify(data=jason)
def insert_problem(data, force=False):
with app.app_context():
if len(list(get_problem(pid=data["pid"]).all())) > 0:
if force == True:
_problem = Problems.query.filter_by(pid=data["pid"]).first()
db.session.delete(_problem)
db.session.commit()
else:
raise InternalException("Problem already exists.")
insert = Problems(data["pid"], data["title"], data["category"], data["description"], data["value"])
if "hint" in data: insert.hint = data["hint"]
if "autogen" in data: insert.autogen = data["autogen"]
if "bonus" in data: insert.bonus = data["bonus"]
if "threshold" in data: insert.threshold = data["threshold"]
if "weightmap" in data: insert.weightmap = data["weightmap"]
db.session.add(insert)
db.session.commit()
return True
def get_problem(title=None, pid=None):
match = {}
if title != None:
match.update({ "title": title })
elif pid != None:
match.update({ "pid": pid })
with app.app_context():
result = Problems.query.filter_by(**match)
return result