my-nixos-config/home-manager/modules/hypr/mullvad.sh

142 lines
4.9 KiB
Bash
Executable file

#!/run/current-system/sw/bin/bash
# CREDIT TO @dottmp on Github (https://github.com/dottmp/mullvad-waybar-module/blob/main/mullvad.sh)
# LICENSED UNDER MIT:
# MIT License
# Copyright (c) 2026 dottmp
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
# Mullvad CLI
#
# Manage the Mullvad VPN daemon via a convenient CLI
# Usage: mullvad <COMMAND>
#
# Commands:
# account Control and display information about your Mullvad account
# auto-connect Control the daemon auto-connect setting
# beta-program Receive notifications about beta updates
# lockdown-mode Control whether to block network access when disconnected from VPN
# dns Configure DNS servers to use when connected
# lan Control the allow local network sharing setting
# connect Connect to a VPN relay
# disconnect Disconnect from the VPN
# reconnect Reconnect to any matching VPN relay
# relay Manage relay and tunnel constraints
# api-access Manage Mullvad API access methods
# obfuscation Manage use of obfuscation protocols for WireGuard. Can make WireGuard traffic look like something else on the network. Helps circumvent censorship and to establish a tunnel when on restricted networks
# split-tunnel Manage split tunneling. To launch applications outside the tunnel, use the program 'mullvad-exclude' instead of this command
# status Return the state of the VPN tunnel
# tunnel Manage tunnel options
# version Show information about the current Mullvad version and available versions
# factory-reset Reset settings, caches, and logs
# reset-settings Reset settings only, but remain logged in and keep logs and caches
# custom-list Manage custom lists
# import-settings Apply a JSON patch generated by 'export-settings'
# export-settings Export a JSON patch based on the current settings
# help Print this message or the help of the given subcommand(s)
# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
# Mullvad Methods
vpn_get_status() {
mullvad status
}
vpn_reconnect() {
mullvad reconnect
}
vpn_disconnect() {
mullvad disconnect
}
vpn_connect() {
mullvad connect
}
# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
# Vars
VPN_STATUS="$(vpn_get_status)"
VPN_CONNECTION=$(echo "$VPN_STATUS" | head -n1 | xargs)
VPN_RELAY=$(echo "$VPN_STATUS" | awk -F'Relay: ' '/Relay:/ {print $2}' | xargs)
VPN_FEATURES=$(echo "$VPN_STATUS" | awk -F'Features: ' '/Features:/ {print $2}' | xargs)
VPN_VISIBLE_LOCATION=$(echo "$VPN_STATUS" | awk -F'Visible location: ' '/Visible location:/ {print $2}' | xargs)
# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
# Actions
#
# [toggle_connection] Connect/disconnect to a VPN relay based on current status and send notification
toggle_connection() {
if [ "$VPN_CONNECTION" = "Connected" ]; then
vpn_disconnect
else
vpn_connect
fi
notify-send
sleep 1
}
# [reconnect] Reconnect to any matching VPN relay and send notification
reconnect() {
vpn_reconnect
notify-send
sleep 1
}
# [get_module_data] Get data for custom/mullvad module in waybar. Returns waybar JSON
get_module_data() {
local class alt tooltip
tooltip="Mullvad VPN\n\nStatus: $VPN_CONNECTION\nRelay: $VPN_RELAY\nFeatures: $VPN_FEATURES\nVisible Location: $VPN_VISIBLE_LOCATION"
case "$VPN_CONNECTION" in
Connected)
class="connected"
alt="connected"
;;
Connecting)
class="connecting"
alt="connecting"
;;
*)
class="disconnected"
alt="disconnected"
;;
esac
echo "{\"text\": \"$VPN_CONNECTION\", \"tooltip\": \"$tooltip\", \"alt\": \"$alt\", \"class\": \"$class\"}"
}
# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
# Input
case "$1" in
toggle) toggle_connection ;;
reconnect) reconnect ;;
*) get_module_data ;;
esac