2020-08-23 22:43:28 +00:00
|
|
|
import sys
|
2020-08-19 17:54:10 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.web.base_handler import BaseHandler
|
2020-08-31 17:46:25 +00:00
|
|
|
from app.classes.shared.controller import controller
|
2020-08-23 22:43:28 +00:00
|
|
|
from app.classes.shared.models import db_helper, Servers
|
|
|
|
from app.classes.minecraft.serverjars import server_jar_obj
|
2020-08-27 22:30:56 +00:00
|
|
|
from app.classes.minecraft.stats import stats
|
2020-08-31 15:29:32 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
2020-08-19 17:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-08-23 22:43:28 +00:00
|
|
|
try:
|
|
|
|
import tornado.web
|
|
|
|
import tornado.escape
|
|
|
|
import bleach
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
console.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-08-19 17:54:10 +00:00
|
|
|
|
|
|
|
class ServerHandler(BaseHandler):
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def get(self, page):
|
|
|
|
# name = tornado.escape.json_decode(self.current_user)
|
|
|
|
user_data = json.loads(self.get_secure_cookie("user_data"))
|
|
|
|
|
|
|
|
template = "public/404.html"
|
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
defined_servers = controller.list_defined_servers()
|
|
|
|
|
2020-08-19 17:54:10 +00:00
|
|
|
page_data = {
|
2020-09-22 20:17:17 +00:00
|
|
|
'version_data': helper.get_version_string(),
|
2020-08-19 17:54:10 +00:00
|
|
|
'user_data': user_data,
|
|
|
|
'server_stats': {
|
|
|
|
'total': len(controller.list_defined_servers()),
|
|
|
|
'running': len(controller.list_running_servers()),
|
|
|
|
'stopped': (len(controller.list_defined_servers()) - len(controller.list_running_servers()))
|
|
|
|
},
|
2020-08-24 23:11:17 +00:00
|
|
|
'hosts_data': db_helper.get_latest_hosts_stats(),
|
|
|
|
'menu_servers': defined_servers,
|
2020-08-31 15:29:32 +00:00
|
|
|
'show_contribute': helper.get_setting("show_contribute_link", True)
|
2020-08-19 17:54:10 +00:00
|
|
|
}
|
2020-08-23 22:43:28 +00:00
|
|
|
|
|
|
|
if page == "step1":
|
|
|
|
|
|
|
|
page_data['server_types'] = server_jar_obj.get_serverjar_data()
|
|
|
|
template = "server/wizard.html"
|
|
|
|
|
|
|
|
self.render(
|
|
|
|
template,
|
|
|
|
data=page_data
|
|
|
|
)
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def post(self, page):
|
|
|
|
|
|
|
|
user_data = json.loads(self.get_secure_cookie("user_data"))
|
|
|
|
|
|
|
|
template = "public/404.html"
|
|
|
|
page_data = {
|
|
|
|
'version_data': "version_data_here",
|
|
|
|
'user_data': user_data,
|
2020-08-31 15:29:32 +00:00
|
|
|
'show_contribute': helper.get_setting("show_contribute_link", True)
|
2020-08-23 22:43:28 +00:00
|
|
|
}
|
2020-08-19 17:54:10 +00:00
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
if page == "command":
|
|
|
|
server_id = bleach.clean(self.get_argument("id", None))
|
|
|
|
command = bleach.clean(self.get_argument("command", None))
|
|
|
|
|
|
|
|
if server_id is not None:
|
2020-08-31 20:16:45 +00:00
|
|
|
db_helper.send_command(user_data['user_id'], server_id, self.get_remote_ip(), command)
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2020-08-23 22:43:28 +00:00
|
|
|
if page == "step1":
|
|
|
|
|
|
|
|
server = bleach.clean(self.get_argument('server', ''))
|
|
|
|
server_name = bleach.clean(self.get_argument('server_name', ''))
|
|
|
|
min_mem = bleach.clean(self.get_argument('min_memory', ''))
|
|
|
|
max_mem = bleach.clean(self.get_argument('max_memory', ''))
|
|
|
|
port = bleach.clean(self.get_argument('port', ''))
|
2020-09-14 20:01:38 +00:00
|
|
|
import_type = bleach.clean(self.get_argument('create_type', ''))
|
|
|
|
import_server_path = bleach.clean(self.get_argument('server_path', ''))
|
|
|
|
import_server_jar = bleach.clean(self.get_argument('server_jar', ''))
|
2020-08-23 22:43:28 +00:00
|
|
|
server_parts = server.split("|")
|
|
|
|
|
2020-09-14 20:01:38 +00:00
|
|
|
if import_type == 'import_jar':
|
|
|
|
good_path = controller.verify_jar_server(import_server_path, import_server_jar)
|
|
|
|
|
|
|
|
if not good_path:
|
|
|
|
self.redirect("/panel/error?error=Server path or Server Jar not found!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
new_server_id = controller.import_jar_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)
|
2020-08-27 22:30:56 +00:00
|
|
|
|
2020-08-31 20:16:45 +00:00
|
|
|
if new_server_id:
|
|
|
|
db_helper.add_to_audit_log(user_data['user_id'],
|
|
|
|
"Created server {} named {}".format(server, server_name),
|
|
|
|
new_server_id,
|
|
|
|
self.get_remote_ip())
|
2020-09-14 20:01:38 +00:00
|
|
|
else:
|
|
|
|
logger.error("Unable to create server")
|
|
|
|
console.error("Unable to create server")
|
|
|
|
|
|
|
|
stats.record_stats()
|
|
|
|
self.redirect("/panel/dashboard")
|
2020-08-23 22:43:28 +00:00
|
|
|
|
2020-08-19 17:54:10 +00:00
|
|
|
self.render(
|
|
|
|
template,
|
|
|
|
data=page_data
|
|
|
|
)
|