From df70119229b043e7488ceb813152f5290e9e1c3e Mon Sep 17 00:00:00 2001 From: Fox Wilson Date: Sun, 29 Nov 2015 12:10:38 -0500 Subject: [PATCH] \o automatic static file processing o/ --- config.py | 2 ++ ctftool | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 00af5e0..359a894 100644 --- a/config.py +++ b/config.py @@ -8,6 +8,8 @@ proxied_ip_header = "X-Forwarded-For" flag_rl = 10 teams_on_graph = 10 mail_from = "tjctf@sandbox1431.mailgun.org" +static_prefix = "http://127.0.0.1/tjctf-static/" +static_dir = "/home/fwilson/web/tjctf-static/" # Don't touch these. Instead, copy secrets.example to secrets and edit that. import yaml diff --git a/ctftool b/ctftool index f62d480..9a91708 100755 --- a/ctftool +++ b/ctftool @@ -1,9 +1,12 @@ #!/usr/bin/python3 from database import * from datetime import datetime, timedelta +import config import getpass +import hashlib import os import os.path +import shutil import sys import yaml import random @@ -64,11 +67,39 @@ elif operation == "scan": dirs = [j for j in [os.path.join(path, i) for i in os.listdir(path)] if os.path.isdir(j)] print(dirs) n = 0 + for d in dirs: + staticpaths = {} + if os.path.exists(os.path.join(d, "static.yml")): + with open(os.path.join(d, "static.yml")) as f: + statics = yaml.load(f) + for static in statics: + h = hashlib.sha256() + with open(os.path.join(d, static), "rb") as staticfile: + while True: + buf = staticfile.read(4096) + h.update(buf) + if not buf: + break + + if "." in static: + name, ext = static.split(".", maxsplit=1) + fn = "{}_{}.{}".format(name, h.hexdigest(), ext) + else: + fn = "{}_{}".format(static, h.hexdigest()) + staticpaths[static] = fn + shutil.copy(os.path.join(d, static), os.path.join(config.static_dir, fn)) + print(fn) + if os.path.exists(os.path.join(d, "problem.yml")): with open(os.path.join(d, "problem.yml")) as f: n += 1 - Challenge.create(**yaml.load(f)) + data = yaml.load(f) + for i in staticpaths: + print("looking for |{}|".format(i)) + data["description"] = data["description"].replace("|{}|".format(i), "{}{}".format(config.static_prefix, staticpaths[i])) + Challenge.create(**data) + print(n, "challenges loaded") # vim: syntax=python:ft=python