-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlvdsel
executable file
·165 lines (135 loc) · 4.06 KB
/
mlvdsel
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
service_prefix='wg-quick@mullvad-'
die() {
${rc:1}
echo "$@" && exit $rc
}
get_list_of_configs() {
sudo ls /etc/wireguard | sed -e '/mullvad-[a-z]\+[0-9]\+.conf/!d'
}
get_list_of_servers() {
get_list_of_configs | sed -e 's/^.*-\([a-z]\+[0-9]\+\).*$/\1/'
}
get_list_of_countries() {
get_list_of_servers | grep -oe '[a-z]\+' | sort -u
}
get_list_of_country_servers() {
local country
country="$1"
get_list_of_servers | grep "$country" | grep -oe '[0-9]\+' | sort -V
}
get_list_of_active_servers() {
sudo systemctl list-units -a -o json "$service_prefix*.service" | jq -r '.[] | .unit' | sed -e 's/.*@mullvad-\([^\.]\+\).service/\1/'
}
ask_one_of() {
local message input
message="$1"
shift
echo "$message" >&2
while read -r input; do
if [ -n "$input" ] && grep -wq "$input" <<< "$@"; then
echo "$input"
break
else
echo "$message" >&2
fi
done
}
# CHECK REQUIREMENTS
# ------------------
for binary in wg-quick systemctl sudo; do
which "$binary" &>/dev/null || die "Required '$binary' is not available."
done
# OPTION PARSING
# --------------
print_help() {
cat <<EOF
Usage: $0 [[-c COUNTRY_CODE] [-s SERVER_NR]|-r|-h]
Options:
-c specify country (* for random)
-s specify server (* for random)
-r random country and server
-l list active server
-e start active server
-d stop active server
-h show this message
EOF
}
country=''
server=''
list_active=false
change_state=''
while getopts "c:s:r?l?e?d??h" opt; do
case $opt in
c) country="$OPTARG" ;;
s) server="$OPTARG" ;;
r) country='*'
server='*' ;;
l) list_active=true ;;
e) change_state=start ;;
d) change_state=stop ;;
*) print_help;
exit 0 ;;
esac
done
sudo -v || die "Need sudo rights to read wireguard configs and toggle daemons"
# ESTABLISHING COUNTRY AND SERVER
# -------------------------------
readarray -t active_servers < <(get_list_of_active_servers)
if $list_active; then
for server in "${active_servers[@]}"; do
echo -n "$server "
sudo systemctl is-active "$service_prefix$server"
done
exit 0
fi
if [ -n "$change_state" ]; then
for server in "${active_servers[@]}"; do
sudo systemctl "$change_state" "$service_prefix$server"
done
exit 0
fi
readarray countries < <(get_list_of_countries)
if [ "${#countries[@]}" -lt 1 ]; then
die "No available mullvad configs found under /etc/wireguard"
fi
if [ -z "$country" ]; then
echo "${countries[@]}" | xargs printf "%.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s\n"
country="$(ask_one_of 'Please select available country (* for random):' "${countries[@]}" '*')"
fi
if [ "$country" = '*' ]; then
size="${#countries[@]}"
index="$((RANDOM % size))"
country=${countries[$index]//[$'\t\r\n']}
fi
readarray servers < <(get_list_of_country_servers "$country")
if [ "${#countries[@]}" -lt 1 ]; then
die "No available mullvad configs found for country ($country)"
elif [ "${#servers[@]}" -eq 1 ]; then
server="${servers[0]//[$'\t\r\n']}"
fi
if [ -z "$server" ]; then
echo "${servers[@]}" | xargs printf "%.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s %.3s\n"
server="$(ask_one_of 'Please select available server (* for random):' "${servers[@]}" '*')"
fi
if [ "$server" = '*' ]; then
size="${#servers[@]}"
index="$((RANDOM % size))"
server=${servers[$index]//[$'\t\r\n']}
fi
new_server="${country}${server}"
if ! grep -qe '^[a-z]\+[0-9]\+$' <<<"$new_server"; then
die "Invalid server name ($new_server)"
fi
echo "Selected server $new_server!"
# TOGGLING SERVICES
# -----------------
readarray -t active_servers < <(get_list_of_active_servers)
if [ ${#active_servers[@]} -gt 0 ]; then
for active_server in "${active_servers[@]}"; do
echo "Disabling and stopping active server ($active_server)"
sudo systemctl disable --now "$service_prefix$active_server" 2>&1 | sed 's/^/\t/'
done
fi
echo "Enabling and starting selected server ($new_server)"
sudo systemctl enable --now "$service_prefix$new_server" 2>&1 | sed 's/^/\t/'