2020-08-13 14:38:36 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import tornado.web
|
|
|
|
import tornado.escape
|
|
|
|
import bleach
|
2020-12-14 21:52:46 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
2020-08-13 14:38:36 +00:00
|
|
|
|
|
|
|
from app.classes.shared.console import console
|
2020-08-17 02:47:53 +00:00
|
|
|
from app.classes.shared.models import Users, installer
|
2020-08-13 14:38:36 +00:00
|
|
|
from app.classes.web.base_handler import BaseHandler
|
2020-08-31 17:46:25 +00:00
|
|
|
from app.classes.shared.controller import controller
|
2020-09-04 16:35:06 +00:00
|
|
|
from app.classes.shared.models import db_helper, Servers
|
2020-08-28 18:38:42 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
class PanelHandler(BaseHandler):
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def get(self, page):
|
|
|
|
user_data = json.loads(self.get_secure_cookie("user_data"))
|
2020-09-01 17:29:39 +00:00
|
|
|
error = bleach.clean(self.get_argument('error', "WTF Error!"))
|
2020-08-13 14:38:36 +00:00
|
|
|
|
|
|
|
template = "panel/denied.html"
|
|
|
|
|
2020-12-14 21:52:46 +00:00
|
|
|
now = time.time()
|
|
|
|
formatted_time = str(datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
|
2020-08-24 17:08:17 +00:00
|
|
|
defined_servers = controller.list_defined_servers()
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
page_data = {
|
2020-09-23 01:43:22 +00:00
|
|
|
# todo: make this actually pull and compare version data
|
2020-09-23 01:42:41 +00:00
|
|
|
'update_available': False,
|
2020-09-01 17:29:39 +00:00
|
|
|
'version_data': helper.get_version_string(),
|
2020-08-19 01:04:43 +00:00
|
|
|
'user_data': user_data,
|
|
|
|
'server_stats': {
|
2020-08-24 17:08:17 +00:00
|
|
|
'total': len(defined_servers),
|
2020-08-19 01:04:43 +00:00
|
|
|
'running': len(controller.list_running_servers()),
|
|
|
|
'stopped': (len(controller.list_defined_servers()) - len(controller.list_running_servers()))
|
|
|
|
},
|
2020-08-24 17:08:17 +00:00
|
|
|
'menu_servers': defined_servers,
|
2020-08-28 18:38:42 +00:00
|
|
|
'hosts_data': db_helper.get_latest_hosts_stats(),
|
2020-09-01 17:29:39 +00:00
|
|
|
'show_contribute': helper.get_setting("show_contribute_link", True),
|
2020-12-14 21:52:46 +00:00
|
|
|
'error': error,
|
|
|
|
'time': formatted_time
|
2020-08-13 14:38:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:21:19 +00:00
|
|
|
# if no servers defined, let's go to the build server area
|
|
|
|
if page_data['server_stats']['total'] == 0:
|
2020-08-19 17:54:10 +00:00
|
|
|
self.set_status(301)
|
|
|
|
self.redirect("/server/step1")
|
2020-08-24 02:17:36 +00:00
|
|
|
return False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
if page == 'unauthorized':
|
|
|
|
template = "panel/denied.html"
|
|
|
|
|
2020-09-01 17:29:39 +00:00
|
|
|
elif page == "error":
|
|
|
|
template = "public/error.html"
|
|
|
|
|
2020-08-28 03:21:55 +00:00
|
|
|
elif page == 'credits':
|
|
|
|
template = "panel/credits.html"
|
|
|
|
|
2020-08-28 18:38:42 +00:00
|
|
|
elif page == 'contribute':
|
|
|
|
template = "panel/contribute.html"
|
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
elif page == "remove_server":
|
|
|
|
server_id = self.get_argument('id', None)
|
|
|
|
server_data = controller.get_server_data(server_id)
|
|
|
|
server_name = server_data['server_name']
|
|
|
|
|
|
|
|
db_helper.add_to_audit_log(user_data['user_id'],
|
|
|
|
"Deleted server {} named {}".format(server_id, server_name),
|
|
|
|
server_id,
|
|
|
|
self.get_remote_ip())
|
|
|
|
|
|
|
|
controller.remove_server(server_id)
|
|
|
|
self.redirect("/panel/dashboard")
|
|
|
|
return
|
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
elif page == 'dashboard':
|
2020-08-24 17:08:17 +00:00
|
|
|
page_data['servers'] = db_helper.get_all_servers_stats()
|
2020-08-27 22:30:56 +00:00
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
for s in page_data['servers']:
|
|
|
|
try:
|
|
|
|
data = json.loads(s['int_ping_results'])
|
|
|
|
s['int_ping_results'] = data
|
|
|
|
except:
|
|
|
|
pass
|
2020-08-28 03:21:55 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
template = "panel/dashboard.html"
|
|
|
|
|
2020-08-31 17:46:25 +00:00
|
|
|
elif page == 'server_detail':
|
2020-09-01 17:29:39 +00:00
|
|
|
server_id = self.get_argument('id', None)
|
|
|
|
subpage = bleach.clean(self.get_argument('subpage', ""))
|
|
|
|
|
|
|
|
if server_id is None:
|
|
|
|
self.redirect("/panel/error?error=Invalid Server ID")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
# does this server id exist?
|
|
|
|
if not db_helper.server_id_exists(server_id):
|
|
|
|
self.redirect("/panel/error?error=Invalid Server ID")
|
|
|
|
return False
|
|
|
|
|
2020-09-04 04:02:03 +00:00
|
|
|
valid_subpages = ['term', 'logs', 'config']
|
2020-09-01 17:29:39 +00:00
|
|
|
|
|
|
|
if subpage not in valid_subpages:
|
|
|
|
subpage = 'term'
|
|
|
|
|
|
|
|
# server_data isn't needed since the server_stats also pulls server data
|
|
|
|
# page_data['server_data'] = db_helper.get_server_data_by_id(server_id)
|
|
|
|
page_data['server_stats'] = db_helper.get_server_stats_by_id(server_id)
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2020-09-01 17:29:39 +00:00
|
|
|
# template = "panel/server_details.html"
|
|
|
|
template = "panel/server_{subpage}.html".format(subpage=subpage)
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2020-12-06 20:42:49 +00:00
|
|
|
elif page == 'panel_config':
|
|
|
|
page_data['users'] = db_helper.get_all_users()
|
|
|
|
# print(page_data['users'])
|
|
|
|
template = "panel/panel_config.html"
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
self.render(
|
|
|
|
template,
|
|
|
|
data=page_data
|
2020-08-24 17:08:17 +00:00
|
|
|
)
|
2020-09-04 16:35:06 +00:00
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def post(self, page):
|
|
|
|
|
|
|
|
if page == 'server_detail':
|
|
|
|
server_id = self.get_argument('id', None)
|
|
|
|
server_name = self.get_argument('server_name', None)
|
|
|
|
server_path = self.get_argument('server_path', None)
|
|
|
|
log_path = self.get_argument('log_path', None)
|
|
|
|
executable = self.get_argument('executable', None)
|
|
|
|
execution_command = self.get_argument('execution_command', None)
|
|
|
|
stop_command = self.get_argument('stop_command', None)
|
2020-09-04 18:05:42 +00:00
|
|
|
auto_start_delay = self.get_argument('auto_start_delay', '10')
|
2020-09-22 16:11:16 +00:00
|
|
|
server_ip = self.get_argument('server_ip', None)
|
2020-09-04 16:35:06 +00:00
|
|
|
server_port = self.get_argument('server_port', None)
|
2020-09-04 18:05:42 +00:00
|
|
|
auto_start = int(float(self.get_argument('auto_start', '0')))
|
|
|
|
crash_detection = int(float(self.get_argument('crash_detection', '0')))
|
2020-09-04 16:35:06 +00:00
|
|
|
subpage = self.get_argument('subpage', None)
|
|
|
|
|
|
|
|
if server_id is None:
|
|
|
|
self.redirect("/panel/error?error=Invalid Server ID")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
# does this server id exist?
|
|
|
|
if not db_helper.server_id_exists(server_id):
|
|
|
|
self.redirect("/panel/error?error=Invalid Server ID")
|
|
|
|
return False
|
|
|
|
|
|
|
|
Servers.update({
|
|
|
|
Servers.server_name: server_name,
|
|
|
|
Servers.path: server_path,
|
|
|
|
Servers.log_path: log_path,
|
|
|
|
Servers.executable: executable,
|
|
|
|
Servers.execution_command: execution_command,
|
|
|
|
Servers.stop_command: stop_command,
|
|
|
|
Servers.auto_start_delay: auto_start_delay,
|
2020-09-22 16:11:16 +00:00
|
|
|
Servers.server_ip: server_ip,
|
2020-09-04 16:35:06 +00:00
|
|
|
Servers.server_port: server_port,
|
|
|
|
Servers.auto_start: auto_start,
|
|
|
|
Servers.crash_detection: crash_detection,
|
|
|
|
}).where(Servers.server_id == server_id).execute()
|
|
|
|
|
2020-10-07 13:19:47 +00:00
|
|
|
controller.refresh_server_settings(server_id)
|
|
|
|
|
2020-09-04 18:05:42 +00:00
|
|
|
user_data = json.loads(self.get_secure_cookie("user_data"))
|
|
|
|
|
|
|
|
db_helper.add_to_audit_log(user_data['user_id'],
|
|
|
|
"Edited server {} named {}".format(server_id, server_name),
|
|
|
|
server_id,
|
|
|
|
self.get_remote_ip())
|
|
|
|
|
2020-09-04 16:35:06 +00:00
|
|
|
self.redirect("/panel/server_detail?id={}&subpage=config".format(server_id))
|