diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index 82604edf..33cd0975 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -174,7 +174,7 @@ class ServerJars: # todo: verify the MD5 # put data in the db - Servers.insert({ + new_id = Servers.insert({ Servers.server_name: name, Servers.server_uuid: server_id, Servers.path: server_dir, @@ -212,6 +212,6 @@ class ServerJars: # let's re-init all servers controller.init_all_servers() - return True + return new_id server_jar_obj = ServerJars() diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index e6353a14..05bab6f0 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -43,6 +43,16 @@ class Users(BaseModel): class Meta: table_name = "users" +class Audit_Log(BaseModel): + audit_id = AutoField() + created = DateTimeField(default=datetime.datetime.now) + user_name = CharField(default="") + user_id = IntegerField(default=0) + source_ip = CharField(default='127.0.0.1') + server_id = IntegerField(default=None) + log_msg = TextField(default='') + + class Host_Stats(BaseModel): time = DateTimeField(default=datetime.datetime.now) @@ -150,7 +160,8 @@ class db_builder: Webhooks, Servers, Server_Stats, - Commands + Commands, + Audit_Log ]) @staticmethod @@ -190,6 +201,10 @@ class db_shortcuts: return rows + def get_server_data_by_id(self, server_id): + query = Servers.get_by_id(server_id) + return model_to_dict(query) + def get_all_defined_servers(self): query = Servers.select() return self.return_rows(query) @@ -201,7 +216,6 @@ class db_shortcuts: for s in servers: latest = Server_Stats.select().where(Server_Stats.server_id == s.get('server_id')).order_by(Server_Stats.created.desc()).limit(1) server_data.append({'server_data': s, "stats": self.return_rows(latest)}) - # print(server_data) return server_data @staticmethod @@ -210,8 +224,52 @@ class db_shortcuts: return model_to_dict(query) def get_unactioned_commands(self): - query = Commands.select().where(Commands.executed == False) + query = Commands.select().where(Commands.executed == 0) return self.return_rows(query) + def get_server_friendly_name(self, server_id): + server_data = self.get_server_data_by_id(server_id) + friendly_name = "{}-{}".format(server_data.get('server_id', 0), server_data.get('server_name', None)) + return friendly_name + + def send_command(self, user_id, server_id, remote_ip, command): + + server_name = self.get_server_friendly_name(server_id) + + self.add_to_audit_log(user_id, "Issued Command {} for Server: {}".format(command, server_name), + server_id, remote_ip) + + Commands.insert({ + Commands.server_id: server_id, + Commands.user: user_id, + Commands.source_ip: remote_ip, + Commands.command: command + }).execute() + + @staticmethod + def mark_command_complete(command_id=None): + if command_id is not None: + logger.debug("Marking Command {} completed".format(command_id)) + Commands.update({ + Commands.executed: True + }).where(Commands.command_id == command_id).execute() + + + @staticmethod + def add_to_audit_log(user_id, log_msg, server_id=None, source_ip=None): + logger.debug("Adding to audit log User:{} - Message: {} ".format(user_id, log_msg)) + user_data = Users.get_by_id(user_id) + + audit_msg = "{} {}".format(str(user_data.username).capitalize(), log_msg) + + Audit_Log.insert({ + Audit_Log.user_name: user_data.username, + Audit_Log.user_id: user_id, + Audit_Log.server_id: server_id, + Audit_Log.log_msg: audit_msg, + Audit_Log.source_ip: source_ip + }).execute() + + installer = db_builder() db_helper = db_shortcuts() diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index ec950dcf..48998ac2 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -55,6 +55,22 @@ class TasksManager: while True: # select any commands waiting to be processed commands = db_helper.get_unactioned_commands() + for c in commands: + + svr = controller.get_server_obj(c['server_id']['server_id']) + command = c.get('command', None) + + if command == 'start_server': + svr.run_threaded_server() + + elif command == 'stop_server': + svr.stop_threaded_server() + + elif command == "restart_server": + svr.restart_threaded_server() + + db_helper.mark_command_complete(c.get('command_id', None)) + time.sleep(1) diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index a9b9c8b0..94e763bd 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -7,7 +7,7 @@ import tornado.escape from app.classes.shared.helpers import helper from app.classes.web.base_handler import BaseHandler from app.classes.shared.console import console -from app.classes.shared.models import Users, fn +from app.classes.shared.models import Users, fn, db_helper logger = logging.getLogger(__name__) @@ -100,11 +100,13 @@ class PublicHandler(BaseHandler): Users.last_login: helper.get_time_as_string() }).where(Users.username == entered_username).execute() + # log this login + db_helper.add_to_audit_log(user_data.user_id, "Logged in", None, self.get_remote_ip()) + cookie_data = { "username": user_data.username, - "user_id": user_data.id, + "user_id": user_data.user_id, "account_type": user_data.allowed_servers, - } self.set_secure_cookie('user_data', json.dumps(cookie_data)) diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index 9ec0ec62..0acc93b8 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -48,8 +48,6 @@ class ServerHandler(BaseHandler): 'show_contribute': helper.get_setting("show_contribute_link", True) } - - if page == "step1": page_data['server_types'] = server_jar_obj.get_serverjar_data() @@ -77,16 +75,7 @@ class ServerHandler(BaseHandler): command = bleach.clean(self.get_argument("command", None)) if server_id is not None: - svr = controller.get_server_obj(server_id) - - if command == "start_server": - svr.run_threaded_server() - - if command == "stop_server": - svr.stop_threaded_server() - - if command == "restart_server": - svr.restart_threaded_server() + db_helper.send_command(user_data['user_id'], server_id, self.get_remote_ip(), command) if page == "step1": @@ -98,9 +87,13 @@ class ServerHandler(BaseHandler): server_parts = server.split("|") - success = server_jar_obj.build_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) + new_server_id = server_jar_obj.build_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) - if success: + if new_server_id: + db_helper.add_to_audit_log(user_data['user_id'], + "Created server {} named {}".format(server, server_name), + new_server_id, + self.get_remote_ip()) stats.record_stats() self.redirect("/panel/dashboard")