-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmullvad.sh
97 lines (74 loc) · 2.8 KB
/
mullvad.sh
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
ICON_CONNECTED=""
ICON_DISCONNECTED=""
COLOR_CONNECTED="#a5fb8f"
COLOR_CONNECTING="#707880"
COLOR_DISCONNECTED="#fc3542"
REMOTE_IP="ip.subdev.org"
NOTIFY_ICON="/usr/share/icons/hicolor/32x32/apps/mullvad-vpn.png"
declare -A countries=(
["Albania"]=AL ["Australia"]=AU ["Austria"]=AT ["Belgium"]=BE ["Brazil"]=BR
["Bulgaria"]=BG ["Canada"]=CA ["Czech Republic"]=CZ ["Denmark"]=DK
["Estonia"]=EE ["Finland"]=FI ["France"]=FR ["Germany"]=DE
["Greece"]=GR ["Hong Kong"]=HK ["Hungary"]=HU ["Ireland"]=IE ["Israel"]=IL
["Italy"]=IT ["Japan"]=JP ["Latvia"]=LV ["Luxembourg"]=LU ["Moldova"]=MD
["Netherlands"]=NL ["New Zealand"]=NZ ["North Macedonia"]=MK ["Norway"]=NO
["Poland"]=PL ["Portugal"]=PT ["Romania"]=RO ["Serbia"]=RS ["Singapore"]=SG
["Slovakia"]=SK ["Spain"]=ES ["Sweden"]=SE ["Switzerland"]=CH
["UK"]=GB ["United Arab Emirates"]=AE ["USA"]=US
)
###
notify() { notify-send "$1" --icon "$NOTIFY_ICON"; }
status() { mullvad status | grep -Eio 'connected|connecting|disconnected' | tr '[:upper:]' '[:lower:]'; }
# get ip directly from relay. won't work if not connected.
# ip() { mullvad status -v | head -n 1 | awk '{print $4}' | grep -Eo '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+'; }
ip() { curl -sfL "$REMOTE_IP"; }
copy() { ip | xclip -selection clipboard; }
city() { mullvad status -v | head -n 1 | awk '{print $6}' | tr ',' ' ' | xargs; }
country() { mullvad status -v | head -n 1 | awk '{print $7}'; }
short() { [[ $(status) == "connected" ]] && echo "${countries[$(country)]}"; }
info() { [[ $(status) == "connected" ]] && echo "Connected to $(city), $(country) ($(ip))"; }
location(){
mullvad relay set location "$1"; sleep 1
if [ "$(status)" == "connected" ]; then
notify "$(info)"
else
notify "Relay location changed to $1"
fi
}
toggle() {
if [[ $(status) == "connected" ]]; then
mullvad disconnect; sleep 1
notify "VPN disconnected."
else
mullvad connect; sleep 1
notify "$(info)"
fi
}
fmt() {
case "$(status)" in
"connected") echo "%{F$COLOR_CONNECTED}${ICON_CONNECTED} $(short)%{F-}";;
"connecting") echo "%{F$COLOR_CONNECTING}${ICON_CONNECTED} ??%{F-}";;
*) echo "%{F$COLOR_DISCONNECTED}$ICON_DISCONNECTED%{F-}";;
esac
}
menu() {
[[ $(command -v rofi) ]] || return
local title="ﱾ Switch VPN relay"
local icon="⚑"
local entries=""
for name in "${!countries[@]}"; do entries+="$icon $name|"; done
selection=$(rofi -location 3 -xoffset 20 -yoffset +38 -sep "|" -dmenu -i -p "$title" <<< "$entries")
[[ "$selection" == "" ]] && return
name="${selection/$icon /}"
code="${countries[$name]}"
location "$code"
}
###
case "$1" in
"toggle") toggle;;
"ip") copy;;
"menu") menu;;
"info") info;;
*) fmt;;
esac