Add Permissions Check To Import From Zip

This commit is contained in:
MCgamin1738 2020-12-12 19:35:41 +00:00
parent f59328e4f1
commit 58b63cb9a4
4 changed files with 18 additions and 5 deletions

View File

@ -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): 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() server_id = helper.create_uuid()
new_server_dir = os.path.join(helper.servers_dir, server_id) new_server_dir = os.path.join(helper.servers_dir, server_id)
if helper.check_file_perms(zip_path):
helper.ensure_dir_exists(new_server_dir) helper.ensure_dir_exists(new_server_dir)
with zipfile.ZipFile(zip_path, 'r') as zip_ref: with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(new_server_dir) zip_ref.extractall(new_server_dir)
else:
return "false"
full_jar_path = os.path.join(new_server_dir, server_jar) 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_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path)

View File

@ -45,6 +45,14 @@ class Helpers:
self.passhasher = PasswordHasher() self.passhasher = PasswordHasher()
self.exiting = False 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): def is_file_older_than_x_days(self, file, days=1):
if self.check_file_exists(file): if self.check_file_exists(file):
file_time = os.path.getmtime(file) file_time = os.path.getmtime(file)

View File

@ -42,7 +42,7 @@ class PanelHandler(BaseHandler):
} }
# if no servers defined, let's go to the build server area # 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.set_status(301)
self.redirect("/server/step1") self.redirect("/server/step1")
return False return False

View File

@ -104,6 +104,9 @@ class ServerHandler(BaseHandler):
return False return False
new_server_id = controller.import_zip_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) 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: else:
# todo: add server type check here and call the correct server add functions if not a jar # 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) new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port)