diff options
-rw-r--r-- | src/backend/checkenv.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/backend/checkenv.py b/src/backend/checkenv.py index 4f540e9..8030bc9 100644 --- a/src/backend/checkenv.py +++ b/src/backend/checkenv.py @@ -1,18 +1,57 @@ #!/usr/bin/python3 import os +import re import subprocess import sisyphus.getenv +import sisyphus.getfs import sisyphus.getnews import urllib.request +from urllib.error import HTTPError + + +NETWORK_CHECK_URL_FILE = os.path.join( + sisyphus.getfs.s_cfg_dir, "network_check_url.conf") + + +def is_valid_url(url): + regex = re.compile( + r'^(http|https)://' + r'([a-zA-Z0-9.-]+)' + r'(\.[a-zA-Z]{2,})' + r'(:\d+)?' + r'(/.*)?$' + ) + return re.match(regex, url) is not None def connectivity(): is_online = int() + default_url = "https://redcorelinux.org" + + try: + with open(NETWORK_CHECK_URL_FILE, "r") as file: + url = file.readline().strip() + + if not url: + url = default_url + + elif not is_valid_url(url): + url = default_url + + except FileNotFoundError: + url = default_url try: - urllib.request.urlopen("http://www.google.com", timeout=5) + urllib.request.urlopen(url, timeout=5) is_online = int(1) + + except HTTPError as e: + if e.code == 429: + is_online = int(1) # ignore rate limiting errors + else: + is_online = int(1) # ignore all other http errors + except urllib.error.URLError: is_online = int(0) |