From 8975e81c7e5ee56a41ee64780381fee63cb5eea8 Mon Sep 17 00:00:00 2001 From: NCLtech Date: Sat, 7 Sep 2024 23:47:28 +0100 Subject: [PATCH] Added additional function to check and report when the VPN connectivity is restored --- run_check.sh | 91 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 16 deletions(-) diff --git a/run_check.sh b/run_check.sh index 831af2f..a053ea3 100755 --- a/run_check.sh +++ b/run_check.sh @@ -1,14 +1,17 @@ #!/bin/bash # Mullvad_VPN_Check -# A script that checks for a Mullvad VPN connection and if there isn't one, will send and alert - +# A script that checks for a Mullvad VPN connection, logs the status, +# and sends alerts if the VPN status changes. # Set Discord webhook WEBHOOK_URL="DISCORD_WEBHOOK_URL" -# Function to run alert -run_alert() { +# Temp file to store the VPN connection status +STATUS_FILE="/tmp/vpn_status.log" + +# Function to send VPN down alert +vpn_down_alert() { curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" < "$STATUS_FILE" else - echo "VPN connection through Mullvad detected." - exit 1 # Exit with error code to indicate condition not met + echo "VPN connected." + + # Send a "VPN restored" alert if VPN was not connected last time + if [ "$previous_status" = "false" ]; then + vpn_up_alert + echo "VPN restored alert sent!" + fi + + # Log the current status + echo "true" > "$STATUS_FILE" fi } -# Fetch JSON data +# Fetch JSON data from Mullvad API json=$(curl -s https://ipv4.am.i.mullvad.net/json) -# Get Reported IP +# Get Reported IP and ISP IP=$(echo "$json" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+') -# Get Reported ISP ORG=$(echo "$json" | grep -oP '(?<="organization":")[^"]+') -# Parse JSON and extract "mullvad_exit_ip" +# Parse JSON and extract "mullvad_exit_ip" (true if VPN, false if not) mullvad_exit_ip=$(jq -r '.mullvad_exit_ip' <<< "$json") -# Check if "mullvad_exit_ip" is false -check_mullvad_exit_ip "$mullvad_exit_ip" +# Check if the status log file exists, if not create it with a default value (false) +if [ ! -f "$STATUS_FILE" ]; then + echo "false" > "$STATUS_FILE" +fi + +# Read the previous VPN status from the log file +previous_status=$(cat "$STATUS_FILE") + +# Check the current VPN status and take action +check_vpn_status "$mullvad_exit_ip"