2021-01-15 19:59:58 +00:00
|
|
|
import os
|
2021-06-02 18:47:08 +00:00
|
|
|
import html
|
2021-08-11 20:29:31 +00:00
|
|
|
import re
|
2022-01-26 01:45:30 +00:00
|
|
|
import logging
|
2022-03-01 22:10:04 +00:00
|
|
|
import time
|
2022-04-11 05:23:55 +00:00
|
|
|
import bleach
|
|
|
|
import tornado.web
|
|
|
|
import tornado.escape
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.server_permissions import EnumPermissionsServer
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2021-08-10 20:17:56 +00:00
|
|
|
from app.classes.shared.server import ServerOutBuf
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.web.base_handler import BaseHandler
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-09-02 01:30:39 +00:00
|
|
|
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class AjaxHandler(BaseHandler):
|
2020-09-02 01:30:39 +00:00
|
|
|
def render_page(self, template, page_data):
|
|
|
|
self.render(
|
|
|
|
template,
|
2021-03-26 13:57:50 +00:00
|
|
|
data=page_data,
|
|
|
|
translate=self.translator.translate,
|
2020-09-02 01:30:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def get(self, page):
|
2022-01-15 00:23:50 +00:00
|
|
|
_, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
error = bleach.clean(self.get_argument("error", "WTF Error!"))
|
2020-09-02 01:30:39 +00:00
|
|
|
|
|
|
|
template = "panel/denied.html"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
page_data = {"user_data": exec_user, "error": error}
|
2020-09-02 01:30:39 +00:00
|
|
|
|
|
|
|
if page == "error":
|
|
|
|
template = "public/error.html"
|
|
|
|
self.render_page(template, page_data)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
elif page == "server_log":
|
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
full_log = self.get_argument("full", False)
|
2020-09-02 01:30:39 +00:00
|
|
|
|
|
|
|
if server_id is None:
|
|
|
|
logger.warning("Server ID not found in server_log ajax call")
|
|
|
|
self.redirect("/panel/error?error=Server ID Not Found")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2020-09-02 01:30:39 +00:00
|
|
|
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2021-09-25 21:05:49 +00:00
|
|
|
server_data = self.controller.servers.get_server_data_by_id(server_id)
|
2020-09-02 01:30:39 +00:00
|
|
|
if not server_data:
|
|
|
|
logger.warning("Server Data not found in server_log ajax call")
|
|
|
|
self.redirect("/panel/error?error=Server ID Not Found")
|
2021-08-10 20:17:56 +00:00
|
|
|
return
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not server_data["log_path"]:
|
|
|
|
logger.warning(
|
|
|
|
f"Log path not found in server_log ajax call ({server_id})"
|
|
|
|
)
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2020-09-04 02:49:20 +00:00
|
|
|
if full_log:
|
2022-04-11 05:23:55 +00:00
|
|
|
log_lines = self.helper.get_setting("max_log_lines")
|
|
|
|
data = Helpers.tail_file(
|
|
|
|
Helpers.get_os_understandable_path(server_data["log_path"]),
|
2022-03-23 02:50:12 +00:00
|
|
|
log_lines,
|
|
|
|
)
|
2020-09-04 02:49:20 +00:00
|
|
|
else:
|
2021-08-10 20:17:56 +00:00
|
|
|
data = ServerOutBuf.lines.get(server_id, [])
|
2020-09-04 02:49:20 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for line in data:
|
2020-09-02 01:30:39 +00:00
|
|
|
try:
|
2022-05-15 01:53:18 +00:00
|
|
|
line = re.sub("(\033\\[(0;)?[0-9]*[A-z]?(;[0-9])?m?)", "", line)
|
2022-04-14 02:10:25 +00:00
|
|
|
line = re.sub("[A-z]{2}\b\b", "", line)
|
|
|
|
line = self.helper.log_colors(html.escape(line))
|
2022-03-23 02:50:12 +00:00
|
|
|
self.write(f"{line}<br />")
|
2020-09-02 01:30:39 +00:00
|
|
|
# self.write(d.encode("utf-8"))
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.warning(f"Skipping Log Line due to error: {e}")
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2020-09-23 01:42:41 +00:00
|
|
|
elif page == "announcements":
|
2022-04-11 05:23:55 +00:00
|
|
|
data = Helpers.get_announcements()
|
2022-03-23 02:50:12 +00:00
|
|
|
page_data["notify_data"] = data
|
|
|
|
self.render_page("ajax/notify.html", page_data)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-01-10 03:21:28 +00:00
|
|
|
elif page == "get_zip_tree":
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-01-10 03:21:28 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.write(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
+ "\n"
|
2022-04-11 05:23:55 +00:00
|
|
|
+ Helpers.generate_zip_tree(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-10 03:21:28 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
elif page == "get_zip_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-01-10 03:21:28 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.write(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
+ "\n"
|
2022-04-11 05:23:55 +00:00
|
|
|
+ Helpers.generate_zip_dir(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-10 03:21:28 +00:00
|
|
|
self.finish()
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-01 01:02:30 +00:00
|
|
|
elif page == "get_backup_tree":
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
folder = self.get_argument("path", None)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
output = ""
|
|
|
|
|
2022-03-01 21:54:06 +00:00
|
|
|
dir_list = []
|
|
|
|
unsorted_files = []
|
2022-03-01 01:02:30 +00:00
|
|
|
file_list = os.listdir(folder)
|
2022-03-01 21:54:06 +00:00
|
|
|
for item in file_list:
|
|
|
|
if os.path.isdir(os.path.join(folder, item)):
|
|
|
|
dir_list.append(item)
|
|
|
|
else:
|
|
|
|
unsorted_files.append(item)
|
2022-03-23 02:50:12 +00:00
|
|
|
file_list = sorted(dir_list, key=str.casefold) + sorted(
|
|
|
|
unsorted_files, key=str.casefold
|
|
|
|
)
|
|
|
|
output += f"""<ul class="tree-nested d-block" id="{folder}ul">"""
|
2022-03-01 01:02:30 +00:00
|
|
|
for raw_filename in file_list:
|
|
|
|
filename = html.escape(raw_filename)
|
|
|
|
rel = os.path.join(folder, raw_filename)
|
|
|
|
dpath = os.path.join(folder, filename)
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(dpath) in self.controller.management.get_excluded_backup_dirs(
|
|
|
|
server_id
|
|
|
|
):
|
2022-03-01 01:02:30 +00:00
|
|
|
if os.path.isdir(rel):
|
2022-03-23 02:50:12 +00:00
|
|
|
output += f"""<li class="tree-item" data-path="{dpath}">
|
2022-03-01 01:02:30 +00:00
|
|
|
\n<div id="{dpath}" data-path="{dpath}" data-name="{filename}" class="tree-caret tree-ctx-item tree-folder">
|
|
|
|
<input type="checkbox" class="checkBoxClass" name="root_path" value="{dpath}" checked>
|
|
|
|
<span id="{dpath}span" class="files-tree-title" data-path="{dpath}" data-name="{filename}" onclick="getDirView(event)">
|
2022-05-15 17:38:48 +00:00
|
|
|
<i style="color: #8862e0;" class="far fa-folder"></i>
|
|
|
|
<i style="color: #8862e0;" class="far fa-folder-open"></i>
|
2022-03-01 01:02:30 +00:00
|
|
|
<strong>{filename}</strong>
|
|
|
|
</span>
|
|
|
|
</input></div><li>
|
2022-03-23 02:50:12 +00:00
|
|
|
\n"""
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
|
|
|
output += f"""<li
|
2022-05-15 17:38:48 +00:00
|
|
|
class="d-block tree-ctx-item tree-file"
|
2022-03-01 01:02:30 +00:00
|
|
|
data-path="{dpath}"
|
|
|
|
data-name="{filename}"
|
|
|
|
onclick=""><input type='checkbox' class="checkBoxClass" name='root_path' value="{dpath}" checked><span style="margin-right: 6px;">
|
|
|
|
<i class="far fa-file"></i></span></input>{filename}</li>"""
|
|
|
|
|
|
|
|
else:
|
|
|
|
if os.path.isdir(rel):
|
2022-03-23 02:50:12 +00:00
|
|
|
output += f"""<li class="tree-item" data-path="{dpath}">
|
2022-03-01 01:02:30 +00:00
|
|
|
\n<div id="{dpath}" data-path="{dpath}" data-name="{filename}" class="tree-caret tree-ctx-item tree-folder">
|
|
|
|
<input type="checkbox" class="checkBoxClass" name="root_path" value="{dpath}">
|
|
|
|
<span id="{dpath}span" class="files-tree-title" data-path="{dpath}" data-name="{filename}" onclick="getDirView(event)">
|
2022-05-15 17:38:48 +00:00
|
|
|
<i style="color: #8862e0;" class="far fa-folder"></i>
|
|
|
|
<i style="color: #8862e0;" class="far fa-folder-open"></i>
|
2022-03-01 01:02:30 +00:00
|
|
|
<strong>{filename}</strong>
|
|
|
|
</span>
|
|
|
|
</input></div><li>
|
2022-03-23 02:50:12 +00:00
|
|
|
\n"""
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
|
|
|
output += f"""<li
|
2022-05-15 17:38:48 +00:00
|
|
|
class="d-block tree-ctx-item tree-file"
|
2022-03-01 01:02:30 +00:00
|
|
|
data-path="{dpath}"
|
|
|
|
data-name="{filename}"
|
|
|
|
onclick=""><input type='checkbox' class="checkBoxClass" name='root_path' value="{dpath}">
|
2022-03-23 06:06:13 +00:00
|
|
|
<span style="margin-right: 6px;"><i class="far fa-file">
|
|
|
|
</i></span></input>{filename}</li>"""
|
2022-04-11 05:23:55 +00:00
|
|
|
self.write(Helpers.get_os_understandable_path(folder) + "\n" + output)
|
2022-03-01 01:02:30 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
elif page == "get_backup_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
folder = self.get_argument("path", None)
|
2022-03-01 01:02:30 +00:00
|
|
|
output = ""
|
|
|
|
|
2022-03-01 21:54:06 +00:00
|
|
|
dir_list = []
|
|
|
|
unsorted_files = []
|
2022-03-01 01:02:30 +00:00
|
|
|
file_list = os.listdir(folder)
|
2022-03-01 21:54:06 +00:00
|
|
|
for item in file_list:
|
|
|
|
if os.path.isdir(os.path.join(folder, item)):
|
|
|
|
dir_list.append(item)
|
|
|
|
else:
|
|
|
|
unsorted_files.append(item)
|
2022-03-23 02:50:12 +00:00
|
|
|
file_list = sorted(dir_list, key=str.casefold) + sorted(
|
|
|
|
unsorted_files, key=str.casefold
|
|
|
|
)
|
|
|
|
output += f"""<ul class="tree-nested d-block" id="{folder}ul">"""
|
2022-03-01 01:02:30 +00:00
|
|
|
for raw_filename in file_list:
|
|
|
|
filename = html.escape(raw_filename)
|
|
|
|
rel = os.path.join(folder, raw_filename)
|
|
|
|
dpath = os.path.join(folder, filename)
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(dpath) in self.controller.management.get_excluded_backup_dirs(
|
|
|
|
server_id
|
|
|
|
):
|
2022-03-01 01:02:30 +00:00
|
|
|
if os.path.isdir(rel):
|
2022-03-23 02:50:12 +00:00
|
|
|
output += f"""<li class="tree-item" data-path="{dpath}">
|
2022-03-01 01:02:30 +00:00
|
|
|
\n<div id="{dpath}" data-path="{dpath}" data-name="{filename}" class="tree-caret tree-ctx-item tree-folder">
|
2022-05-09 17:34:45 +00:00
|
|
|
<input type="checkbox" name="root_path" value="{dpath}" checked>
|
2022-03-01 01:02:30 +00:00
|
|
|
<span id="{dpath}span" class="files-tree-title" data-path="{dpath}" data-name="{filename}" onclick="getDirView(event)">
|
|
|
|
<i class="far fa-folder"></i>
|
|
|
|
<i class="far fa-folder-open"></i>
|
|
|
|
<strong>{filename}</strong>
|
|
|
|
</span>
|
2022-03-23 02:50:12 +00:00
|
|
|
</input></div><li>"""
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
|
|
|
output += f"""<li
|
2022-03-04 22:58:18 +00:00
|
|
|
class="tree-item tree-nested d-block tree-ctx-item tree-file"
|
2022-03-01 01:02:30 +00:00
|
|
|
data-path="{dpath}"
|
|
|
|
data-name="{filename}"
|
2022-05-09 17:34:45 +00:00
|
|
|
onclick=""><input type='checkbox' name='root_path' value='{dpath}' checked><span style="margin-right: 6px;">
|
2022-03-01 01:02:30 +00:00
|
|
|
<i class="far fa-file"></i></span></input>{filename}</li>"""
|
|
|
|
|
|
|
|
else:
|
|
|
|
if os.path.isdir(rel):
|
2022-03-23 02:50:12 +00:00
|
|
|
output += f"""<li class="tree-item" data-path="{dpath}">
|
2022-03-01 01:02:30 +00:00
|
|
|
\n<div id="{dpath}" data-path="{dpath}" data-name="{filename}" class="tree-caret tree-ctx-item tree-folder">
|
|
|
|
<input type="checkbox" name="root_path" value="{dpath}">
|
|
|
|
<span id="{dpath}span" class="files-tree-title" data-path="{dpath}" data-name="{filename}" onclick="getDirView(event)">
|
|
|
|
<i class="far fa-folder"></i>
|
|
|
|
<i class="far fa-folder-open"></i>
|
|
|
|
<strong>{filename}</strong>
|
|
|
|
</span>
|
2022-03-23 02:50:12 +00:00
|
|
|
</input></div><li>"""
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
|
|
|
output += f"""<li
|
2022-03-04 22:58:18 +00:00
|
|
|
class="tree-item tree-nested d-block tree-ctx-item tree-file"
|
2022-03-01 01:02:30 +00:00
|
|
|
data-path="{dpath}"
|
|
|
|
data-name="{filename}"
|
|
|
|
onclick=""><input type='checkbox' name='root_path' value='{dpath}'>
|
2022-03-23 06:06:13 +00:00
|
|
|
<span style="margin-right: 6px;"><i class="far fa-file">
|
|
|
|
</i></span></input>{filename}</li>"""
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
self.write(Helpers.get_os_understandable_path(folder) + "\n" + output)
|
2022-03-01 01:02:30 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
elif page == "get_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
path = self.get_argument("path", None)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "get_tree"):
|
2022-03-01 01:02:30 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.validate_traversal(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"], path
|
|
|
|
):
|
|
|
|
self.write(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
+ "\n"
|
2022-04-11 05:23:55 +00:00
|
|
|
+ Helpers.generate_dir(path)
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
self.finish()
|
2021-01-17 17:20:28 +00:00
|
|
|
|
2021-01-18 15:02:38 +00:00
|
|
|
@tornado.web.authenticated
|
2020-09-22 02:01:16 +00:00
|
|
|
def post(self, page):
|
2022-01-15 00:23:50 +00:00
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-01-15 00:23:50 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2021-11-30 19:37:45 +00:00
|
|
|
permissions = {
|
2022-04-14 02:10:25 +00:00
|
|
|
"Commands": EnumPermissionsServer.COMMANDS,
|
|
|
|
"Terminal": EnumPermissionsServer.TERMINAL,
|
|
|
|
"Logs": EnumPermissionsServer.LOGS,
|
|
|
|
"Schedule": EnumPermissionsServer.SCHEDULE,
|
|
|
|
"Backup": EnumPermissionsServer.BACKUP,
|
|
|
|
"Files": EnumPermissionsServer.FILES,
|
|
|
|
"Config": EnumPermissionsServer.CONFIG,
|
|
|
|
"Players": EnumPermissionsServer.PLAYERS,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2020-09-22 02:01:16 +00:00
|
|
|
|
|
|
|
if page == "send_command":
|
2022-03-23 02:50:12 +00:00
|
|
|
command = self.get_body_argument("command", default=None, strip=True)
|
|
|
|
server_id = self.get_argument("id", None)
|
2020-09-22 02:01:16 +00:00
|
|
|
|
|
|
|
if server_id is None:
|
|
|
|
logger.warning("Server ID not found in send_command ajax call")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning("Server ID not found in send_command ajax call")
|
2020-09-22 02:01:16 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
srv_obj = self.controller.get_server_obj(server_id)
|
2020-09-22 02:01:16 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if command == srv_obj.settings["stop_command"]:
|
|
|
|
logger.info(
|
|
|
|
"Stop command detected as terminal input - intercepting."
|
|
|
|
+ f"Starting Crafty's stop process for server with id: {server_id}"
|
|
|
|
)
|
|
|
|
self.controller.management.send_command(
|
|
|
|
exec_user["user_id"], server_id, self.get_remote_ip(), "stop_server"
|
|
|
|
)
|
2022-01-18 20:42:26 +00:00
|
|
|
command = None
|
2022-03-23 02:50:12 +00:00
|
|
|
elif command == "restart":
|
|
|
|
logger.info(
|
|
|
|
"Restart command detected as terminal input - intercepting."
|
|
|
|
+ f"Starting Crafty's stop process for server with id: {server_id}"
|
|
|
|
)
|
|
|
|
self.controller.management.send_command(
|
|
|
|
exec_user["user_id"],
|
|
|
|
server_id,
|
|
|
|
self.get_remote_ip(),
|
|
|
|
"restart_server",
|
|
|
|
)
|
2022-01-18 20:42:26 +00:00
|
|
|
command = None
|
2020-09-22 02:01:16 +00:00
|
|
|
if command:
|
|
|
|
if srv_obj.check_running():
|
|
|
|
srv_obj.send_command(command)
|
2020-09-02 01:30:39 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log(
|
|
|
|
exec_user["user_id"],
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Sent command to "
|
|
|
|
f"{self.controller.servers.get_server_friendly_name(server_id)} "
|
|
|
|
f"terminal: {command}",
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id,
|
|
|
|
self.get_remote_ip(),
|
|
|
|
)
|
2021-09-14 02:02:57 +00:00
|
|
|
|
2022-02-26 23:00:54 +00:00
|
|
|
elif page == "send_order":
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.users.update_server_order(
|
|
|
|
exec_user["user_id"], bleach.clean(self.get_argument("order"))
|
|
|
|
)
|
2022-02-26 23:00:54 +00:00
|
|
|
return
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-11 05:18:22 +00:00
|
|
|
elif page == "backup_now":
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-03-11 05:18:22 +00:00
|
|
|
if server_id is None:
|
2022-03-22 14:24:15 +00:00
|
|
|
logger.error("Server ID is none. Canceling backup!")
|
2022-03-11 05:18:22 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
server = self.controller.get_server_obj(server_id)
|
|
|
|
self.controller.management.add_to_audit_log_raw(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.users.get_user_by_id(exec_user["user_id"])["username"],
|
|
|
|
exec_user["user_id"],
|
|
|
|
server_id,
|
2022-03-11 05:18:22 +00:00
|
|
|
f"Backup now executed for server {server_id} ",
|
2022-03-23 02:50:12 +00:00
|
|
|
source_ip=self.get_remote_ip(),
|
|
|
|
)
|
2022-03-11 05:18:22 +00:00
|
|
|
|
|
|
|
server.backup_server()
|
2022-02-26 23:00:54 +00:00
|
|
|
|
2022-03-02 17:55:49 +00:00
|
|
|
elif page == "clear_comms":
|
2022-03-23 02:50:12 +00:00
|
|
|
if exec_user["superuser"]:
|
2022-03-02 17:55:49 +00:00
|
|
|
self.controller.clear_unexecuted_commands()
|
|
|
|
return
|
|
|
|
|
2021-09-13 19:03:47 +00:00
|
|
|
elif page == "kill":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Commands"] in user_perms:
|
2022-01-15 00:23:50 +00:00
|
|
|
if not superuser:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Commands")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2021-09-13 19:03:47 +00:00
|
|
|
svr = self.controller.get_server_obj(server_id)
|
2021-09-25 19:29:28 +00:00
|
|
|
try:
|
|
|
|
svr.kill()
|
2022-03-19 01:50:09 +00:00
|
|
|
time.sleep(5)
|
|
|
|
svr.cleanup_server_object()
|
|
|
|
svr.record_server_stats()
|
2021-09-25 19:29:28 +00:00
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
|
|
|
f"Could not find PID for requested termsig. Full error: {e}"
|
|
|
|
)
|
2021-09-13 19:03:47 +00:00
|
|
|
return
|
2021-11-23 21:11:23 +00:00
|
|
|
elif page == "eula":
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2021-11-23 21:11:23 +00:00
|
|
|
svr = self.controller.get_server_obj(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
svr.agree_eula(exec_user["user_id"])
|
2021-09-13 19:03:47 +00:00
|
|
|
|
2021-11-29 21:22:46 +00:00
|
|
|
elif page == "restore_backup":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Backup"] in user_perms:
|
2022-01-15 00:23:50 +00:00
|
|
|
if not superuser:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Backups")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = bleach.clean(self.get_argument("id", None))
|
|
|
|
zip_name = bleach.clean(self.get_argument("zip_file", None))
|
2021-11-29 21:22:46 +00:00
|
|
|
svr_obj = self.controller.servers.get_server_obj(server_id)
|
|
|
|
server_data = self.controller.servers.get_server_data_by_id(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
if server_data["type"] == "minecraft-java":
|
2022-03-08 02:36:23 +00:00
|
|
|
backup_path = svr_obj.backup_path
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.validate_traversal(backup_path, zip_name):
|
2022-04-14 02:10:25 +00:00
|
|
|
temp_dir = Helpers.unzip_backup_archive(backup_path, zip_name)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server = self.controller.import_zip_server(
|
|
|
|
svr_obj.server_name,
|
2022-04-14 02:10:25 +00:00
|
|
|
temp_dir,
|
2022-03-23 02:50:12 +00:00
|
|
|
server_data["executable"],
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
server_data["server_port"],
|
|
|
|
)
|
2022-03-08 02:36:23 +00:00
|
|
|
new_server_id = new_server
|
|
|
|
new_server = self.controller.get_server_data(new_server)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.rename_backup_dir(
|
|
|
|
server_id, new_server_id, new_server["server_uuid"]
|
|
|
|
)
|
2022-03-08 02:36:23 +00:00
|
|
|
self.controller.remove_server(server_id, True)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/panel/dashboard")
|
2022-03-08 02:36:23 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
backup_path = svr_obj.backup_path
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.validate_traversal(backup_path, zip_name):
|
2022-04-14 02:10:25 +00:00
|
|
|
temp_dir = Helpers.unzip_backup_archive(backup_path, zip_name)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server = self.controller.import_bedrock_zip_server(
|
|
|
|
svr_obj.server_name,
|
2022-04-14 02:10:25 +00:00
|
|
|
temp_dir,
|
2022-03-23 02:50:12 +00:00
|
|
|
server_data["executable"],
|
|
|
|
server_data["server_port"],
|
|
|
|
)
|
2022-03-08 02:36:23 +00:00
|
|
|
new_server_id = new_server
|
|
|
|
new_server = self.controller.get_server_data(new_server)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.rename_backup_dir(
|
|
|
|
server_id, new_server_id, new_server["server_uuid"]
|
|
|
|
)
|
2022-03-08 02:36:23 +00:00
|
|
|
self.controller.remove_server(server_id, True)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/panel/dashboard")
|
2022-01-09 23:04:54 +00:00
|
|
|
|
|
|
|
elif page == "unzip_server":
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_file_exists(path):
|
2022-04-14 23:45:13 +00:00
|
|
|
self.helper.unzip_server(path, exec_user["user_id"])
|
2022-03-01 18:27:41 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id = exec_user["user_id"]
|
2022-03-01 18:27:41 +00:00
|
|
|
if user_id:
|
|
|
|
time.sleep(5)
|
|
|
|
user_lang = self.controller.users.get_user_lang_by_id(user_id)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.websocket_helper.broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
2022-04-11 10:08:36 +00:00
|
|
|
{
|
|
|
|
"error": self.helper.translation.translate(
|
|
|
|
"error", "no-file", user_lang
|
|
|
|
)
|
|
|
|
},
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-10 03:21:28 +00:00
|
|
|
return
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-01 01:02:30 +00:00
|
|
|
elif page == "backup_select":
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.backup_select(path, exec_user["user_id"])
|
2022-03-01 01:02:30 +00:00
|
|
|
return
|
|
|
|
|
2021-01-18 15:02:38 +00:00
|
|
|
@tornado.web.authenticated
|
2021-01-17 17:20:28 +00:00
|
|
|
def delete(self, page):
|
2022-01-15 00:23:50 +00:00
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-01-15 00:23:50 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2021-11-30 19:37:45 +00:00
|
|
|
permissions = {
|
2022-04-14 02:10:25 +00:00
|
|
|
"Commands": EnumPermissionsServer.COMMANDS,
|
|
|
|
"Terminal": EnumPermissionsServer.TERMINAL,
|
|
|
|
"Logs": EnumPermissionsServer.LOGS,
|
|
|
|
"Schedule": EnumPermissionsServer.SCHEDULE,
|
|
|
|
"Backup": EnumPermissionsServer.BACKUP,
|
|
|
|
"Files": EnumPermissionsServer.FILES,
|
|
|
|
"Config": EnumPermissionsServer.CONFIG,
|
|
|
|
"Players": EnumPermissionsServer.PLAYERS,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-01-11 05:26:33 +00:00
|
|
|
if page == "del_task":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Schedule"] in user_perms:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Tasks")
|
2022-01-11 05:26:33 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
sch_id = self.get_argument("schedule_id", "-404")
|
2022-01-11 05:26:33 +00:00
|
|
|
self.tasks_manager.remove_job(sch_id)
|
|
|
|
|
2021-11-30 19:37:45 +00:00
|
|
|
if page == "del_backup":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Backup"] in user_perms:
|
2022-01-15 00:23:50 +00:00
|
|
|
if not superuser:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Backups")
|
|
|
|
return
|
2022-04-11 05:23:55 +00:00
|
|
|
file_path = Helpers.get_os_understandable_path(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.get_body_argument("file_path", default=None, strip=True)
|
|
|
|
)
|
|
|
|
server_id = self.get_argument("id", None)
|
2021-01-17 17:20:28 +00:00
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning(f"Delete {file_path} for server {server_id}")
|
2021-01-17 17:20:28 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "del_backup"):
|
2021-11-20 02:09:09 +00:00
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
2021-01-17 17:20:28 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
server_info = self.controller.servers.get_server_data_by_id(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
if not (
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.in_path(
|
|
|
|
Helpers.get_os_understandable_path(server_info["path"]), file_path
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
or Helpers.in_path(
|
|
|
|
Helpers.get_os_understandable_path(server_info["backup_path"]),
|
2022-03-23 02:50:12 +00:00
|
|
|
file_path,
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
) or not Helpers.check_file_exists(os.path.abspath(file_path)):
|
2022-02-27 17:26:54 +00:00
|
|
|
logger.warning(f"Invalid path in del_backup ajax call ({file_path})")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning(f"Invalid path in del_backup ajax call ({file_path})")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2021-01-17 17:20:28 +00:00
|
|
|
|
|
|
|
# Delete the file
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.validate_traversal(
|
2022-04-11 10:08:36 +00:00
|
|
|
Helpers.get_os_understandable_path(server_info["backup_path"]),
|
|
|
|
file_path,
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
2022-01-08 23:03:45 +00:00
|
|
|
os.remove(file_path)
|
2021-01-17 17:20:28 +00:00
|
|
|
|
2021-08-18 22:50:53 +00:00
|
|
|
elif page == "delete_server":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Config"] in user_perms:
|
2022-01-15 00:23:50 +00:00
|
|
|
if not superuser:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Config")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Removing server from panel for server: "
|
|
|
|
f"{self.controller.servers.get_server_friendly_name(server_id)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 04:53:44 +00:00
|
|
|
|
|
|
|
server_data = self.controller.get_server_data(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_name = server_data["server_name"]
|
2022-01-15 04:53:44 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log(
|
|
|
|
exec_user["user_id"],
|
|
|
|
f"Deleted server {server_id} named {server_name}",
|
|
|
|
server_id,
|
|
|
|
self.get_remote_ip(),
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-01-15 04:53:44 +00:00
|
|
|
self.tasks_manager.remove_all_server_tasks(server_id)
|
2021-08-18 22:50:53 +00:00
|
|
|
self.controller.remove_server(server_id, False)
|
|
|
|
|
|
|
|
elif page == "delete_server_files":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Config"] in user_perms:
|
2022-01-15 00:23:50 +00:00
|
|
|
if not superuser:
|
2022-01-26 01:45:30 +00:00
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Config")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Removing server and all associated files for server: "
|
|
|
|
f"{self.controller.servers.get_server_friendly_name(server_id)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 04:53:44 +00:00
|
|
|
|
|
|
|
server_data = self.controller.get_server_data(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_name = server_data["server_name"]
|
2022-01-15 04:53:44 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log(
|
|
|
|
exec_user["user_id"],
|
|
|
|
f"Deleted server {server_id} named {server_name}",
|
|
|
|
server_id,
|
|
|
|
self.get_remote_ip(),
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-01-15 04:53:44 +00:00
|
|
|
self.tasks_manager.remove_all_server_tasks(server_id)
|
2021-08-18 22:50:53 +00:00
|
|
|
self.controller.remove_server(server_id, True)
|
|
|
|
|
2021-03-26 13:57:50 +00:00
|
|
|
def check_server_id(self, server_id, page_name):
|
|
|
|
if server_id is None:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Server ID not defined in {page_name} ajax call ({server_id})"
|
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Server ID not defined in {page_name} ajax call ({server_id})"
|
|
|
|
)
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2021-03-26 13:57:50 +00:00
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
# does this server id exist?
|
2021-09-08 22:01:10 +00:00
|
|
|
if not self.controller.servers.server_id_exists(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Server ID not found in {page_name} ajax call ({server_id})"
|
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Server ID not found in {page_name} ajax call ({server_id})"
|
|
|
|
)
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2021-03-26 13:57:50 +00:00
|
|
|
return True
|