#!/bin/sh
#
# Show network status, at various layers.
BOLD=$(tput bold)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
success() {
echo "$GREEN$BOLD✓ ${RESET}$*"
}
failure() {
echo "$RED$BOLD✗ ${RESET}$*"
}
CONNECTED=$(iwctl station list | grep connected)
if [ -z "$CONNECTED" ]; then
failure "Not connected to any network."
exit 1
else
success "Host $BOLD$(hostname)$RESET is connected."
fi
# FIXME: Only shows first work of networks with spaces:
WIFI=$(iwctl station wlan0 show | grep "Connected network" | awk '{ print $3 }')
if [ -z "$WIFI" ]; then
failure "Not connected to any network."
exit 1
else
success "Connected to wifi ${BOLD}$WIFI${RESET}."
fi
# # XXX: libcurl has the dns-servers feature disabled.
# WIFI_DNS=$(cat /run/dhcpcd/nameservers | grep -Pv "^# ")
# if ! CAP=$(curl --dns-servers "$WIFI_DNS" --silent captive.apple.com); then
# failure "HTTP traffic is failing (and no reachable captive portal found)."
# exit 3
# fi
#
# if [ "$CAP" = "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>" ]; then
# success "No captive portal detected."
# else
# failure "Captive portal or weird firewall detected."
# exit 4
# fi
# TODO: check for a routable IP
# TODO: check for a gateway
DNS=false
if drill whynothugo.nl > /dev/null; then
success "DNS (whynothugo.nl) works."
DNS=true
else
failure "DNS (whynothugo.nl) resolution failing."
fi
if drill example.com > /dev/null; then
success "DNS (example.com) works."
DNS=true
else
failure "DNS (example.com) resolution failing."
fi
if [ "$DNS" = "false" ]; then
exit 2
fi
# TODO: I should run this command, and swallot its output. Only show it on error.
# XXX: Actually, all commands in this file should be in parallel, and we should
# swallow its output. Whenever one returns non-zero, we render its output and
# exit code.
# orange(warn) background and black text for header (step, command) and regular
# colours for stdout and red for stderr
#curl --connect-timeout 30 http://httpforever.com/
if ! CAP=$(curl --silent captive.apple.com); then
failure "HTTP traffic is failing (or there's an odd captive portal around."
exit 3
fi
if [ "$CAP" = "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>" ]; then
success "No captive portal detected."
else
failure "Captive portal or weird firewall detected."
exit 4
fi
success "All good, network seems functional! 🎉"
# TODO: try DDG for really weird network filters.
# TODO: try TPB to check censoring.