2024-05-06 14:28:46 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
# Mullvad_VPN_Check
|
2024-09-07 22:47:28 +00:00
|
|
|
# A script that checks for a Mullvad VPN connection, logs the status,
|
|
|
|
|
# and sends alerts if the VPN status changes.
|
2024-05-06 14:28:46 +00:00
|
|
|
|
|
|
|
|
# Set Discord webhook
|
|
|
|
|
WEBHOOK_URL="DISCORD_WEBHOOK_URL"
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# Temp file to store the VPN connection status
|
|
|
|
|
STATUS_FILE="/tmp/vpn_status.log"
|
|
|
|
|
|
|
|
|
|
# Function to send VPN down alert
|
|
|
|
|
vpn_down_alert() {
|
2024-05-06 14:28:46 +00:00
|
|
|
curl -H "Content-Type: application/json" -X POST --data @- "$WEBHOOK_URL" <<EOF
|
|
|
|
|
{
|
|
|
|
|
"content": null,
|
|
|
|
|
"embeds": [
|
|
|
|
|
{
|
|
|
|
|
"title": "VPN Connectivity - WARNING",
|
|
|
|
|
"description": "\"$(hostname)\" is not going through a Mullvad VPN connection",
|
2024-05-06 14:49:59 +00:00
|
|
|
"color": 13238272,
|
2024-05-06 14:28:46 +00:00
|
|
|
"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
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# Function to send VPN restored alert
|
|
|
|
|
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() {
|
2024-05-06 14:28:46 +00:00
|
|
|
mullvad_exit_ip="$1"
|
2024-09-07 22:47:28 +00:00
|
|
|
|
|
|
|
|
# Check the current status (true for VPN, false for no VPN)
|
2024-05-06 14:28:46 +00:00
|
|
|
if [ "$mullvad_exit_ip" = "false" ]; then
|
2024-09-07 22:47:28 +00:00
|
|
|
echo "VPN not connected, sending alert..."
|
|
|
|
|
|
|
|
|
|
# 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"
|
2024-05-06 14:28:46 +00:00
|
|
|
else
|
2024-09-07 22:47:28 +00:00
|
|
|
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"
|
2024-05-06 14:28:46 +00:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# Fetch JSON data from Mullvad API
|
2024-05-06 14:28:46 +00:00
|
|
|
json=$(curl -s https://ipv4.am.i.mullvad.net/json)
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# Get Reported IP and ISP
|
2024-05-06 14:28:46 +00:00
|
|
|
IP=$(echo "$json" | grep -oE '"ip":\s*"[^"]+"' | grep -oE '[0-9.]+')
|
|
|
|
|
ORG=$(echo "$json" | grep -oP '(?<="organization":")[^"]+')
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# Parse JSON and extract "mullvad_exit_ip" (true if VPN, false if not)
|
2024-05-06 14:28:46 +00:00
|
|
|
mullvad_exit_ip=$(jq -r '.mullvad_exit_ip' <<< "$json")
|
|
|
|
|
|
2024-09-07 22:47:28 +00:00
|
|
|
# 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"
|
2024-05-06 14:28:46 +00:00
|
|
|
|