From f59328e4f16be84ce1d0230e137b09067ebdba30 Mon Sep 17 00:00:00 2001 From: MCgamin1738 <5355146-MCgamin1738@users.noreply.gitlab.com> Date: Sat, 12 Dec 2020 17:54:10 +0000 Subject: [PATCH 1/2] Adding Support For Importing Servers From Zip Files --- app/classes/shared/controller.py | 26 ++++++++ app/classes/web/server_handler.py | 6 ++ app/frontend/templates/server/wizard.html | 76 +++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/app/classes/shared/controller.py b/app/classes/shared/controller.py index 805abe5d..880ef1e4 100644 --- a/app/classes/shared/controller.py +++ b/app/classes/shared/controller.py @@ -3,6 +3,8 @@ import time import logging import sys import yaml +import asyncio +import zipfile from distutils import dir_util from app.classes.shared.helpers import helper @@ -228,6 +230,13 @@ class Controller: return False return True + @staticmethod + def verify_zip_server(zip_path: str): + zip_check = helper.check_file_exists(zip_path) + if not zip_check: + return False + return True + def import_jar_server(self, server_name: str, server_path: str, server_jar: str, min_mem: int, max_mem: int, port: int): server_id = helper.create_uuid() new_server_dir = os.path.join(helper.servers_dir, server_id) @@ -244,6 +253,23 @@ class Controller: server_log_file, server_stop, port) return new_id + def import_zip_server(self, server_name: str, zip_path: str, server_jar: str, min_mem: int, max_mem: int, port: int): + server_id = helper.create_uuid() + new_server_dir = os.path.join(helper.servers_dir, server_id) + + helper.ensure_dir_exists(new_server_dir) + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(new_server_dir) + + full_jar_path = os.path.join(new_server_dir, server_jar) + server_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path) + server_log_file = "{}/logs/latest.log".format(new_server_dir) + server_stop = "stop" + + new_id = self.register_server(server_name, server_id, new_server_dir, server_command, server_jar, + server_log_file, server_stop, port) + return new_id + def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, server_log_file: str, server_stop: str, server_port=25565): # put data in the db new_id = Servers.insert({ diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index f368fec1..bffdff6e 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -97,7 +97,13 @@ class ServerHandler(BaseHandler): return False new_server_id = controller.import_jar_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) + elif import_type == 'import_zip': + good_path = controller.verify_zip_server(import_server_path) + if not good_path: + self.redirect("/panel/error?error=Zip file not found!") + return False + new_server_id = controller.import_zip_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) else: # todo: add server type check here and call the correct server add functions if not a jar new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) diff --git a/app/frontend/templates/server/wizard.html b/app/frontend/templates/server/wizard.html index 5616404c..e50b12e8 100644 --- a/app/frontend/templates/server/wizard.html +++ b/app/frontend/templates/server/wizard.html @@ -149,6 +149,82 @@ +
+
+
+ +

Import From Zip File

+
+

+ +

+ {% raw xsrf_form_html() %} + + +
+
+

Server Details

+
+
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+
+ + + +
+

Quick Settings - Don't worry, you can change these later

+
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+ + +
+
+
+

+
+
+
{% end %} From 58b63cb9a4f1e302ab676150b00153b82968f479 Mon Sep 17 00:00:00 2001 From: MCgamin1738 <5355146-MCgamin1738@users.noreply.gitlab.com> Date: Sat, 12 Dec 2020 19:35:41 +0000 Subject: [PATCH 2/2] Add Permissions Check To Import From Zip --- app/classes/shared/controller.py | 10 ++++++---- app/classes/shared/helpers.py | 8 ++++++++ app/classes/web/panel_handler.py | 2 +- app/classes/web/server_handler.py | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/classes/shared/controller.py b/app/classes/shared/controller.py index 880ef1e4..20a01337 100644 --- a/app/classes/shared/controller.py +++ b/app/classes/shared/controller.py @@ -256,10 +256,12 @@ class Controller: def import_zip_server(self, server_name: str, zip_path: str, server_jar: str, min_mem: int, max_mem: int, port: int): server_id = helper.create_uuid() new_server_dir = os.path.join(helper.servers_dir, server_id) - - helper.ensure_dir_exists(new_server_dir) - with zipfile.ZipFile(zip_path, 'r') as zip_ref: - zip_ref.extractall(new_server_dir) + if helper.check_file_perms(zip_path): + helper.ensure_dir_exists(new_server_dir) + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(new_server_dir) + else: + return "false" full_jar_path = os.path.join(new_server_dir, server_jar) server_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index b7c91982..4afe599c 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -45,6 +45,14 @@ class Helpers: self.passhasher = PasswordHasher() self.exiting = False + def check_file_perms(self, path): + try: + fp = open(path, "r").close() + logger.info("{} is readable".format(path)) + return True + except PermissionError: + return False + def is_file_older_than_x_days(self, file, days=1): if self.check_file_exists(file): file_time = os.path.getmtime(file) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index fa602545..63843ded 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -42,7 +42,7 @@ class PanelHandler(BaseHandler): } # if no servers defined, let's go to the build server area - if page_data['server_stats']['total'] == 0: + if page_data['server_stats']['total'] == 0 and page != "error": self.set_status(301) self.redirect("/server/step1") return False diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index bffdff6e..66cdc059 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -104,6 +104,9 @@ class ServerHandler(BaseHandler): return False new_server_id = controller.import_zip_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) + if new_server_id == "false": + self.redirect("/panel/error?error=ZIP file not accessible! You can fix this permissions issue with sudo chown -R crafty:crafty {} And sudo chmod 2775 -R {}".format(import_server_path, import_server_path)) + return False else: # todo: add server type check here and call the correct server add functions if not a jar new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port)