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.
This commit is contained in:
Andrew 2021-09-13 13:10:34 -04:00
parent 043b7d9712
commit 2528c123f2
4 changed files with 46 additions and 3 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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();
}
})
});
}

View File

@ -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):