2016-03-10 07:29:27 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2015-12-23 06:26:27 +00:00
|
|
|
from argparse import ArgumentParser
|
2016-03-12 21:46:38 +00:00
|
|
|
from flask import Flask, send_file
|
2015-12-23 06:26:27 +00:00
|
|
|
|
2016-01-16 16:41:16 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2015-12-27 01:21:15 +00:00
|
|
|
import api
|
2015-12-21 21:08:50 +00:00
|
|
|
import config
|
2015-12-23 06:26:27 +00:00
|
|
|
import json
|
2016-03-10 07:29:27 +00:00
|
|
|
import logging
|
2016-01-06 01:28:19 +00:00
|
|
|
import os
|
2016-03-12 07:34:26 +00:00
|
|
|
import traceback
|
2015-12-23 01:37:38 +00:00
|
|
|
|
2016-01-07 00:23:43 +00:00
|
|
|
from api.decorators import api_wrapper
|
|
|
|
|
2016-01-16 16:41:16 +00:00
|
|
|
app.config.from_object(config)
|
2015-12-31 02:56:00 +00:00
|
|
|
|
2016-01-06 01:28:19 +00:00
|
|
|
if not os.path.exists(app.config["UPLOAD_FOLDER"]):
|
|
|
|
os.makedirs(app.config["UPLOAD_FOLDER"])
|
2016-04-08 02:48:12 +00:00
|
|
|
if not os.path.exists(app.config["GRADER_FOLDER"]):
|
|
|
|
os.makedirs(app.config["GRADER_FOLDER"])
|
2016-03-17 01:41:05 +00:00
|
|
|
if not os.path.exists("pfp"):
|
|
|
|
os.makedirs("pfp")
|
2016-01-06 01:28:19 +00:00
|
|
|
|
2015-12-24 02:06:49 +00:00
|
|
|
with app.app_context():
|
2016-01-02 20:45:05 +00:00
|
|
|
from api.models import db, Files, Teams, Problems, Solves, Users
|
2015-12-24 05:29:04 +00:00
|
|
|
db.init_app(app)
|
|
|
|
db.create_all()
|
2015-12-24 02:06:49 +00:00
|
|
|
|
2016-03-12 07:34:26 +00:00
|
|
|
app.db = db
|
|
|
|
|
2015-12-25 01:37:49 +00:00
|
|
|
app.secret_key = config.SECRET_KEY
|
2015-12-21 07:04:00 +00:00
|
|
|
|
2015-12-25 00:57:58 +00:00
|
|
|
app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin")
|
2015-12-31 02:56:00 +00:00
|
|
|
app.register_blueprint(api.problem.blueprint, url_prefix="/api/problem")
|
2016-01-18 06:41:11 +00:00
|
|
|
app.register_blueprint(api.stats.blueprint, url_prefix="/api/stats")
|
2016-01-12 03:54:26 +00:00
|
|
|
app.register_blueprint(api.team.blueprint, url_prefix="/api/team")
|
|
|
|
app.register_blueprint(api.user.blueprint, url_prefix="/api/user")
|
2015-12-27 01:21:15 +00:00
|
|
|
api.logger.initialize_logs()
|
2015-12-25 00:57:58 +00:00
|
|
|
|
2015-12-23 06:26:27 +00:00
|
|
|
@app.route("/api")
|
2016-01-07 00:23:43 +00:00
|
|
|
@api_wrapper
|
2015-12-23 23:23:18 +00:00
|
|
|
def api_main():
|
2016-01-07 00:23:43 +00:00
|
|
|
return { "success": 1, "message": "The API is online." }
|
2015-12-21 18:10:36 +00:00
|
|
|
|
2016-03-12 21:46:38 +00:00
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
|
|
|
return send_file("../web/index.html")
|
|
|
|
|
2016-03-17 01:41:05 +00:00
|
|
|
def run(args=None):
|
2015-12-24 05:29:04 +00:00
|
|
|
with app.app_context():
|
2016-03-17 01:41:05 +00:00
|
|
|
try:
|
|
|
|
keyword_args = dict(args._get_kwargs())
|
|
|
|
app.debug = keyword_args["debug"] if "debug" in keyword_args else False
|
|
|
|
except: pass
|
2015-12-24 04:14:54 +00:00
|
|
|
app.run(host="0.0.0.0", port=8000)
|
2016-03-10 07:29:27 +00:00
|
|
|
|
|
|
|
def load_problems(args):
|
2016-03-12 07:34:26 +00:00
|
|
|
keyword_args = dict(args._get_kwargs())
|
|
|
|
force = keyword_args["force"] if "force" in keyword_args else False
|
|
|
|
|
2016-03-10 07:29:27 +00:00
|
|
|
if not os.path.exists(config.PROBLEM_DIR):
|
2016-03-12 07:34:26 +00:00
|
|
|
api.logger.log("api.problem.log", "Problems directory doesn't exist.")
|
2016-03-10 07:29:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
for (dirpath, dirnames, filenames) in os.walk(config.PROBLEM_DIR):
|
|
|
|
if "problem.json" in filenames:
|
|
|
|
json_file = os.path.join(dirpath, "problem.json")
|
|
|
|
contents = open(json_file).read()
|
|
|
|
try:
|
|
|
|
data = json.loads(contents)
|
|
|
|
except ValueError as e:
|
2016-03-12 07:34:26 +00:00
|
|
|
api.logger.log("api.problem.log", "Invalid JSON format in file {filename} ({exception})".format(filename=json_file, exception=e))
|
2016-03-10 07:29:27 +00:00
|
|
|
continue
|
|
|
|
if not isinstance(data, dict):
|
2016-03-12 07:34:26 +00:00
|
|
|
api.logger.log("api.problem.log", "{filename} is not a dict.".format(filename=json_file))
|
2016-03-10 07:29:27 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
missing_keys = []
|
|
|
|
for key in ["pid", "title", "category", "value"]:
|
|
|
|
if key not in data:
|
|
|
|
missing_keys.append(key)
|
|
|
|
if len(missing_keys) > 0:
|
2016-03-12 07:34:26 +00:00
|
|
|
api.logger.log("api.problem.log", "{filename} is missing the following keys: {keys}".format(filename=json_file, keys=", ".join(missing_keys)))
|
2016-03-10 07:29:27 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
relative_path = os.path.relpath(dirpath, config.PROBLEM_DIR)
|
2016-03-12 07:34:26 +00:00
|
|
|
data["description"] = open(os.path.join(dirpath, "description.md"), "r").read()
|
|
|
|
api.logger.log("api.problem.log", "Found problem '{}'".format(data["title"]))
|
|
|
|
with app.app_context():
|
|
|
|
try:
|
|
|
|
api.problem.insert_problem(data, force=force)
|
|
|
|
except Exception as e:
|
|
|
|
api.logger.log("api.problem.log", "Problem '{}' was not added to the database. Error: {}".format(data["title"], e))
|
|
|
|
api.logger.log("api.problem.log", "{}".format(traceback.format_exc()))
|
|
|
|
|
|
|
|
api.logger.log("api.problem.log", "Finished.")
|
|
|
|
|
|
|
|
def main():
|
2016-03-10 07:29:27 +00:00
|
|
|
parser = ArgumentParser(description="EasyCTF Server Management")
|
|
|
|
|
|
|
|
subparser = parser.add_subparsers(help="Select one of the following actions.")
|
|
|
|
parser_problems = subparser.add_parser("problems", help="Manage problems.")
|
|
|
|
subparser_problems = parser_problems.add_subparsers(help="Select one of the following actions.")
|
|
|
|
parser_problems_load = subparser_problems.add_parser("load", help="Load all problems into database.")
|
2016-03-12 07:34:26 +00:00
|
|
|
parser_problems_load.add_argument("-f", "--force", action="store_true", help="Force overwrite problems.", default=False)
|
2016-03-10 07:29:27 +00:00
|
|
|
parser_problems_load.set_defaults(func=load_problems)
|
|
|
|
|
|
|
|
parser_run = subparser.add_parser("run", help="Run the server.")
|
|
|
|
parser_run.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False)
|
|
|
|
parser_run.set_defaults(func=run)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if "func" in args:
|
|
|
|
args.func(args)
|
|
|
|
else:
|
2016-03-12 07:34:26 +00:00
|
|
|
parser.print_help()
|
|
|
|
|
2016-03-17 01:41:05 +00:00
|
|
|
if __name__ == "__main__":
|
2016-04-08 02:48:12 +00:00
|
|
|
main()
|