diff --git a/epicyon.py b/epicyon.py index dfdf5a0d9..d856bd300 100644 --- a/epicyon.py +++ b/epicyon.py @@ -129,6 +129,7 @@ from poison import load_dictionary from poison import load_2grams from webapp_post import get_instance_software from siteactive import site_is_active +from siteactive import is_online def str2bool(value_str) -> bool: @@ -457,6 +458,11 @@ def _command_options() -> None: dest='shared_items_federated_domains', help='Specify federation list for shared items, ' + 'separated by spaces') + parser.add_argument("--internet", "--online", + dest='online', + type=str2bool, nargs='?', + const=True, default=False, + help="Checks if internet is available") parser.add_argument("--poisoned", "--poison", dest='poisoned', type=str2bool, nargs='?', @@ -869,6 +875,14 @@ def _command_options() -> None: argb = parser.parse_args() + if argb.online: + # is the internet available? + if is_online(): + print("True") + else: + print("False") + sys.exit() + if argb.poisoned: # LLM poisoning example base_dir = os.getcwd() diff --git a/siteactive.py b/siteactive.py index 79b0fdbba..fe21daaea 100644 --- a/siteactive.py +++ b/siteactive.py @@ -10,6 +10,7 @@ __module_group__ = "Core" import http.client import ssl +import socket from urllib.parse import urlparse from utils import data_dir @@ -183,3 +184,17 @@ def load_unavailable_sites(base_dir: str) -> []: print('EX: unable to read unavailable sites ' + unavailable_sites_filename) return sites_unavailable + + +def is_online(host: str = "8.8.8.8", + port: int = 53, timeout: int = 3) -> bool: + """ + Returns True if the internet is available + """ + try: + socket.setdefaulttimeout(timeout) + socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) + return True + except socket.error as ex: + print(ex) + return False