aboutsummaryrefslogtreecommitdiff
path: root/config/waybar/scripts/mullvad.sh
blob: e02bb2fa07435e354322b3eac689888cb4bc6633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash

STATUS_KEY="state"
RUNNING="connected"

mullvad_status () {
    status="$(mullvad status --json | jq -r '.'$STATUS_KEY)"
    if [ "$status" = $RUNNING ]; then
        return 0
    fi
    return 1
}

toggle_status () {
    if mullvad_status; then
        mullvad disconnect
    else
        mullvad connect
    fi
}

case $1 in
    --status)
        if mullvad_status; then
            status_json=$(mullvad status --json)
            endpoint=$(echo "$status_json" | jq -r '.details.endpoint | "\(.tunnel_interface) (\(.tunnel_type))"')
            location=$(echo "$status_json" | jq -r '.details.location | "\(.country), \(.city) (\(.ipv4))"')
            echo "{\"text\":\"\",\"class\":\"connected\",\"alt\":\"connected\", \"tooltip\": \"${endpoint}\\n${location}\"}"
        else
            echo "{\"text\":\"\",\"class\":\"disconnected\",\"alt\":\"disconnected\", \"tooltip\": \"VPN is not active.\"}"
        fi
    ;;
    --toggle)
        toggle_status
    ;;
esac