SRNI-2005/ctf
0
1import datetime2import shutil3from pathlib import Path4 5import click6from flask import Blueprint, current_app7 8from CTFd.utils import get_config as get_config_util9from CTFd.utils import set_config as set_config_util10from CTFd.utils.config import ctf_name11from CTFd.utils.exports import export_ctf as export_ctf_util12from CTFd.utils.exports import import_ctf as import_ctf_util13from CTFd.utils.exports import set_import_end_time, set_import_error14 15_cli = Blueprint("cli", __name__)16 17 18def jsenums():19 import json20 import os21 22 from CTFd.constants import JS_ENUMS23 24 path = os.path.join(current_app.root_path, "themes/core/assets/js/constants.js")25 26 with open(path, "w+") as f:27 for k, v in JS_ENUMS.items():28 f.write("const {} = Object.freeze({});".format(k, json.dumps(v)))29 30 31BUILD_COMMANDS = {"jsenums": jsenums}32 33 34@_cli.cli.command("get_config")35@click.argument("key")36def get_config(key):37 print(get_config_util(key))38 39 40@_cli.cli.command("set_config")41@click.argument("key")42@click.argument("value")43def set_config(key, value):44 print(set_config_util(key, value).value)45 46 47@_cli.cli.command("build")48@click.argument("cmd")49def build(cmd):50 cmd = BUILD_COMMANDS.get(cmd)51 cmd()52 53 54@_cli.cli.command("export_ctf")55@click.argument("path", default="")56def export_ctf(path):57 backup = export_ctf_util()58 59 if path:60 with open(path, "wb") as target:61 shutil.copyfileobj(backup, target)62 else:63 name = ctf_name()64 day = datetime.datetime.now().strftime("%Y-%m-%d_%T")65 full_name = f"{name}.{day}.zip"66 67 with open(full_name, "wb") as target:68 shutil.copyfileobj(backup, target)69 70 print(f"Exported {full_name}")71 72 73@_cli.cli.command("import_ctf")74@click.argument("path", type=click.Path(exists=True))75@click.option(76 "--delete_import_on_finish",77 default=False,78 is_flag=True,79 help="Delete import file when import is finished",80)81def import_ctf(path, delete_import_on_finish=False):82 try:83 import_ctf_util(path)84 except Exception as e:85 from CTFd.utils.dates import unix_time86 87 set_import_error("Import Failure: " + str(e))88 set_import_end_time(value=unix_time(datetime.datetime.utcnow()))89 90 if delete_import_on_finish:91 print(f"Deleting {path}")92 Path(path).unlink()93 