diff --git a/.gitignore b/.gitignore index 738baf8d..9833443b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,5 +18,6 @@ venv.bak/ servers/ session.lock .header +default.json app/config/web diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index c691f285..4ce4f697 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -2,6 +2,7 @@ import os import sys import json import time +import shutil import logging from datetime import datetime @@ -142,18 +143,17 @@ class ServerJars: def download_jar(self, server, version, path): base_url = "https://serverjars.com/api/fetchJar/{server}/{version}".format(server=server, version=version) - r = requests.get(base_url, timeout=2) - if r.status_code in [200, 201]: + # open a file stream + with requests.get(base_url, timeout=2, stream=True) as r: try: - with open(path, 'bw') as output: - output.write(r.content) - return True + with open(path, 'wb') as output: + shutil.copyfileobj(r.raw, output) + except Exception as e: logger.error("Unable to save jar to {path} due to error:{error}".format(path=path, error=e)) pass - logger.error("Got {} code from download, escaping".format(r.status_code)) return False # todo: import server @@ -169,6 +169,8 @@ class ServerJars: # download the jar self.download_jar(server, version, full_jar_path) + time.sleep(3) + # todo: verify the MD5 # put data in the db diff --git a/app/classes/minecraft/stats.py b/app/classes/minecraft/stats.py index 68aa77d9..c6dc59c2 100644 --- a/app/classes/minecraft/stats.py +++ b/app/classes/minecraft/stats.py @@ -232,7 +232,6 @@ class Stats: server_stats = stats_to_send.get('servers') for server in server_stats: - Server_Stats.insert({ Server_Stats.server_id: server.get('id', 0), Server_Stats.started: server.get('started', ""), @@ -252,7 +251,7 @@ class Stats: }).execute() # delete 1 week old data - max_age = int(helper.get_setting("CRAFTY", "history_max_age")) + max_age = helper.get_setting("history_max_age") now = datetime.datetime.now() last_week = now.day - max_age diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index afa91c06..3d9d0858 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -34,7 +34,7 @@ class Helpers: self.servers_dir = os.path.join(self.root_dir, 'servers') self.session_file = os.path.join(self.root_dir, 'session.lock') - self.settings_file = os.path.join(self.root_dir, 'config.ini') + self.settings_file = os.path.join(self.root_dir, 'config.json') self.db_path = os.path.join(self.root_dir, 'crafty.sqlite') self.serverjar_cache = os.path.join(self.config_dir, 'serverjars.json') @@ -52,14 +52,19 @@ class Helpers: logger.error("{} does not exits".format(file)) return False - def get_setting(self, section, key): + def get_setting(self, key): try: - our_config = configparser.ConfigParser() - our_config.read(self.settings_file) + with open(self.settings_file, "r") as f: + data = json.load(f) - if our_config.has_option(section, key): - return our_config[section][key] + if key in data.keys(): + return data.get(key) + + else: + logger.error("Config File Error: setting {} does not exist".format(key)) + console.error("Config File Error: setting {} does not exist".format(key)) + return False except Exception as e: logger.critical("Config File Error: Unable to read {} due to {}".format(self.settings_file, e)) @@ -366,7 +371,10 @@ class Helpers: with open(default_file, 'r') as f: data = json.load(f) - os.remove(default_file) + del_json = helper.get_setting('delete_default_json') + + if del_json: + os.remove(default_file) return data diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index 01172a9e..542c5a64 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -196,8 +196,14 @@ class db_shortcuts: return self.return_rows(query) def get_all_servers_stats(self): - query = Server_Stats.select().order_by(Server_Stats.stats_id.desc()).limit(1) - return self.return_rows(query) + servers = self.get_all_defined_servers() + server_data = [] + + for s in servers: + latest = Server_Stats.select().where(Server_Stats.server_id == s.get('server_id')).order_by(Server_Stats.created.desc()).limit(1) + server_data.append({'server_data': s, "stats": self.return_rows(latest)}) + # print(server_data) + return server_data def get_latest_hosts_stats(self): query = Host_Stats.select().order_by(Host_Stats.id.desc()).get() diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 5a9838da..e85bee38 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -86,7 +86,7 @@ class TasksManager: @staticmethod def start_stats_recording(): - stats_update_frequency = int(helper.get_setting("CRAFTY", 'stats_update_frequency')) + stats_update_frequency = helper.get_setting('stats_update_frequency') logger.info("Stats collection frequency set to {stats} seconds".format(stats=stats_update_frequency)) console.info("Stats collection frequency set to {stats} seconds".format(stats=stats_update_frequency)) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 7d32f498..feb90a40 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -48,10 +48,10 @@ class PanelHandler(BaseHandler): elif page == 'dashboard': page_data['servers'] = db_helper.get_all_servers_stats() + for s in page_data['servers']: try: data = json.loads(s['int_ping_results']) - print(data) s['int_ping_results'] = data except: pass diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index 7681deb2..a9b9c8b0 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -24,7 +24,7 @@ class PublicHandler(BaseHandler): def set_current_user(self, user): - expire_days = helper.get_setting("WEB", 'cookie_expire') + expire_days = helper.get_setting('cookie_expire') # if helper comes back with false if not expire_days: @@ -111,6 +111,6 @@ class PublicHandler(BaseHandler): next_page = "/panel/dashboard" self.redirect(next_page) - else: + self.redirect("/public/login") diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index c2898f84..53b5bf5a 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -7,6 +7,7 @@ from app.classes.web.base_handler import BaseHandler from app.classes.minecraft.controller import controller from app.classes.shared.models import db_helper, Servers from app.classes.minecraft.serverjars import server_jar_obj +from app.classes.minecraft.stats import stats logger = logging.getLogger(__name__) @@ -94,7 +95,9 @@ class ServerHandler(BaseHandler): server_parts = server.split("|") success = server_jar_obj.build_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) + if success: + stats.record_stats() self.redirect("/panel/dashboard") self.render( diff --git a/app/classes/web/tornado.py b/app/classes/web/tornado.py index ca29a8c1..f79ca01b 100644 --- a/app/classes/web/tornado.py +++ b/app/classes/web/tornado.py @@ -80,27 +80,24 @@ class webserver: # let's verify we have an SSL cert helper.create_self_signed_cert() - http_port = helper.get_setting("WEB", 'http_port') - https_port = helper.get_setting("WEB", 'https_port') + http_port = helper.get_setting('http_port') + https_port = helper.get_setting('https_port') - lang = helper.get_setting("WEB", 'language') - debug_errors = helper.get_setting("WEB", 'show_errors') - cookie_secret = helper.get_setting("WEB", 'cookie_secret') + lang = helper.get_setting('language') + debug_errors = helper.get_setting('show_errors') + cookie_secret = helper.get_setting('cookie_secret') if cookie_secret is False: cookie_secret = helper.random_string_generator(32) - if cookie_secret.lower == "random": - cookie_secret = helper.random_string_generator(32) - if not lang: lang = "en_EN" if not http_port: - port = 8000 + http_port = 8000 if not https_port: - port = 8443 + https_port = 8443 cert_objects = { 'certfile': os.path.join(helper.config_dir, 'web', 'certs', 'commander.cert.pem'), diff --git a/app/config/serverjars.json b/app/config/serverjars.json index 739f439c..7a9b996b 100644 --- a/app/config/serverjars.json +++ b/app/config/serverjars.json @@ -1,5 +1,5 @@ { - "last_refreshed": "08/26/2020, 08:13:15", + "last_refreshed": "08/27/2020, 08:26:57", "servers": { "nukkitx": [ "1.14" diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index 0cfac13f..298b6a91 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -107,49 +107,49 @@ - - {{ server['server_id']['server_name'] }} + + {{ server['server_data']['server_name'] }} - {% if server['running']%} -   -   + {% if server['stats'][0]['running'] %} +   +   {% else %} -   +   {% end %} -
-
+
+
- {{server['cpu']}}% + {{server['stats'][0]['cpu']}}% -
-
+
+
- {{server['mem_percent']}}% - {{server['mem']}} + {{server['stats'][0]['mem_percent']}}% - {{server['stats'][0]['mem']}} - {{ server['world_name'] }} : {{ server['world_size'] }} + {{ server['stats'][0]['world_name'] }} : {{ server['stats'][0]['world_size'] }} - {% if server['int_ping_results'] == True %} - {{ server['online'] }} / {{ server['max'] }} Max
- {{ server['desc'] }}
- {{ server['version'] }} + {% if server['stats'][0]['int_ping_results'] %} + {{ server['stats'][0]['online'] }} / {{ server['stats'][0]['max'] }} Max
+ {{ server['stats'][0]['desc'] }}
+ {{ server['stats'][0]['version'] }} {% else %} {{ _("Unable to connect to server") }} {% end %} - {% if server['running'] %} + {% if server['stats'][0]['running'] %} Online {% else %} Offline diff --git a/app/frontend/templates/server/wizard.html b/app/frontend/templates/server/wizard.html index db647de5..63023312 100644 --- a/app/frontend/templates/server/wizard.html +++ b/app/frontend/templates/server/wizard.html @@ -67,7 +67,7 @@
- + @@ -90,6 +90,13 @@ }); }); + function wait_msg(){ + bootbox.alert({ + title: 'Downloading Server...', + message: ' Please be patient as we download the server' + }); + } + function check_sizes(){ max_mem = parseFloat($('#max_memory').val()); min_mem = parseFloat($('#min_memory').val()); diff --git a/config.ini b/config.ini deleted file mode 100644 index 2f1756f6..00000000 --- a/config.ini +++ /dev/null @@ -1,14 +0,0 @@ -[WEB] -https = true -force_https = false -http_port = 8000 -https_port = 8443 -language = en_EN -cookie_expire = 30 -cookie_secret = random -show_errors = true - -[CRAFTY] -# max history in days -history_max_age = 7 -stats_update_frequency = 10 \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 00000000..abd826fb --- /dev/null +++ b/config.json @@ -0,0 +1,12 @@ +{ + "https": true, + "http_port": 8000, + "https_port": 8443, + "language": "en_EN", + "cookie_expire": 30, + "cookie_secret": "random", + "show_errors": true, + "history_max_age": 7, + "stats_update_frequency": 5, + "delete_default_json": false +} \ No newline at end of file diff --git a/default.json.example b/default.json.example index 146442c4..ff8b11d6 100644 --- a/default.json.example +++ b/default.json.example @@ -1,4 +1,4 @@ { - "username": "admin" + "username": "admin", "password": "crafty" } \ No newline at end of file