2020-08-23 22:43:28 +00:00
import sys
2020-08-19 17:54:10 +00:00
import json
import logging
2021-01-28 18:46:13 +00:00
import os
import shutil
2020-08-19 17:54:10 +00:00
from app . classes . shared . console import console
from app . classes . web . base_handler import BaseHandler
2021-09-08 22:01:10 +00:00
from app . classes . models . crafty_permissions import Enum_Permissions_Crafty
2020-08-23 22:43:28 +00:00
from app . classes . minecraft . serverjars import server_jar_obj
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 :
2021-04-17 20:34:13 +00:00
logger . critical ( " Import Error: Unable to load {} module " . format ( e . name ) , exc_info = True )
console . critical ( " Import Error: Unable to load {} module " . format ( e . name ) )
2020-08-23 22:43:28 +00:00
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)
2021-04-03 17:36:01 +00:00
exec_user_data = json . loads ( self . get_secure_cookie ( " user_data " ) )
exec_user_id = exec_user_data [ ' user_id ' ]
2021-09-08 22:01:10 +00:00
exec_user = self . controller . users . get_user_by_id ( exec_user_id )
2021-10-09 08:32:52 +00:00
2021-04-03 17:39:25 +00:00
exec_user_role = set ( )
2021-04-03 17:36:01 +00:00
if exec_user [ ' superuser ' ] == 1 :
defined_servers = self . controller . list_defined_servers ( )
2021-04-03 17:39:25 +00:00
exec_user_role . add ( " Super User " )
2021-09-08 22:20:38 +00:00
exec_user_crafty_permissions = self . controller . crafty_perms . list_defined_crafty_permissions ( )
2021-11-17 18:08:41 +00:00
list_roles = [ ]
for role in self . controller . roles . get_all_roles ( ) :
list_roles . append ( self . controller . roles . get_role ( role . role_id ) )
2021-03-13 22:12:42 +00:00
else :
2021-09-06 17:27:01 +00:00
exec_user_crafty_permissions = self . controller . crafty_perms . get_crafty_permissions_list ( exec_user_id )
2021-09-08 22:01:10 +00:00
defined_servers = self . controller . servers . get_authorized_servers ( exec_user_id )
2021-11-17 18:08:41 +00:00
list_roles = [ ]
2021-04-03 17:36:01 +00:00
for r in exec_user [ ' roles ' ] :
2021-09-08 22:01:10 +00:00
role = self . controller . roles . get_role ( r )
2021-04-03 17:39:25 +00:00
exec_user_role . add ( role [ ' role_name ' ] )
2021-11-17 18:08:41 +00:00
list_roles . append ( self . controller . roles . get_role ( role [ ' role_id ' ] ) )
2020-08-19 17:54:10 +00:00
template = " public/404.html "
page_data = {
2020-09-22 20:17:17 +00:00
' version_data ' : helper . get_version_string ( ) ,
2021-04-03 17:36:01 +00:00
' user_data ' : exec_user_data ,
2021-04-03 18:18:43 +00:00
' user_role ' : exec_user_role ,
2021-11-17 18:08:41 +00:00
' roles ' : list_roles ,
2021-08-24 21:07:00 +00:00
' user_crafty_permissions ' : exec_user_crafty_permissions ,
' crafty_permissions ' : {
' Server_Creation ' : Enum_Permissions_Crafty . Server_Creation ,
' User_Config ' : Enum_Permissions_Crafty . User_Config ,
' Roles_Config ' : Enum_Permissions_Crafty . Roles_Config ,
} ,
2020-08-19 17:54:10 +00:00
' server_stats ' : {
2021-03-22 04:02:18 +00:00
' total ' : len ( self . controller . list_defined_servers ( ) ) ,
' running ' : len ( self . controller . list_running_servers ( ) ) ,
' stopped ' : ( len ( self . controller . list_defined_servers ( ) ) - len ( self . controller . list_running_servers ( ) ) )
2020-08-19 17:54:10 +00:00
} ,
2021-09-08 22:01:10 +00:00
' hosts_data ' : self . controller . management . get_latest_hosts_stats ( ) ,
2020-08-24 23:11:17 +00:00
' menu_servers ' : defined_servers ,
2021-10-09 08:59:54 +00:00
' show_contribute ' : helper . get_setting ( " show_contribute_link " , True ) ,
' lang ' : self . controller . users . get_user_lang_by_id ( exec_user_id )
2020-08-19 17:54:10 +00:00
}
2021-11-17 06:17:36 +00:00
if exec_user [ ' superuser ' ] == 1 :
2021-11-17 18:08:41 +00:00
page_data [ ' roles ' ] = list_roles
2020-08-23 22:43:28 +00:00
if page == " step1 " :
2021-10-09 08:32:52 +00:00
if not exec_user [ ' superuser ' ] and not self . controller . crafty_perms . can_create_server ( exec_user_id ) :
2021-08-24 22:23:33 +00:00
self . redirect ( " /panel/error?error=Unauthorized access: not a server creator or server limit reached " )
2021-08-24 21:07:00 +00:00
return
2021-06-02 18:17:50 +00:00
page_data [ ' server_types ' ] = server_jar_obj . get_serverjar_data_sorted ( )
2021-08-23 22:24:40 +00:00
page_data [ ' js_server_types ' ] = json . dumps ( server_jar_obj . get_serverjar_data_sorted ( ) )
2020-08-23 22:43:28 +00:00
template = " server/wizard.html "
self . render (
template ,
2021-03-26 13:57:50 +00:00
data = page_data ,
translate = self . translator . translate ,
2020-08-23 22:43:28 +00:00
)
@tornado.web.authenticated
def post ( self , page ) :
2021-04-03 17:36:01 +00:00
exec_user_data = json . loads ( self . get_secure_cookie ( " user_data " ) )
exec_user_id = exec_user_data [ ' user_id ' ]
2021-09-08 22:01:10 +00:00
exec_user = self . controller . users . get_user_by_id ( exec_user_id )
2020-08-23 22:43:28 +00:00
template = " public/404.html "
page_data = {
' version_data ' : " version_data_here " ,
2021-04-03 17:36:01 +00:00
' user_data ' : exec_user_data ,
2021-10-09 08:59:54 +00:00
' show_contribute ' : helper . get_setting ( " show_contribute_link " , True ) ,
' lang ' : self . controller . users . get_user_lang_by_id ( exec_user_id )
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 :
2021-01-28 18:46:13 +00:00
if command == " clone_server " :
def is_name_used ( name ) :
2021-09-08 22:01:10 +00:00
for server in self . controller . servers . get_all_defined_servers ( ) :
2021-01-28 18:46:13 +00:00
if server [ ' server_name ' ] == name :
return True
2021-07-30 16:20:01 +00:00
return
2021-10-09 08:32:52 +00:00
2021-09-08 22:01:10 +00:00
server_data = self . controller . servers . get_server_data_by_id ( server_id )
2021-01-28 18:46:13 +00:00
server_uuid = server_data . get ( ' server_uuid ' )
new_server_name = server_data . get ( ' server_name ' ) + " (Copy) "
name_counter = 1
while is_name_used ( new_server_name ) :
name_counter + = 1
new_server_name = server_data . get ( ' server_name ' ) + " (Copy {} ) " . format ( name_counter )
new_server_uuid = helper . create_uuid ( )
while os . path . exists ( os . path . join ( helper . servers_dir , new_server_uuid ) ) :
new_server_uuid = helper . create_uuid ( )
new_server_path = os . path . join ( helper . servers_dir , new_server_uuid )
# copy the old server
shutil . copytree ( server_data . get ( ' path ' ) , new_server_path )
# TODO get old server DB data to individual variables
stop_command = server_data . get ( ' stop_command ' )
new_server_command = str ( server_data . get ( ' execution_command ' ) ) . replace ( server_uuid , new_server_uuid )
new_executable = server_data . get ( ' executable ' )
new_server_log_file = str ( server_data . get ( ' log_path ' ) ) . replace ( server_uuid , new_server_uuid )
auto_start = server_data . get ( ' auto_start ' )
auto_start_delay = server_data . get ( ' auto_start_delay ' )
crash_detection = server_data . get ( ' crash_detection ' )
server_port = server_data . get ( ' server_port ' )
2021-09-08 22:01:10 +00:00
self . controller . servers . create_server ( new_server_name , new_server_uuid , new_server_path , " " , new_server_command , new_executable , new_server_log_file , stop_command , server_port )
2021-01-28 18:46:13 +00:00
2021-03-22 04:02:18 +00:00
self . controller . init_all_servers ( )
2021-01-28 18:46:13 +00:00
return
2021-10-09 08:32:52 +00:00
2021-09-08 22:01:10 +00:00
self . controller . management . send_command ( exec_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 " :
2021-11-17 18:08:41 +00:00
if not exec_user [ ' superuser ' ] :
user_roles = self . controller . roles . get_all_roles ( )
else :
user_roles = self . controller . roles . get_all_roles ( )
2020-08-23 22:43:28 +00:00
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 ( " | " )
2021-11-17 18:08:41 +00:00
captured_roles = [ ]
for role in user_roles :
try :
if bleach . clean ( self . get_argument ( str ( role ) , ' ' ) ) == " on " :
captured_roles . append ( role )
except :
pass
2020-08-23 22:43:28 +00:00
2021-05-02 15:57:53 +00:00
if not server_name :
self . redirect ( " /panel/error?error=Server name cannot be empty! " )
2021-07-30 16:20:01 +00:00
return
2021-05-02 15:57:53 +00:00
2020-09-14 20:01:38 +00:00
if import_type == ' import_jar ' :
2021-03-22 04:02:18 +00:00
good_path = self . controller . verify_jar_server ( import_server_path , import_server_jar )
2020-09-14 20:01:38 +00:00
if not good_path :
self . redirect ( " /panel/error?error=Server path or Server Jar not found! " )
2021-07-30 16:20:01 +00:00
return
2020-09-14 20:01:38 +00:00
2021-03-22 04:02:18 +00:00
new_server_id = self . controller . import_jar_server ( server_name , import_server_path , import_server_jar , min_mem , max_mem , port )
2021-09-08 22:01:10 +00:00
self . controller . management . add_to_audit_log ( exec_user_data [ ' user_id ' ] ,
2021-04-19 10:36:12 +00:00
" imported a jar server named \" {} \" " . format ( server_name ) , # Example: Admin imported a server named "old creative"
new_server_id ,
self . get_remote_ip ( ) )
2020-12-12 17:54:10 +00:00
elif import_type == ' import_zip ' :
2021-04-19 10:36:12 +00:00
# here import_server_path means the zip path
2021-03-22 04:02:18 +00:00
good_path = self . controller . verify_zip_server ( import_server_path )
2020-12-12 17:54:10 +00:00
if not good_path :
self . redirect ( " /panel/error?error=Zip file not found! " )
2021-07-30 16:20:01 +00:00
return
2020-09-14 20:01:38 +00:00
2021-03-22 04:02:18 +00:00
new_server_id = self . controller . import_zip_server ( server_name , import_server_path , import_server_jar , min_mem , max_mem , port )
2020-12-12 19:35:41 +00:00
if new_server_id == " false " :
2021-04-19 10:36:12 +00:00
self . redirect ( " /panel/error?error=Zip file not accessible! You can fix this permissions issue with sudo chown -R crafty:crafty {} And sudo chmod 2775 -R {} " . format ( import_server_path , import_server_path ) )
2021-07-30 16:20:01 +00:00
return
2021-09-08 22:01:10 +00:00
self . controller . management . add_to_audit_log ( exec_user_data [ ' user_id ' ] ,
2021-04-19 10:36:12 +00:00
" imported a zip server named \" {} \" " . format ( server_name ) , # Example: Admin imported a server named "old creative"
2020-08-31 20:16:45 +00:00
new_server_id ,
self . get_remote_ip ( ) )
2021-04-19 10:36:12 +00:00
else :
if len ( server_parts ) != 2 :
self . redirect ( " /panel/error?error=Invalid server data " )
2021-07-30 16:20:01 +00:00
return
2021-04-19 10:36:12 +00:00
server_type , server_version = server_parts
2021-08-22 09:06:44 +00:00
# TODO: add server type check here and call the correct server add functions if not a jar
2021-09-08 22:20:38 +00:00
role_ids = self . controller . users . get_user_roles_id ( exec_user_id )
2021-04-19 10:36:12 +00:00
new_server_id = self . controller . create_jar_server ( server_type , server_version , server_name , min_mem , max_mem , port )
2021-09-08 22:01:10 +00:00
self . controller . management . add_to_audit_log ( exec_user_data [ ' user_id ' ] ,
2021-04-19 10:36:12 +00:00
" created a {} {} server named \" {} \" " . format ( server_version , str ( server_type ) . capitalize ( ) , server_name ) , # Example: Admin created a 1.16.5 Bukkit server named "survival"
2021-04-03 23:21:05 +00:00
new_server_id ,
self . get_remote_ip ( ) )
2021-10-09 08:32:52 +00:00
# These lines create a new Role for the Server with full permissions and add the user to it if he's not a superuser
2021-11-17 18:08:41 +00:00
if len ( captured_roles ) == 0 :
2021-11-17 06:17:36 +00:00
if not exec_user [ ' superuser ' ] :
new_server_uuid = self . controller . servers . get_server_data_by_id ( new_server_id ) . get ( " server_uuid " )
role_id = self . controller . roles . add_role ( " Creator of Server with uuid= {} " . format ( new_server_uuid ) )
self . controller . server_perms . add_role_server ( new_server_id , role_id , " 11111111 " )
self . controller . users . add_role_to_user ( exec_user_id , role_id )
self . controller . crafty_perms . add_server_creation ( exec_user_id )
else :
2021-11-17 18:08:41 +00:00
for role in captured_roles :
role_id = role
self . controller . server_perms . add_role_server ( new_server_id , role_id , " 11111111 " )
2021-08-22 09:06:44 +00:00
2021-03-22 04:02:18 +00:00
self . controller . stats . record_stats ( )
2020-09-14 20:01:38 +00:00
self . redirect ( " /panel/dashboard " )
2020-08-23 22:43:28 +00:00
2020-08-19 17:54:10 +00:00
self . render (
template ,
2021-03-26 13:57:50 +00:00
data = page_data ,
translate = self . translator . translate ,
2021-08-10 02:11:15 +00:00
)