import json import os import shlex from subprocess import Popen import toml from flask import Flask, request, abort app = Flask(__name__) SETTINGS_FILE = "settings.toml" config = {} if os.path.exists(SETTINGS_FILE): config = toml.loads(open(SETTINGS_FILE, "r").read()) def json_path(obj, path): if not type(obj) in [dict, list]: return None if "." not in path: return None if path not in obj else obj[path] key = path.split(".") if key[0] not in obj: return None return json_path(obj[key[0]], ".".join(key[1:])) def process(target): app.logger.error("PROCESSING: "+str(target)) Popen(shlex.split(target["exec"]), cwd=target["path"]) app.logger.error("POST-processing :P") return "True", 200 @app.route("/", methods=['GET','POST']) def index(): content = request.data.decode("utf8") try: data = json.loads(content) #app.logger.error(json.dumps(data, indent=1)) except Exception as e: app.logger.exception(e) return "False", 400 #app.logger.error(request.headers) for source in config: source = config[source] if not "targets" in source: continue for target in source["targets"]: target_name = target target = source["targets"][target] requested_name = json_path(data, source["name_path"]) if not requested_name: continue app.logger.error(str(target_name) + str(requested_name)) if requested_name == target_name: if "token" in target: if not source["tokenHeader"] in request.headers: return "False", 403 if not request.headers[source["tokenHeader"]] == target["token"]: return "False", 403 return process(target) return "False", 404 app.run("0.0.0.0",8000)