79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
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, username, command, rest, bonusData):
|
|
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, username, 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)
|
|
|
|
if 'user_name' not in request.args:
|
|
username = "User"
|
|
else:
|
|
username = request.args['user_name']
|
|
|
|
return handle_command(request.args['command_source'], username, request.args['command_name'], request.args['rest'], request.args['bonus_data'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
init()
|
|
api.run(host='0.0.0.0')
|