Added additional function to check and report when the VPN connectivity is restored

This commit is contained in:
NCLtech 2024-09-07 23:47:28 +01:00
parent 7185f3b395
commit 8975e81c7e

View File

@ -1,14 +1,17 @@
#!/bin/bash #!/bin/bash
# Mullvad_VPN_Check # 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 # Set Discord webhook
WEBHOOK_URL="DISCORD_WEBHOOK_URL" WEBHOOK_URL="DISCORD_WEBHOOK_URL"
# Function to run alert # Temp file to store the VPN connection status
run_alert() { 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" <<EOF curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{ {
"content": null, "content": null,
@ -38,29 +41,85 @@ curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
EOF EOF
} }
# Function to check if "mullvad_exit_ip" is false # Function to send VPN restored alert
check_mullvad_exit_ip() { vpn_up_alert() {
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
{
"content": null,
"embeds": [
{
"title": "VPN Connectivity - RESTORED",
"description": "\"$(hostname)\" is now going through a Mullvad VPN connection",
"color": 3066993,
"fields": [
{
"name": "Date and Time of Test:",
"value": "$(date +'%Y-%m-%d %H:%M:%S')"
},
{
"name": "Current ISP",
"value": "$ORG"
},
{
"name": "Current IP",
"value": "$IP"
}
]
}
],
"attachments": []
}
EOF
}
# Function to check the VPN status and act accordingly
check_vpn_status() {
mullvad_exit_ip="$1" mullvad_exit_ip="$1"
# Check the current status (true for VPN, false for no VPN)
if [ "$mullvad_exit_ip" = "false" ]; then if [ "$mullvad_exit_ip" = "false" ]; then
run_alert echo "VPN not connected, sending alert..."
echo "Alert sent!"
# Send alert if VPN was connected last time
if [ "$previous_status" = "true" ]; then
vpn_down_alert
echo "VPN down alert sent!"
fi
# Log the current status
echo "false" > "$STATUS_FILE"
else else
echo "VPN connection through Mullvad detected." echo "VPN connected."
exit 1 # Exit with error code to indicate condition not met
# 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 fi
} }
# Fetch JSON data # Fetch JSON data from Mullvad API
json=$(curl -s https://ipv4.am.i.mullvad.net/json) 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.]+') IP=$(echo "$json" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+')
# Get Reported ISP
ORG=$(echo "$json" | grep -oP '(?<="organization":")[^"]+') 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") mullvad_exit_ip=$(jq -r '.mullvad_exit_ip' <<< "$json")
# Check if "mullvad_exit_ip" is false # Check if the status log file exists, if not create it with a default value (false)
check_mullvad_exit_ip "$mullvad_exit_ip" 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"