2024-08-24 11:23:19 +00:00
|
|
|
import os
|
2024-06-21 18:44:33 +00:00
|
|
|
from http import HTTPStatus
|
2024-09-06 10:44:05 +00:00
|
|
|
from typing import Union
|
2024-06-24 14:42:41 +00:00
|
|
|
|
2024-07-01 13:42:28 +00:00
|
|
|
from cloudflare.types.dns import SRVRecord
|
2024-06-25 04:55:49 +00:00
|
|
|
from firebase_admin.auth import UserRecord
|
|
|
|
|
2024-07-01 13:42:28 +00:00
|
|
|
import cloudflare_manager
|
2024-06-14 21:33:55 +00:00
|
|
|
import file_manager
|
2024-06-25 04:55:49 +00:00
|
|
|
import firebase_manager
|
|
|
|
from server_mc_manager import MinecraftServerManager
|
2024-06-14 21:33:55 +00:00
|
|
|
|
|
|
|
mc_manager: MinecraftServerManager = MinecraftServerManager()
|
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-06-25 20:46:03 +00:00
|
|
|
user_id: str = user.uid
|
2024-06-25 04:55:49 +00:00
|
|
|
store = firebase_manager.firestore_database
|
|
|
|
_users = store.collection("users")
|
2024-06-29 16:13:24 +00:00
|
|
|
query = _users.where(field_path="subdomain", op_string="==", value=subdomain)
|
2024-06-25 04:55:49 +00:00
|
|
|
for _ in query.stream():
|
|
|
|
return HTTPStatus.ALREADY_REPORTED, "Subdomain already associated."
|
|
|
|
try:
|
2024-06-25 20:46:03 +00:00
|
|
|
if firebase_manager.get_user_field(user_id, "subdomain"):
|
|
|
|
return HTTPStatus.OK, "You cannot change your subdomain."
|
|
|
|
exists: bool = firebase_manager.update_firestore(user_id, {'subdomain': subdomain})
|
2024-06-25 04:55:49 +00:00
|
|
|
if not exists:
|
|
|
|
account_create(user)
|
|
|
|
return set_subdomain(user, subdomain)
|
2024-06-25 20:46:03 +00:00
|
|
|
port = firebase_manager.get_server_port(user_id)
|
|
|
|
if port is None:
|
|
|
|
return HTTPStatus.NOT_FOUND, "Port not found on database"
|
|
|
|
try:
|
2024-07-01 13:42:28 +00:00
|
|
|
record: SRVRecord = cloudflare_manager.add_cloudflare_dns(subdomain, port)
|
|
|
|
record_id: str = record.id
|
2024-07-14 20:06:02 +00:00
|
|
|
if record_id == "":
|
2024-07-01 13:42:28 +00:00
|
|
|
return HTTPStatus.BAD_REQUEST, "Failed to add record to cloudflare"
|
|
|
|
firebase_manager.update_firestore(user_id, {'cloudflare_entry': str(record_id)})
|
2024-06-25 20:46:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
firebase_manager.update_firestore(user_id, {'subdomain': None})
|
2024-07-01 13:42:28 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Cloudflare configuration failed. {str(e)}"
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.OK, "Successfully associated subdomain."
|
|
|
|
except Exception as e:
|
|
|
|
return HTTPStatus.FORBIDDEN, str(e)
|
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, Union[str, list]]:
|
2024-06-25 04:55:49 +00:00
|
|
|
user_id: str = user.uid
|
|
|
|
server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers')
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-06-25 04:55:49 +00:00
|
|
|
servers = list(server_ref.stream())
|
2024-07-14 20:06:02 +00:00
|
|
|
servers_data: list[dict[str, any]] = []
|
|
|
|
for doc in servers:
|
|
|
|
server_info = doc.to_dict()
|
|
|
|
if server_info.get('running', False):
|
|
|
|
try:
|
|
|
|
connected_players = mc_manager.get_online_players(server_info.get('port', 25565))
|
|
|
|
except Exception as e:
|
2024-08-15 08:49:41 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-07-14 20:06:02 +00:00
|
|
|
connected_players = 0
|
|
|
|
server_info['onlinePlayers'] = connected_players
|
|
|
|
servers_data.append(server_info)
|
2024-06-25 20:46:03 +00:00
|
|
|
if not servers_data:
|
|
|
|
account_create(user)
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.OK, servers_data
|
2024-07-11 13:45:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
return account_create(user)
|
2024-06-20 16:57:04 +00:00
|
|
|
|
|
|
|
|
2024-08-15 09:52:18 +00:00
|
|
|
def fetch_logs(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-08-15 10:07:45 +00:00
|
|
|
user_id: str = user.uid
|
|
|
|
lines_to_read: int = 200
|
|
|
|
log_file = f"users/{user_id}/{name}/logs/latest.log"
|
|
|
|
try:
|
|
|
|
with open(log_file, "r") as f:
|
|
|
|
logs = f.readlines()[-lines_to_read:]
|
|
|
|
return HTTPStatus.OK, str(logs)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return HTTPStatus.NOT_FOUND, "Log file not found."
|
2024-08-20 16:56:43 +00:00
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_history(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
|
|
|
user_id: str = user.uid
|
|
|
|
history = f"users/{user_id}/{name}/history.log"
|
|
|
|
try:
|
|
|
|
with open(history, "r") as f:
|
|
|
|
logs = f.readlines()
|
|
|
|
return HTTPStatus.OK, str(logs)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return HTTPStatus.NOT_FOUND, "History file not found."
|
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error."
|
2024-08-15 09:52:18 +00:00
|
|
|
|
|
|
|
|
2024-08-24 11:23:19 +00:00
|
|
|
def fetch_players_status(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[dict[str, str], str]]:
|
|
|
|
user_id: str = user.uid
|
|
|
|
server_path = f"users/{user_id}/{name}"
|
|
|
|
players_status_files = {
|
|
|
|
"Whitelist": "whitelist.json",
|
|
|
|
"BannedPlayers": "banned-players.json",
|
|
|
|
"BannedIps": "banned-ips.json",
|
|
|
|
"Operators": "ops.json"
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
file_contents = {}
|
|
|
|
|
|
|
|
for file_name, file_path in players_status_files.items():
|
|
|
|
full_file_path = f"{server_path}/{file_path}"
|
|
|
|
if os.path.exists(full_file_path):
|
|
|
|
with open(full_file_path, 'r') as file:
|
|
|
|
file_contents[file_name] = file.read()
|
|
|
|
else:
|
|
|
|
return HTTPStatus.NOT_FOUND, "Launch the server at least once."
|
|
|
|
return HTTPStatus.OK, file_contents
|
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error."
|
|
|
|
|
2024-09-10 19:22:58 +00:00
|
|
|
|
2024-09-11 02:16:14 +00:00
|
|
|
def fetch_dir_content(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, list]]:
|
|
|
|
user_id: str = user.uid
|
|
|
|
server_path: str = f"users/{user_id}/{name}/"
|
|
|
|
dirs: dict[str, [str, str]] = {
|
|
|
|
'plugins': ['plugins', '.jar'],
|
|
|
|
'datapack' : ['world/datapacks', '.zip'],
|
2024-09-10 19:22:58 +00:00
|
|
|
}
|
2024-09-11 02:16:14 +00:00
|
|
|
files: list[dict[str, list[str]]] = []
|
2024-09-10 19:22:58 +00:00
|
|
|
try:
|
2024-09-11 02:16:14 +00:00
|
|
|
for key, (path, extension) in dirs.items():
|
|
|
|
final_path = f"{server_path}/{path}"
|
|
|
|
if os.path.exists(final_path):
|
|
|
|
parsed_content = file_manager.filter_directory_contents( extension, os.listdir(final_path))
|
|
|
|
files.append({key: parsed_content})
|
|
|
|
return HTTPStatus.OK, files
|
2024-09-10 19:22:58 +00:00
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-09-11 02:16:14 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error fetching files for server {name}"
|
2024-09-10 19:22:58 +00:00
|
|
|
|
2024-08-24 11:23:19 +00:00
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-07-11 13:46:38 +00:00
|
|
|
if firebase_manager.user_field_exists(user.uid):
|
2024-06-25 20:46:03 +00:00
|
|
|
return HTTPStatus.FORBIDDEN, "User already exists."
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-06-25 20:46:03 +00:00
|
|
|
file_manager.create_folder("users/" + user.uid)
|
2024-06-25 04:55:49 +00:00
|
|
|
firebase_manager.set_firestore(user.uid, {'mail': user.email,
|
|
|
|
'name': user.display_name,
|
|
|
|
'photo_url': user.photo_url,
|
2024-06-25 20:46:03 +00:00
|
|
|
'subdomain': None,
|
2024-07-01 13:42:28 +00:00
|
|
|
'port': firebase_manager.fetch_port() + 1,
|
|
|
|
'cloudflare_entry': None,
|
2024-06-25 04:55:49 +00:00
|
|
|
})
|
|
|
|
return HTTPStatus.CREATED, "Successfully created account."
|
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, "Error creating account."
|
|
|
|
|
|
|
|
|
2024-09-10 11:26:12 +00:00
|
|
|
def account_delete(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
|
|
|
user_id = user.uid
|
|
|
|
try:
|
|
|
|
dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry")
|
|
|
|
if dns_record_id is None:
|
|
|
|
return HTTPStatus.IM_A_TEAPOT, "Cloudflare 'dns_record_id' not found in Firestore."
|
|
|
|
cloudflare_manager.delete_cloudflare_dns(dns_record_id)
|
|
|
|
except Exception as e:
|
|
|
|
return HTTPStatus.NOT_MODIFIED, f"Account deletion failed. {str(e)}"
|
|
|
|
try:
|
|
|
|
firebase_manager.delete_user(user_id)
|
|
|
|
except Exception as e:
|
|
|
|
return HTTPStatus.EXPECTATION_FAILED, f"Database deletion failed | {e}"
|
|
|
|
try:
|
|
|
|
file_manager.delete_non_empty_folder("users/" + user_id)
|
|
|
|
return HTTPStatus.OK, f"Successfully deleted user '{user_id}'."
|
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server."
|
2024-06-25 04:55:49 +00:00
|
|
|
|
|
|
|
|
2024-09-10 11:26:12 +00:00
|
|
|
allowed_frameworks: list[str] = ["paper", "spigot", "bukkit"]
|
|
|
|
|
2024-06-25 04:55:49 +00:00
|
|
|
def server_create(user: UserRecord, name: str, version: str, framework: str = "paper") -> (
|
2024-07-14 17:23:59 +00:00
|
|
|
tuple)[HTTPStatus, Union[str, None]]:
|
2024-06-25 04:55:49 +00:00
|
|
|
if framework not in allowed_frameworks:
|
|
|
|
return HTTPStatus.METHOD_NOT_ALLOWED, f"Framework {framework} not recognized."
|
|
|
|
user_id = user.uid
|
|
|
|
server_path: str = f"users/{user_id}/{name}"
|
|
|
|
server_template_path: str = f"servers/{framework}/{version}"
|
|
|
|
try:
|
2024-06-25 20:46:03 +00:00
|
|
|
port = firebase_manager.get_server_port(user_id)
|
|
|
|
if port is None:
|
|
|
|
return HTTPStatus.NOT_FOUND, "Port not found on database"
|
2024-06-25 04:55:49 +00:00
|
|
|
subdomain = firebase_manager.firestore_database.collection("users").document(user_id).get().get("subdomain")
|
|
|
|
if subdomain is None:
|
|
|
|
firebase_manager.delete_server(user_id, name)
|
|
|
|
return HTTPStatus.NOT_FOUND, f"You haven't associated a subdomain yet."
|
|
|
|
if firebase_manager.server_name_taken(user_id, name):
|
|
|
|
return HTTPStatus.CONFLICT, f"Server name '{name}' already in use."
|
2024-06-25 20:46:03 +00:00
|
|
|
firebase_manager.create_server(user_id, name, version, port, framework)
|
2024-06-21 18:44:33 +00:00
|
|
|
file_manager.create_folder(server_path)
|
|
|
|
file_manager.copy_folder_contents(server_template_path, server_path)
|
|
|
|
file_manager.copy_folder_contents("servers/shared", server_path)
|
2024-07-14 20:19:33 +00:00
|
|
|
prop_path: str = server_path + "/server.properties"
|
|
|
|
file_manager.update_server_property(prop_path, "server-port", port)
|
|
|
|
file_manager.update_server_property(prop_path, "query.port", port)
|
|
|
|
file_manager.update_server_property(prop_path, "enable-query", "true")
|
2024-08-20 16:56:43 +00:00
|
|
|
file_manager.log_action(user_id, name, "ServerCreate")
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.CREATED, f"Successfully created server '{name}'."
|
2024-06-21 18:44:33 +00:00
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}"
|
2024-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-06-25 04:55:49 +00:00
|
|
|
user_id = user.uid
|
2024-07-11 15:12:16 +00:00
|
|
|
try:
|
|
|
|
port: int = firebase_manager.get_server_port(user_id)
|
|
|
|
mc_manager.stop_server_forcefully(port)
|
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e)+f" error when stopping server {name}")
|
2024-06-25 04:55:49 +00:00
|
|
|
server_path: str = f"users/{user_id}/{name}"
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-06-25 04:55:49 +00:00
|
|
|
firebase_manager.delete_server(user_id, name)
|
2024-06-21 18:44:33 +00:00
|
|
|
file_manager.delete_non_empty_folder(server_path)
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.OK, f"Successfully deleted server '{name}'."
|
2024-07-11 13:45:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, None
|
2024-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-06-25 04:55:49 +00:00
|
|
|
user_id = user.uid
|
2024-07-11 15:02:21 +00:00
|
|
|
mc_manager.set_cooldown(user_id=user_id)
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-06-25 20:46:03 +00:00
|
|
|
port: int = firebase_manager.get_server_port(user_id)
|
2024-09-06 10:44:05 +00:00
|
|
|
server_id = mc_manager.start_server(f"users/{user_id}/{name}", port, user_id, name)
|
2024-06-25 20:46:03 +00:00
|
|
|
if server_id is None:
|
|
|
|
return HTTPStatus.OK, f"You cannot run multiples instances at this time."
|
2024-06-21 18:44:33 +00:00
|
|
|
mc_manager.servers[server_id]['port'] = int(port)
|
2024-06-29 21:54:47 +00:00
|
|
|
try:
|
|
|
|
firebase_manager.update_server_running_state(user_id, name, True)
|
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, (f"Error updating server {name}'s running state in database. "
|
|
|
|
f"{type(e).__name__}{str(e)}.")
|
2024-08-20 16:56:43 +00:00
|
|
|
file_manager.log_action(user_id, name, "ServerRun")
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.ACCEPTED, f"Successfully started server '{name}'."
|
2024-06-21 18:44:33 +00:00
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 20:46:03 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}"
|
2024-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-06-25 04:55:49 +00:00
|
|
|
port = None
|
2024-07-11 15:02:21 +00:00
|
|
|
user_id: str = user.uid
|
|
|
|
if mc_manager.has_cooldown(user_id=user_id):
|
|
|
|
return HTTPStatus.FORBIDDEN, f"Wait 30 seconds before stopping '{name}'."
|
2024-07-11 15:12:54 +00:00
|
|
|
mc_manager.set_cooldown(user_id=user_id)
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-07-11 15:02:21 +00:00
|
|
|
port = firebase_manager.get_server_port(user_id)
|
2024-06-25 04:55:49 +00:00
|
|
|
if port is None:
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.NOT_FOUND, f"Server '{name}' not found in firestore."
|
2024-06-25 20:46:03 +00:00
|
|
|
exists: bool = mc_manager.stop_server(port)
|
|
|
|
if exists:
|
2024-07-11 15:02:21 +00:00
|
|
|
firebase_manager.update_server_running_state(user_id, name, False)
|
2024-09-10 11:16:16 +00:00
|
|
|
file_manager.log_action(user_id, name, "ServerStop")
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.OK, f"Successfully stopped server '{name}'."
|
|
|
|
return HTTPStatus.OK, f"Server '{name}' already stopped."
|
2024-06-21 18:44:33 +00:00
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
if port:
|
|
|
|
mc_manager.stop_server_forcefully(port)
|
2024-07-11 15:02:21 +00:00
|
|
|
firebase_manager.update_server_running_state(user_id, name, False)
|
2024-08-20 16:56:43 +00:00
|
|
|
file_manager.log_action(user_id, name, "ServerStop")
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.OK, f"Successfully stopped server '{name}'."
|
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'."
|
2024-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-07-08 20:05:07 +00:00
|
|
|
property_file_path: str = f"users/{uid}/{name}/server.properties"
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
|
|
|
file_manager.update_server_property(property_file_path, prop, value)
|
2024-07-09 17:40:37 +00:00
|
|
|
firebase_manager.update_server_property(uid, name, prop, value)
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.OK, f"Successfully set '{prop}' to '{value}'."
|
2024-06-21 18:44:33 +00:00
|
|
|
except ValueError as e:
|
2024-06-25 04:55:49 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
|
|
|
return HTTPStatus.BAD_REQUEST, f"Property '{prop}' not found."
|
2024-06-25 20:46:03 +00:00
|
|
|
except FileNotFoundError:
|
2024-07-09 17:40:37 +00:00
|
|
|
return HTTPStatus.NOT_FOUND, f"File server.properties for server '{name}' not found."
|
2024-06-21 18:44:33 +00:00
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-07-01 13:42:28 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}"
|
2024-06-21 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-07-08 20:05:07 +00:00
|
|
|
errors: list[str] = []
|
|
|
|
for prop, value in props:
|
|
|
|
if prop not in MinecraftServerManager.allowed_properties:
|
|
|
|
return HTTPStatus.FORBIDDEN, f"Property '{prop}' not allowed."
|
|
|
|
status, message = update_property(uid=user.uid, name=name, prop=prop, value=value)
|
|
|
|
if status != HTTPStatus.OK:
|
|
|
|
errors.append(message)
|
|
|
|
if len(errors) > 0:
|
2024-08-15 08:49:41 +00:00
|
|
|
return HTTPStatus.IM_A_TEAPOT, str(errors)
|
2024-08-24 11:30:25 +00:00
|
|
|
file_manager.log_action(user.uid, name, "UpdateProperties", str(props))
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.OK, f"Successfully updated server '{name}'."
|
2024-07-08 20:05:07 +00:00
|
|
|
|
|
|
|
|
2024-07-14 17:23:59 +00:00
|
|
|
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
2024-06-21 18:44:33 +00:00
|
|
|
try:
|
2024-06-25 20:46:03 +00:00
|
|
|
port = firebase_manager.get_server_port(user.uid)
|
2024-06-25 04:55:49 +00:00
|
|
|
if port is None:
|
2024-07-11 15:02:21 +00:00
|
|
|
return HTTPStatus.NOT_FOUND, f"Server '{name}' not found in firestore."
|
2024-06-25 04:55:49 +00:00
|
|
|
mc_manager.execute_server_command(port, command)
|
2024-08-20 16:56:43 +00:00
|
|
|
file_manager.log_action(user.uid, name, "Command", command)
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.OK, f"Command '{command}' executed successfully."
|
2024-06-21 18:44:33 +00:00
|
|
|
except Exception as e:
|
2024-07-11 13:45:13 +00:00
|
|
|
file_manager.log_error(type(e).__name__, str(e))
|
2024-06-25 04:55:49 +00:00
|
|
|
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error executing command: {command} || {str(e)}"
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-24 14:42:41 +00:00
|
|
|
|
2024-09-06 10:44:05 +00:00
|
|
|
def scheduled_actions() -> None:
|
|
|
|
mc_manager.check_servers_idle()
|
|
|
|
|
2024-06-24 14:42:41 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|