2022-08-17 21:22:03 +00:00
|
|
|
import os
|
|
|
|
import time
|
2022-08-17 23:07:56 +00:00
|
|
|
import shutil
|
2022-08-17 21:22:03 +00:00
|
|
|
import logging
|
|
|
|
import threading
|
2022-08-31 23:45:19 +00:00
|
|
|
import urllib
|
2022-08-17 21:22:03 +00:00
|
|
|
|
|
|
|
from app.classes.controllers.server_perms_controller import PermissionsServers
|
|
|
|
from app.classes.controllers.servers_controller import ServersController
|
|
|
|
from app.classes.shared.helpers import Helpers
|
|
|
|
from app.classes.shared.file_helpers import FileHelpers
|
2023-08-09 21:47:53 +00:00
|
|
|
from app.classes.shared.websocket_manager import WebSocketManager
|
2023-09-09 00:14:12 +00:00
|
|
|
from app.classes.steamcmd.serverapps import SteamApps
|
|
|
|
from app.classes.steamcmd.steamcmd import SteamCMD
|
2022-08-17 21:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ImportHelpers:
|
|
|
|
allowed_quotes = ['"', "'", "`"]
|
|
|
|
|
|
|
|
def __init__(self, helper, file_helper):
|
|
|
|
self.file_helper: FileHelpers = file_helper
|
|
|
|
self.helper: Helpers = helper
|
2023-09-09 00:14:12 +00:00
|
|
|
self.steam_apps: SteamApps = SteamApps(helper)
|
|
|
|
self.steam: SteamCMD()
|
2022-08-17 21:22:03 +00:00
|
|
|
|
|
|
|
def import_jar_server(self, server_path, new_server_dir, port, new_id):
|
|
|
|
import_thread = threading.Thread(
|
|
|
|
target=self.import_threaded_jar_server,
|
|
|
|
daemon=True,
|
|
|
|
args=(server_path, new_server_dir, port, new_id),
|
|
|
|
name=f"{new_id}_import",
|
|
|
|
)
|
|
|
|
import_thread.start()
|
|
|
|
|
|
|
|
def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id):
|
|
|
|
for item in os.listdir(server_path):
|
|
|
|
if not item == "db_stats":
|
|
|
|
try:
|
|
|
|
if os.path.isdir(os.path.join(server_path, item)):
|
|
|
|
FileHelpers.copy_dir(
|
|
|
|
os.path.join(server_path, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
FileHelpers.copy_file(
|
|
|
|
os.path.join(server_path, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
except shutil.Error as ex:
|
|
|
|
logger.error(f"Server import failed with error: {ex}")
|
|
|
|
|
|
|
|
has_properties = False
|
|
|
|
for item in os.listdir(new_server_dir):
|
|
|
|
if str(item) == "server.properties":
|
|
|
|
has_properties = True
|
|
|
|
if not has_properties:
|
|
|
|
logger.info(
|
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as file:
|
|
|
|
file.write(f"server-port={port}")
|
|
|
|
file.close()
|
|
|
|
time.sleep(5)
|
|
|
|
ServersController.finish_import(new_id)
|
|
|
|
server_users = PermissionsServers.get_server_user_list(new_id)
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-08-17 21:41:44 +00:00
|
|
|
|
|
|
|
def import_java_zip_server(self, temp_dir, new_server_dir, port, new_id):
|
|
|
|
import_thread = threading.Thread(
|
|
|
|
target=self.import_threaded_java_zip_server,
|
|
|
|
daemon=True,
|
|
|
|
args=(temp_dir, new_server_dir, port, new_id),
|
2022-08-17 23:07:56 +00:00
|
|
|
name=f"{new_id}_java_zip_import",
|
2022-08-17 21:41:44 +00:00
|
|
|
)
|
|
|
|
import_thread.start()
|
|
|
|
|
|
|
|
def import_threaded_java_zip_server(self, temp_dir, new_server_dir, port, new_id):
|
|
|
|
has_properties = False
|
|
|
|
# extracts archive to temp directory
|
|
|
|
for item in os.listdir(temp_dir):
|
|
|
|
if str(item) == "server.properties":
|
|
|
|
has_properties = True
|
|
|
|
try:
|
|
|
|
if not os.path.isdir(os.path.join(temp_dir, item)):
|
|
|
|
FileHelpers.move_file(
|
|
|
|
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if item != "db_stats":
|
|
|
|
FileHelpers.move_dir(
|
|
|
|
os.path.join(temp_dir, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
|
|
|
if not has_properties:
|
|
|
|
logger.info(
|
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as file:
|
|
|
|
file.write(f"server-port={port}")
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
server_users = PermissionsServers.get_server_user_list(new_id)
|
2022-08-17 22:14:16 +00:00
|
|
|
ServersController.finish_import(new_id)
|
2022-08-17 21:41:44 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-08-17 23:07:56 +00:00
|
|
|
# deletes temp dir
|
|
|
|
FileHelpers.del_dirs(temp_dir)
|
|
|
|
|
|
|
|
def import_bedrock_server(
|
|
|
|
self, server_path, new_server_dir, port, full_jar_path, new_id
|
|
|
|
):
|
|
|
|
import_thread = threading.Thread(
|
2022-08-27 14:44:34 +00:00
|
|
|
target=self.import_threaded_bedrock_server,
|
2022-08-17 23:07:56 +00:00
|
|
|
daemon=True,
|
2022-08-27 14:44:34 +00:00
|
|
|
args=(server_path, new_server_dir, port, full_jar_path, new_id),
|
2022-08-17 23:07:56 +00:00
|
|
|
name=f"{new_id}_bedrock_import",
|
|
|
|
)
|
|
|
|
import_thread.start()
|
|
|
|
|
|
|
|
def import_threaded_bedrock_server(
|
|
|
|
self, server_path, new_server_dir, port, full_jar_path, new_id
|
|
|
|
):
|
|
|
|
for item in os.listdir(server_path):
|
|
|
|
if not item == "db_stats":
|
|
|
|
try:
|
|
|
|
if os.path.isdir(os.path.join(server_path, item)):
|
|
|
|
FileHelpers.copy_dir(
|
|
|
|
os.path.join(server_path, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
FileHelpers.copy_file(
|
|
|
|
os.path.join(server_path, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
except shutil.Error as ex:
|
|
|
|
logger.error(f"Server import failed with error: {ex}")
|
|
|
|
|
|
|
|
has_properties = False
|
|
|
|
for item in os.listdir(new_server_dir):
|
|
|
|
if str(item) == "server.properties":
|
|
|
|
has_properties = True
|
|
|
|
if not has_properties:
|
|
|
|
logger.info(
|
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as file:
|
|
|
|
file.write(f"server-port={port}")
|
|
|
|
file.close()
|
|
|
|
if os.name != "nt":
|
|
|
|
if Helpers.check_file_exists(full_jar_path):
|
|
|
|
os.chmod(full_jar_path, 0o2760)
|
|
|
|
ServersController.finish_import(new_id)
|
|
|
|
server_users = PermissionsServers.get_server_user_list(new_id)
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-08-17 23:07:56 +00:00
|
|
|
|
|
|
|
def import_bedrock_zip_server(
|
|
|
|
self, temp_dir, new_server_dir, full_jar_path, port, new_id
|
|
|
|
):
|
|
|
|
import_thread = threading.Thread(
|
|
|
|
target=self.import_threaded_bedrock_zip_server,
|
|
|
|
daemon=True,
|
|
|
|
args=(temp_dir, new_server_dir, full_jar_path, port, new_id),
|
|
|
|
name=f"{new_id}_bedrock_import",
|
|
|
|
)
|
|
|
|
import_thread.start()
|
|
|
|
|
|
|
|
def import_threaded_bedrock_zip_server(
|
|
|
|
self, temp_dir, new_server_dir, full_jar_path, port, new_id
|
|
|
|
):
|
|
|
|
has_properties = False
|
|
|
|
# extracts archive to temp directory
|
|
|
|
for item in os.listdir(temp_dir):
|
|
|
|
if str(item) == "server.properties":
|
|
|
|
has_properties = True
|
|
|
|
try:
|
|
|
|
if not os.path.isdir(os.path.join(temp_dir, item)):
|
|
|
|
FileHelpers.move_file(
|
|
|
|
os.path.join(temp_dir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if item != "db_stats":
|
|
|
|
FileHelpers.move_dir(
|
|
|
|
os.path.join(temp_dir, item),
|
|
|
|
os.path.join(new_server_dir, item),
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
|
|
|
if not has_properties:
|
|
|
|
logger.info(
|
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as file:
|
|
|
|
file.write(f"server-port={port}")
|
|
|
|
file.close()
|
|
|
|
ServersController.finish_import(new_id)
|
|
|
|
server_users = PermissionsServers.get_server_user_list(new_id)
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-08-17 23:07:56 +00:00
|
|
|
if os.name != "nt":
|
|
|
|
if Helpers.check_file_exists(full_jar_path):
|
|
|
|
os.chmod(full_jar_path, 0o2760)
|
|
|
|
# deletes temp dir
|
|
|
|
FileHelpers.del_dirs(temp_dir)
|
2022-08-31 23:45:19 +00:00
|
|
|
|
2023-09-09 00:14:12 +00:00
|
|
|
def download_steam_server(self, app_id, server_id, server_dir, server_exe):
|
|
|
|
download_thread = threading.Thread(
|
|
|
|
target=self.create_steam_server,
|
|
|
|
daemon=True,
|
|
|
|
args=(app_id, server_id, server_dir, server_exe),
|
|
|
|
name=f"{server_id}_download",
|
|
|
|
)
|
|
|
|
download_thread.start()
|
|
|
|
|
|
|
|
def create_steam_server(self, app_id, server_id, server_dir, server_exe):
|
|
|
|
# TODO: what is the server exe called @zedifus
|
2024-02-14 23:50:26 +00:00
|
|
|
# @pretzel As we are not able to use steamcmd to launch game it
|
|
|
|
# is not possible to be populate as we dont know the executable.
|
|
|
|
server_exe = "game.exe"
|
|
|
|
|
|
|
|
# Initiate SteamCMD & game installing status.
|
|
|
|
ServersController.set_import(server_id)
|
2023-09-09 00:14:12 +00:00
|
|
|
|
2024-02-14 23:50:26 +00:00
|
|
|
# Set our storage locations
|
|
|
|
steamcmd_path = os.path.join(server_dir, "steamcmd_files")
|
|
|
|
gamefiles_path = os.path.join(server_dir, "gameserver_files")
|
2023-09-09 00:14:12 +00:00
|
|
|
|
2024-02-14 23:50:26 +00:00
|
|
|
# Ensure game and steam directories exist in server directory.
|
|
|
|
self.helper.ensure_dir_exists(steamcmd_path)
|
|
|
|
self.helper.ensure_dir_exists(gamefiles_path)
|
|
|
|
|
|
|
|
# Set the SteamCMD install directory for next install.
|
|
|
|
self.steam = SteamCMD(steamcmd_path)
|
|
|
|
|
|
|
|
# Install SteamCMD for managing game server files.
|
|
|
|
self.steam.install()
|
|
|
|
|
|
|
|
# Set the server execuion command. TODO brainstorm how to approach.
|
|
|
|
full_jar_path = os.path.join(steamcmd_path, server_exe)
|
2023-09-09 00:14:12 +00:00
|
|
|
if Helpers.is_os_windows():
|
2024-02-14 23:50:26 +00:00
|
|
|
server_command = f'"{full_jar_path}"' # TODO why called jar
|
2023-09-09 00:14:12 +00:00
|
|
|
else:
|
|
|
|
server_command = f"./{server_exe}"
|
|
|
|
logger.debug("command: " + server_command)
|
|
|
|
|
2024-02-14 23:50:26 +00:00
|
|
|
# Install the game server files.
|
|
|
|
self.steam.app_update(app_id, gamefiles_path)
|
|
|
|
|
|
|
|
# Finalise SteamCMD & game installing status.
|
2023-09-09 00:14:12 +00:00
|
|
|
ServersController.finish_import(server_id)
|
2024-02-14 23:50:26 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(server_id)
|
|
|
|
for user in server_users:
|
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2023-09-09 00:14:12 +00:00
|
|
|
|
2022-08-31 23:45:19 +00:00
|
|
|
def download_bedrock_server(self, path, new_id):
|
|
|
|
download_thread = threading.Thread(
|
|
|
|
target=self.download_threaded_bedrock_server,
|
|
|
|
daemon=True,
|
|
|
|
args=(path, new_id),
|
|
|
|
name=f"{new_id}_download",
|
|
|
|
)
|
|
|
|
download_thread.start()
|
|
|
|
|
|
|
|
def download_threaded_bedrock_server(self, path, new_id):
|
2022-09-01 00:01:38 +00:00
|
|
|
# downloads zip from remote url
|
|
|
|
try:
|
|
|
|
bedrock_url = Helpers.get_latest_bedrock_url()
|
|
|
|
if bedrock_url.lower().startswith("https"):
|
|
|
|
urllib.request.urlretrieve(
|
|
|
|
bedrock_url,
|
|
|
|
os.path.join(path, "bedrock_server.zip"),
|
|
|
|
)
|
|
|
|
|
|
|
|
unzip_path = os.path.join(path, "bedrock_server.zip")
|
|
|
|
unzip_path = self.helper.wtol_path(unzip_path)
|
|
|
|
# unzips archive that was downloaded.
|
|
|
|
FileHelpers.unzip_file(unzip_path)
|
|
|
|
# adjusts permissions for execution if os is not windows
|
|
|
|
if not self.helper.is_os_windows():
|
|
|
|
os.chmod(os.path.join(path, "bedrock_server"), 0o0744)
|
|
|
|
|
|
|
|
# we'll delete the zip we downloaded now
|
|
|
|
os.remove(os.path.join(path, "bedrock_server.zip"))
|
|
|
|
except Exception as e:
|
2022-09-01 16:51:54 +00:00
|
|
|
logger.critical(
|
|
|
|
f"Failed to download bedrock executable during server creation! \n{e}"
|
|
|
|
)
|
2022-08-31 23:45:19 +00:00
|
|
|
|
|
|
|
ServersController.finish_import(new_id)
|
|
|
|
server_users = PermissionsServers.get_server_user_list(new_id)
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|