import flask from flask import request import commands.loader as command_loader from commands.command_base import AbstractCommand api = flask.Flask(__name__) # enable/disable this to get web pages of crashes returned api.config["DEBUG"] = True loadedCommands = {} def init(): # todo load entire command library and cache it here load_commands() def load_commands(): global loadedCommands loadedCommands = command_loader.load_commands_new(AbstractCommand.CommandType.Ver2) def is_command(command: str) -> bool: #print(command) for cmd in loadedCommands: #print(cmd) if command == cmd: return True if command == "!echo": return True else: return False def handle_command(source, command, rest): if command == "!echo": message = "Got payload [%s]" % rest #print(message) return flask.make_response("{\"message\":\"%s\"}" % message, 200, {"Content-Type": "application/json"}) cmd:AbstractCommand = loadedCommands[command] if cmd is not None: cmd_response = cmd.do_command(source, command, rest) return flask.make_response("{\"message\":\"%s\"}" % cmd_response, 200, {"Content-Type": "application/json"}) #print("Doing a command") @api.route('/api/v1/command', methods=['GET']) def command_check(): if 'name' in request.args: if is_command(request.args['name']): return flask.make_response('', 200) else: return flask.make_response('', 404) @api.route('/api/v1/exec', methods=['GET']) def exec_command(): if 'command_name' not in request.args: return flask.make_response('{\"text\":"Argument \'command_name\' not in request"}', 400) if 'rest' not in request.args: return flask.make_response('{\"text\":"Argument \'rest\' not in request"}', 400) if 'command_source' not in request.args: return flask.make_response('{\"text\":"Argument \'command_source\' not in request"}', 400) return handle_command(request.args['command_source'], request.args['command_name'], request.args['rest']) if __name__ == '__main__': init() api.run(host='0.0.0.0')