diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 608a1ced..c96ac876 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -54,6 +54,7 @@ class ServersController(metaclass=Singleton): created_by: int, server_port: int = 25565, server_host: str = "127.0.0.1", + app_id: int = None, ) -> int: """Create a server in the database @@ -89,6 +90,7 @@ class ServersController(metaclass=Singleton): created_by, server_port, server_host, + app_id, ) @staticmethod diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index a83fd0a2..c7ed5611 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -42,6 +42,7 @@ class Servers(BaseModel): created_by = IntegerField(default=-100) shutdown_timeout = IntegerField(default=60) ignored_exits = CharField(default="0") + app_id = IntegerField(null=True) class Meta: table_name = "servers" @@ -71,6 +72,7 @@ class HelperServers: created_by: int, server_port: int = 25565, server_host: str = "127.0.0.1", + app_id: int = None, ) -> int: """Create a server in the database @@ -111,6 +113,7 @@ class HelperServers: Servers.backup_path: backup_path, Servers.type: server_type, Servers.created_by: created_by, + Servers.app_id: app_id, } ).execute() diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index c6b35537..e3a39f2c 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -33,6 +33,7 @@ from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.import_helper import ImportHelpers from app.classes.minecraft.serverjars import ServerJars from app.classes.steamcmd.serverapps import SteamApps +from app.classes.steamcmd.steamcmd_command import SteamCMDcommand logger = logging.getLogger(__name__) @@ -44,6 +45,7 @@ class Controller: self.import_helper: ImportHelpers = import_helper self.server_jars: ServerJars = ServerJars(helper) self.steam_apps: SteamApps = SteamApps(helper) + self.steam_cmd: SteamCMDcommand = SteamCMDcommand() self.users_helper: HelperUsers = HelperUsers(database, self.helper) self.roles_helper: HelperRoles = HelperRoles(database) self.servers_helper: HelperServers = HelperServers(database) @@ -766,6 +768,55 @@ class Controller: ) return new_id + def create_steam_server(self, app_id, server_name, user_id, server_port): + server_id = Helpers.create_uuid() + new_server_dir = os.path.join(self.helper.servers_dir, server_id) + backup_path = os.path.join(self.helper.backup_path, server_id) + # TODO: what is the server exe called @zedifus + server_exe = "????" + if Helpers.is_os_windows(): + new_server_dir = Helpers.wtol_path(new_server_dir) + backup_path = Helpers.wtol_path(backup_path) + new_server_dir.replace(" ", "^ ") + backup_path.replace(" ", "^ ") + + Helpers.ensure_dir_exists(new_server_dir) + Helpers.ensure_dir_exists(backup_path) + + # Sets the steamCMD install directory for next install. + self.steam_cmd.force_install_dir(new_server_dir) + + full_jar_path = os.path.join(new_server_dir, server_exe) + + if Helpers.is_os_windows(): + server_command = f'"{full_jar_path}"' + else: + server_command = f"./{server_exe}" + logger.debug("command: " + server_command) + server_log_file = "" + server_stop = "stop" + + new_id = self.register_server( + server_name, + server_id, + new_server_dir, + backup_path, + server_command, + server_exe, + server_log_file, + server_stop, + server_port, + user_id, + server_type="steam", + app_id=app_id, + ) + ServersController.set_import(new_id) + self.steam_cmd.app_update( + app_id, + ) + ServersController.finish_import(new_id) + return new_id + def create_bedrock_server(self, server_name, user_id): server_id = Helpers.create_uuid() new_server_dir = os.path.join(self.helper.servers_dir, server_id) @@ -900,6 +951,7 @@ class Controller: created_by: int, server_type: str, server_host: str = "127.0.0.1", + app_id: int = None, ): # put data in the db new_id = self.servers.create_server( @@ -915,6 +967,7 @@ class Controller: created_by, server_port, server_host, + app_id, ) if not Helpers.check_file_exists( diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index 6ef23de2..f54c4e1f 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -617,6 +617,74 @@ class ServerHandler(BaseHandler): self.controller.servers.stats.record_stats() self.redirect("/panel/dashboard") + if page == "steam_cmd_step1": + if not superuser and not self.controller.crafty_perms.can_create_server( + exec_user["user_id"] + ): + self.redirect( + "/panel/error?error=Unauthorized access: " + "not a server creator or server limit reached" + ) + return + + if not superuser: + user_roles = self.controller.roles.get_all_roles() + else: + user_roles = self.get_user_roles() + app_id = bleach.clean(self.get_argument("steam_server", "")) + server_name = bleach.clean(self.get_argument("server_name", "")) + min_mem = bleach.clean(self.get_argument("min_memory", "")) + max_mem = bleach.clean(self.get_argument("max_memory", "")) + port = bleach.clean(self.get_argument("port", "")) + if int(port) < 1 or int(port) > 65535: + self.redirect( + "/panel/error?error=Constraint Error: " + "Port must be greater than 0 and less than 65535" + ) + return + captured_roles = [] + for role in user_roles: + if bleach.clean(self.get_argument(str(role), "")) == "on": + captured_roles.append(role) + + if not server_name: + self.redirect("/panel/error?error=Server name cannot be empty!") + return + + new_server_id = self.controller.create_steam_server( + app_id, + server_name, + exec_user["user_id"], + ) + + # These lines create a new Role for the Server with full permissions + # and add the user to it if he's not a superuser + if len(captured_roles) == 0: + if not superuser: + new_server_uuid = self.controller.servers.get_server_data_by_id( + new_server_id + ).get("server_uuid") + role_id = self.controller.roles.add_role( + f"Creator of Server with uuid={new_server_uuid}", + exec_user["user_id"], + ) + self.controller.server_perms.add_role_server( + new_server_id, role_id, "11111111" + ) + self.controller.users.add_role_to_user( + exec_user["user_id"], role_id + ) + + else: + for role in captured_roles: + role_id = role + self.controller.server_perms.add_role_server( + new_server_id, role_id, "11111111" + ) + + self.controller.servers.stats.record_stats() + self.redirect("/panel/dashboard") + try: self.render( template, diff --git a/app/migrations/20230320_app_id.py b/app/migrations/20230320_app_id.py new file mode 100644 index 00000000..ba32ca3c --- /dev/null +++ b/app/migrations/20230320_app_id.py @@ -0,0 +1,16 @@ +# Generated by database migrator +import peewee + + +def migrate(migrator, database, **kwargs): + migrator.add_columns("servers", app_id=peewee.IntegerField(null=True)) + """ + Write your migrations here. + """ + + +def rollback(migrator, database, **kwargs): + migrator.drop_columns("servers", ["app_id"]) + """ + Write your rollback migrations here. + """