2024-06-23 22:47:43 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-08-06 21:59:05 +02:00
|
|
|
adguard_test_domain='check.adguard.test'
|
|
|
|
|
|
|
|
if [[ "$(uname)" == "Darwin" ]]; then
|
|
|
|
# macOS
|
|
|
|
local_ip=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -n1)
|
2024-08-07 00:07:59 +02:00
|
|
|
uptime_seconds=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//')
|
|
|
|
current_time=$(date +%s)
|
|
|
|
uptime_seconds=$((current_time - uptime_seconds))
|
|
|
|
days=$((uptime_seconds / 86400))
|
|
|
|
hours=$(( (uptime_seconds % 86400) / 3600 ))
|
|
|
|
minutes=$(( (uptime_seconds % 3600) / 60 ))
|
|
|
|
uptime="up "
|
|
|
|
[[ $days -gt 0 ]] && uptime+="$days days, "
|
|
|
|
[[ $hours -gt 0 ]] && uptime+="$hours hours, "
|
|
|
|
uptime+="$minutes minutes"
|
2024-08-06 21:59:05 +02:00
|
|
|
else
|
|
|
|
# Linux
|
|
|
|
local_ip=$(hostname -I | awk '{print $1}')
|
|
|
|
uptime=$(uptime -p)
|
|
|
|
fi
|
2024-06-23 22:47:43 +02:00
|
|
|
|
|
|
|
wan_info=$(curl -s --max-time 10 https://am.i.mullvad.net/json)
|
|
|
|
wan_connected=false
|
|
|
|
if [ ! -z "$wan_info" ]; then
|
|
|
|
wan_connected=true
|
|
|
|
wan_ip=$(echo "$wan_info" | jq -r '.ip')
|
|
|
|
mullvad_exit_ip=$(echo "$wan_info" | jq '.mullvad_exit_ip')
|
|
|
|
blacklisted=$(echo "$wan_info" | jq '.blacklisted.blacklisted')
|
|
|
|
else
|
|
|
|
wan_ip="Unavailable"
|
2024-08-07 00:07:59 +02:00
|
|
|
mullvad_exit_ip=false
|
|
|
|
blacklisted=false
|
2024-06-23 22:47:43 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if Tailscale is installed and get IP
|
2024-08-06 21:59:05 +02:00
|
|
|
if command -v tailscale &> /dev/null; then
|
2024-06-23 22:47:43 +02:00
|
|
|
has_tailscale=true
|
2024-08-06 21:59:05 +02:00
|
|
|
tailscale_ip=$(tailscale ip -4)
|
2024-06-23 22:47:43 +02:00
|
|
|
# Get Tailscale exit-node information
|
2024-08-06 21:59:05 +02:00
|
|
|
ts_exitnode_output=$(tailscale exit-node list)
|
2024-06-23 22:47:43 +02:00
|
|
|
# Parse exit node hostname
|
|
|
|
if echo "$ts_exitnode_output" | grep -q 'selected'; then
|
|
|
|
mullvad_exitnode=true
|
2024-08-07 03:18:59 +02:00
|
|
|
# Extract the hostname of the selected exit node, taking only the part before any newline
|
|
|
|
mullvad_hostname=$(echo "$ts_exitnode_output" | grep 'selected' | awk '{print $2}' | awk -F'\n' '{print $1}')
|
2024-06-23 22:47:43 +02:00
|
|
|
else
|
|
|
|
mullvad_exitnode=false
|
|
|
|
mullvad_hostname=""
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
has_tailscale=false
|
|
|
|
tailscale_ip="Not installed"
|
|
|
|
mullvad_exitnode=false
|
|
|
|
mullvad_hostname=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
nextdns_info=$(curl -sL --max-time 10 https://test.nextdns.io)
|
|
|
|
if [ -z "$nextdns_info" ]; then
|
2024-08-06 21:59:05 +02:00
|
|
|
echo "Failed to fetch NextDNS status or no internet connection." >&2
|
|
|
|
nextdns_connected=false
|
|
|
|
nextdns_protocol=""
|
|
|
|
nextdns_client=""
|
2024-06-23 22:47:43 +02:00
|
|
|
else
|
|
|
|
nextdns_status=$(echo "$nextdns_info" | jq -r '.status')
|
2024-08-06 21:59:05 +02:00
|
|
|
if [ "$nextdns_status" = "ok" ]; then
|
2024-06-23 22:47:43 +02:00
|
|
|
nextdns_connected=true
|
|
|
|
nextdns_protocol=$(echo "$nextdns_info" | jq -r '.protocol')
|
|
|
|
nextdns_client=$(echo "$nextdns_info" | jq -r '.clientName')
|
2024-08-06 21:59:05 +02:00
|
|
|
else
|
|
|
|
nextdns_connected=false
|
|
|
|
nextdns_protocol=""
|
|
|
|
nextdns_client=""
|
2024-06-23 22:47:43 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-08-06 21:59:05 +02:00
|
|
|
# Check AdGuard Home DNS
|
|
|
|
resolved_ip=$(dig +short $adguard_test_domain)
|
2024-09-23 22:17:54 +02:00
|
|
|
if [[ $resolved_ip =~ ^100\. ]]; then
|
2024-08-06 21:59:05 +02:00
|
|
|
adguard_connected=true
|
|
|
|
adguard_protocol="AdGuard Home"
|
2024-09-23 22:17:54 +02:00
|
|
|
adguard_client="$resolved_ip"
|
2024-08-06 21:59:05 +02:00
|
|
|
else
|
|
|
|
adguard_connected=false
|
|
|
|
adguard_protocol=""
|
|
|
|
adguard_client=""
|
|
|
|
fi
|
2024-06-23 22:47:43 +02:00
|
|
|
|
2024-08-07 00:07:59 +02:00
|
|
|
# Output JSON using jq for proper formatting and escaping
|
|
|
|
jq -n \
|
|
|
|
--arg local_ip "$local_ip" \
|
|
|
|
--argjson wan_connected "$wan_connected" \
|
|
|
|
--arg wan_ip "$wan_ip" \
|
|
|
|
--argjson has_tailscale "$has_tailscale" \
|
|
|
|
--arg tailscale_ip "$tailscale_ip" \
|
|
|
|
--argjson mullvad_exitnode "$mullvad_exitnode" \
|
|
|
|
--arg mullvad_hostname "$mullvad_hostname" \
|
|
|
|
--argjson mullvad_exit_ip "$mullvad_exit_ip" \
|
|
|
|
--argjson blacklisted "$blacklisted" \
|
|
|
|
--argjson nextdns_connected "$nextdns_connected" \
|
|
|
|
--arg nextdns_protocol "$nextdns_protocol" \
|
|
|
|
--arg nextdns_client "$nextdns_client" \
|
|
|
|
--argjson adguard_connected "$adguard_connected" \
|
|
|
|
--arg adguard_protocol "$adguard_protocol" \
|
|
|
|
--arg adguard_client "$adguard_client" \
|
|
|
|
--arg uptime "$uptime" \
|
|
|
|
'{
|
|
|
|
local_ip: $local_ip,
|
|
|
|
wan_connected: $wan_connected,
|
|
|
|
wan_ip: $wan_ip,
|
|
|
|
has_tailscale: $has_tailscale,
|
|
|
|
tailscale_ip: $tailscale_ip,
|
|
|
|
mullvad_exitnode: $mullvad_exitnode,
|
|
|
|
mullvad_hostname: $mullvad_hostname,
|
|
|
|
mullvad_exit_ip: $mullvad_exit_ip,
|
|
|
|
blacklisted: $blacklisted,
|
|
|
|
nextdns_connected: $nextdns_connected,
|
|
|
|
nextdns_protocol: $nextdns_protocol,
|
|
|
|
nextdns_client: $nextdns_client,
|
|
|
|
adguard_connected: $adguard_connected,
|
|
|
|
adguard_protocol: $adguard_protocol,
|
|
|
|
adguard_client: $adguard_client,
|
|
|
|
uptime: $uptime
|
|
|
|
}'
|