Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle default network config values if core configuration is incomplete #99

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 19 additions & 19 deletions ovos_utils/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
from ovos_utils.log import LOG


_DEFAULT_TEST_CONFIG = {
"ip_url": 'https://api.ipify.org',
"dns_primary": "1.1.1.1",
"dns_secondary": "8.8.8.8",
"web_url": "http://nmcheck.gnome.org/check_network_status.txt",
"web_url_secondary": "https://checkonline.home-assistant.io/online.txt",
"captive_portal_url": "http://nmcheck.gnome.org/check_network_status.txt",
"captive_portal_text": "NetworkManager is online"
}


def get_network_tests_config():
"""Get network_tests object from mycroft.configuration."""
from ovos_config import Configuration
config = Configuration()
return config.get(
"network_tests",
{
"ip_url": 'https://api.ipify.org',
"dns_primary": "1.1.1.1",
"dns_secondary": "8.8.8.8",
"web_url": "http://nmcheck.gnome.org/check_network_status.txt",
"web_url_secondary": "https://checkonline.home-assistant.io/online.txt",
"captive_portal_url": "http://nmcheck.gnome.org/check_network_status.txt",
"captive_portal_text": "NetworkManager is online"
}
)
return config.get("network_tests", _DEFAULT_TEST_CONFIG)


def get_ip():
Expand All @@ -41,7 +41,7 @@ def get_ip():

def get_external_ip():
cfg = get_network_tests_config()
return requests.get(cfg["ip_url"]).text
return requests.get(cfg.get("ip_url") or _DEFAULT_TEST_CONFIG['ip_url']).text


def is_connected_dns(host=None, port=53, timeout=3):
Expand All @@ -52,8 +52,8 @@ def is_connected_dns(host=None, port=53, timeout=3):

if host is None:
cfg = get_network_tests_config()
return is_connected_dns(cfg["dns_primary"]) or \
is_connected_dns(cfg["dns_secondary"])
return is_connected_dns(cfg.get("dns_primary" or _DEFAULT_TEST_CONFIG['dns_primary'])) or \
is_connected_dns(cfg.get("dns_secondary" or _DEFAULT_TEST_CONFIG['dns_secondary']))

try:
# connect to the host -- tells us if the host is actually reachable
Expand All @@ -73,8 +73,8 @@ def is_connected_http(host=None):
"""
if host is None:
cfg = get_network_tests_config()
return is_connected_http(cfg["web_url"]) or \
is_connected_http(cfg["web_url_secondary"])
return is_connected_http(cfg.get("web_url") or _DEFAULT_TEST_CONFIG['web_url']) or \
is_connected_http(cfg.get("web_url_secondary") or _DEFAULT_TEST_CONFIG['web_url_secondary'])

try:
status = requests.head(host).status_code
Expand All @@ -94,8 +94,8 @@ def check_captive_portal(host=None, expected_text=None) -> bool:

if not host or not expected_text:
cfg = get_network_tests_config()
host = host or cfg["captive_portal_url"]
expected_text = expected_text or cfg["captive_portal_url_text"]
host = host or cfg.get("captive_portal_url") or _DEFAULT_TEST_CONFIG["captive_portal_url"]
expected_text = expected_text or cfg.get("captive_portal_text") or _DEFAULT_TEST_CONFIG["captive_portal_text"]

try:
# We need to check a site that doesn't use HTTPS
Expand Down