Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions watchdog/pi_zero/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@
"192.168.1.11",
"192.168.1.12",
]
INTERNET_IP = "1.1.1.1"
_INTERNET_HTTP_URLS = [
"https://clients3.google.com/generate_204",
"https://connectivitycheck.gstatic.com/generate_204",
"http://cp.cloudflare.com",
"www.msftconnecttest.com/connecttest.txt",
]
_INTERNET_PING_IPS = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]

PING_COUNT = 2
TIMEOUT = 2 # seconds, used for ping and HTTP timeout
Expand Down Expand Up @@ -135,6 +141,25 @@
return False


def internet_check_ok() -> bool:
for url in _INTERNET_HTTP_URLS:
try:
with urlopen(url, timeout=TIMEOUT) as resp:

Check warning on line 147 in watchdog/pi_zero/watchdog.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

watchdog/pi_zero/watchdog.py#L147

Audit url open for permitted schemes. Allowing use of file:/ or custom schemes is often unexpected.
if resp.status in (200, 204):
logging.info("Internet HTTP check OK: %s", url)
return True
except Exception: # noqa: S110

Check warning on line 151 in watchdog/pi_zero/watchdog.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

watchdog/pi_zero/watchdog.py#L151

Try, Except, Pass detected.
pass

for ip in _INTERNET_PING_IPS:
if ping_host(ip, count=1, timeout=TIMEOUT):
logging.info("Internet ping fallback OK: %s", ip)
return True

logging.warning("Internet connectivity FAILED")
return False


def http_health_ok(url: str) -> bool:
req = Request(url, method="GET", headers={"accept": "application/json"})
try:
Expand Down Expand Up @@ -253,8 +278,8 @@

reboot_12v = False

internet_ok = ping_host(INTERNET_IP, count=1, timeout=TIMEOUT)
internet_fails = update_fail_counter(internet_ok, FAIL_INTERNET_FILE, f"Internet {INTERNET_IP}")
internet_ok = internet_check_ok()
internet_fails = update_fail_counter(internet_ok, FAIL_INTERNET_FILE, "Internet")
if internet_fails >= MAX_FAILS:
reboot_12v = True

Expand Down
Loading