From 2528c123f2f65649223d24cf3ea20bb4f3eeb026 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 13 Sep 2021 13:10:34 -0400 Subject: [PATCH] Adds port/internet connection checks. Checks when crafty starts for internet and a port forward for Crafty's https port. Checks on server launch for internet connection and for port forwarding for executed server. --- app/classes/shared/helpers.py | 21 +++++++++++++++++++++ app/classes/shared/server.py | 16 ++++++++++++++-- app/frontend/templates/base.html | 7 ++++++- main.py | 5 +++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 7a37f799..0937f764 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -14,6 +14,7 @@ import html import zipfile import pathlib import shutil +from requests import get from datetime import datetime from socket import gethostname @@ -76,6 +77,26 @@ class Helpers: logger.error("{} does not exist".format(file)) return True + @staticmethod + def check_internet(): + try: + requests.get('https://google.com', timeout=1) + return True + except Exception as err: + return False + + @staticmethod + def check_port(server_port): + host_public = get('https://api.ipify.org').text + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + result = sock.connect_ex((host_public ,server_port)) + sock.close() + if result == 0: + return True + else: + return False + + def check_for_old_logs(self, db_helper): servers = db_helper.get_all_defined_servers() for server in servers: diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index a504c15c..49f463f9 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -191,8 +191,20 @@ class Server: 'error': msg }) return False - websocket_helper.broadcast('send_start_reload', { - }) + if helper.check_internet(): + loc_server_port = db_helper.get_server_stats_by_id(self.server_id)['server_port'] + if helper.check_port(loc_server_port): + websocket_helper.broadcast('send_start_reload', { + }) + else: + websocket_helper.broadcast('send_start_error', { + 'error': "We have detected port {} may not be open on the host network or a firewall is blocking it. Remote client connections to the server may be limited.".format(loc_server_port) + }) + else: + websocket_helper.broadcast('send_start_error', { + 'error': "We have detected the machine running Crafty has no connection to the internet. Client connections to the server may be limited." + }) + self.process = pexpect.spawn(self.server_command, cwd=self.server_path, timeout=None, encoding='utf-8') out_buf = ServerOutBuf(self.process, self.server_id) diff --git a/app/frontend/templates/base.html b/app/frontend/templates/base.html index 2e807ca7..9714b1e2 100644 --- a/app/frontend/templates/base.html +++ b/app/frontend/templates/base.html @@ -228,7 +228,12 @@ if (webSocket) { var x = document.querySelector('.modal-backdrop'); if(x){ x.remove()} - bootbox.alert(start_error.error); + bootbox.alert({ + message: start_error.error, + callback: function () { + location.reload(); + } +}) }); } diff --git a/main.py b/main.py index 9d9e258e..209fb2f7 100644 --- a/main.py +++ b/main.py @@ -131,6 +131,11 @@ if __name__ == '__main__': # this should always be last tasks_manager.start_main_kill_switch_watcher() + if not helper.check_internet(): + console.error("We have detected the machine running Crafty has no connection to the internet. Client connections to the server may be limited.") + elif not helper.check_port(helper.get_setting('https_port')): + console.error("We have detected Crafty's port, {} may not be open on the host network or a firewall is blocking it. Remote client connections to Crafty may be limited.".format(helper.get_setting('https_port'))) + Crafty = MainPrompt(tasks_manager, migration_manager) def sigterm_handler(signum, current_stack_frame):