-
Notifications
You must be signed in to change notification settings - Fork 2
/
mullvad-netns.bash
413 lines (341 loc) · 11.9 KB
/
mullvad-netns.bash
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env bash
#
# Author: Patrick McLean <chutzpah@gentoo.org>
#
# SPDX-License-Identifier: GPL-2.0+
CONFIG_FILE=${MULLVAD_NETNS_CONF:-"/etc/mullvad-netns/config"}
# all these variables can be overriden in the config file
MULLVAD_PORT=51820
RELAYS_URI="https://api.mullvad.net/public/relays/wireguard/v1/"
MULLVAD_API_URI="https://api.mullvad.net/wg/"
# the default country to select a server from, list of available countries is available with
# curl https://api.mullvad.net/public/relays/wireguard/v1/ | jq ".countries[].name"
# defaults to USA
COUNTRY="usa"
# the default city to select a server from, list of available cities withing a country available with
#curl -s https://api.mullvad.net/public/relays/wireguard/v1/ | jq ".countries[] | select(.name == \"${COUNTRY}\") | .cities[].name"
# defaults to any server in California
CITY=", ca"
# file name to find the account in
ACCOUNT_FILENAME="/etc/mullvad-netns/account"
# directory to store keys in
WG_KEYFILE="/etc/mullvad-netns/privatekey"
# location of cache of servers list
SERVERS_CACHE="/var/cache/mullvad-netns/mullvad-servers.json"
# how often to refresh cache of mullvad servers
SERVERS_CACHE_MAX_AGE="1 day"
# location to load nftables rules from
NFTABLES_RULESET="/etc/mullvad-netns/rules.nft"
# list of nameservers to use inside the netns
NAMESERVERS=(
"193.138.218.74"
"1.1.1.1"
"1.0.0.1"
"2606:4700:4700::1111"
"2606:4700:4700::1001"
)
# make sure these are empty
declare -a TEMPFILES=()
unset netns
cleanup() {
rm -f "${TEMPFILES[@]}"
# remove the network namespace if it's empty
[[ -n ${netns} && -z $(ip netns pids "${netns}") ]] && ip netns del "${netns}"
}
_curl() {
# drop privliges and run curl
runuser -u nobody -- curl --location --fail --fail-early --silent --show-error "${@}"
}
_jq() {
# drop privliges and run jq
runuser -u nobody -- jq "${@}"
}
_trim_spaces() {
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
mullvad_update_server_list() {
# atomically update the mullvad server list cache
if [[ ! -d ${SERVERS_CACHE%/*} ]]; then
mkdir -p "${SERVERS_CACHE%/*}" || return
fi
TEMPFILES+=("$(mktemp "${SERVERS_CACHE%/*}/.mullvad-servers-XXXXXX.json")") || return
local tempfile="${TEMPFILES[-1]}"
_curl "${RELAYS_URI}" | _jq . > "${tempfile}" || { rm -f "${tempfile}"; return 1; }
chmod 0644 "${tempfile}" || return
mv -f "${tempfile}" "${SERVERS_CACHE}"
}
mullvad_select_random_server() {
local country="${1:-${COUNTRY}}" city="${2:-${CITY}}"
if [[ -r ${SERVERS_CACHE} ]]; then
if [[ $(date -u -r "${SERVERS_CACHE}" +%s) -lt $(date -u --date="-${SERVERS_CACHE_MAX_AGE}" +%s) ]]; then
mullvad_update_server_list || return
fi
else
mullvad_update_server_list || return
fi
local country_select city_select
[[ -n ${country} ]] && country_select="| select(.name | test(\"${country}\"; \"i\"))"
[[ -n ${city} ]] && city_select="| select(.name | test(\"${city}\"; \"i\"))"
local -a server_list
readarray -t server_list < <(_jq -r "
(.countries[] ${country_select}
| (.cities[] ${city_select}
| (.relays[]
| [.hostname, .public_key, .ipv4_addr_in, .ipv6_addr_in])
)
)
| flatten
| join(\"\\t\")" "${SERVERS_CACHE}"); wait "${!}" || return
local server_count="${#server_list[@]}"
printf -- '%s\n' "${server_list[$((RANDOM % server_count))]}"
}
mullvad_set_local_ips() {
local pubkey="${1}"
local account_lines account
if [[ ! -r ${ACCOUNT_FILENAME} ]]; then
printf -- '%s: Could not find Mullvad account file at "%s"\n' "${progname}" "${ACCOUNT_FILENAME}" >&2
return 1
elif [[ $(($(stat --format='0%a' "${ACCOUNT_FILENAME}") & 0133)) -ne 0 ]]; then
printf -- '%s: Mullvad account file "%s" should not be readable or writeable by others\n' "${progname}" "${ACCOUNT_FILENAME}" >&2
return 1
elif ! readarray -t account_lines < "${ACCOUNT_FILENAME}"; then
printf -- '%s: Cound not read Mullvad account from "%s"\n' "${progname}" "${ACCOUNT_FILENAME}" >&2
return 1
fi
local account_line
for account_line in "${account_lines[@]}"; do
[[ ${account_line} == \#* ]] && continue
if [[ ${account_line} =~ ^[[:space:]]*((([0-9]{4}[[:space:]]+){3}[0-9]{4})|[0-9]{16})[[:space:]]*(#.*|)$ ]]; then
account="$(_trim_spaces "${account_line%#*}")"
else
printf -- '%s: WARNING skipping invalid account "%s"\n' "${progname}" "${account_line}" >&2
continue
fi
done
if [[ ! ${account} =~ ^((([0-9]{4}[[:space:]]+){3}[0-9]{4})|[0-9]{16})$ ]]; then
printf -- '%s: Could not find valid Mullvad account in "%s"\n' "${progname}" "${ACCOUNT_FILENAME}" >&2
return 1
fi
local address
address="$(_curl "${MULLVAD_API_URI}" \
-d account="${account// /}" \
--data-urlencode pubkey="${pubkey}")" || return
if [[ ! ${address} =~ ^[0-9.]+/[0-9]{1,2},[a-f0-9:]+/[0-9]{1,3}$ ]]; then
printf -- '%s\n' "${address}" >&2
return 1
fi
IFS=',' read -r local_ipv4 local_ipv6 <<< "${address}" || return
}
get_wireguard_keys() {
local keyfile="${WG_KEYFILE:-/etc/wireguard/mullvad/privatekey}"
local keydir="${keyfile%/*}"
if [[ ! -d ${keydir} ]]; then
mkdir -p "${keydir}" || return
chmod 0755 "${keydir}" || return
fi
local privatekey pubkey
if [[ -r ${keyfile} ]]; then
if [[ $(($(stat --format='0%a' "${keyfile}") & 0133)) -ne 0 ]]; then
printf -- '%s: Private key file "%s" should not be readable or writeable by others\n' "${progname}" "${keyfile}" >&2
return 1
fi
privatekey="$(<"${keyfile}")" || return
else
privatekey=$(set -o pipefail; umask 077; wg genkey | tee "${keyfile}") || return
fi
pubkey="$(wg pubkey <<< "${privatekey}")" || return
printf -- '%s %s\n"' "${privatekey}" "${pubkey}"
}
setup_interface() {
# script to setup the network namespace
local -a setup_script=(
"netns add ${netns}"
"link add dev ${linkname} type wireguard"
"link set netns ${netns} ${linkname}"
)
# scripts that get run for the final steps
local -a address_script=(
"address add ${local_ipv4} dev ${linkname}"
)
local -a address6_script=(
"address add ${local_ipv6} dev ${linkname}"
)
local -a linkup_script=(
"link set up dev lo"
"link set up dev ${linkname}"
)
local -a routes_script=(
"route add default dev ${linkname} scope global"
)
local -a routes6_script=(
"route add default dev ${linkname} scope global"
)
# initial setup
if ! ip -batch - <<< "$(printf '%s\n' "${setup_script[@]}")"; then
ip link del "${linkname}" 2>/dev/null
ip netns del "${linkname}" 2>/dev/null
return 1
fi
local endpoint
if [[ -n ${ipv6} ]]; then
endpoint="${ipv6_addr}:${MULLVAD_PORT}"
else
endpoint="${ipv4_addr}:${MULLVAD_PORT}"
fi
# configure the wireguard interface in the netns
if ! ip netns exec "${netns}" wg set "${linkname}" \
private-key <(printf -- '%s\n' "${private_key}") \
peer "${pubkey}" \
allowed-ips '0.0.0.0/0,::0/0' \
endpoint "${endpoint}"
then
ip netns del "${linkname}"
return 1
fi
# load nftables rules in to netns before bringing up interface
if [[ -n ${NFTABLES_RULESET} && -r ${NFTABLES_RULESET} ]]; then
if ! ip netns exec "${netns}" nft -f "${NFTABLES_RULESET}"; then
ip netns del "${linkname}"
return 1
fi
fi
# configure addresses on interfaces, bring them up and initialize the routes
if ! (
set -e
ip -family inet -netns "${netns}" -batch - <<< "$(printf -- "%s\n" "${address_script[@]}")"
ip -family inet6 -netns "${netns}" -batch - <<< "$(printf -- "%s\n" "${address6_script[@]}")"
ip -netns "${netns}" -batch - <<< "$(printf -- "%s\n" "${linkup_script[@]}")"
ip -family inet -netns "${netns}" -batch - <<< "$(printf -- "%s\n" "${routes_script[@]}")"
ip -family inet6 -netns "${netns}" -batch - <<< "$(printf -- "%s\n" "${routes6_script[@]}")"
); then
ip netns del "${linkname}"
return 1
fi
return 0
}
name_netns() {
local name="${linkname}" counter=0
# make sure the network namespace name isn't already in use
while ip netns list | grep -q -F -- "${name}"; do
((counter++))
name="${linkname}-${counter}"
done
if [[ -z ${name} ]]; then
printf '%s: could not find a valid netns name\n' "${progname}" >&2
return 1
fi
# we will use the linkname as the netns for now
netns="${name}"
}
setup_mount_namespace() {
TEMPFILES+=("$(mktemp --tmpdir="${TMPDIR:-/tmp}" mullvad-resolvconf-XXXXXX)") || return
local tempfile="${TEMPFILES[-1]}"
printf "nameserver %s\n" "${NAMESERVERS[@]}" > "${tempfile}"
chmod 0644 "${tempfile}" || return
local run_command
run_command="$(printf -- '"%s" ' "${@}")"
local -a mountns_command
mountns_command=(
"mount --bind \"${tempfile}\" /etc/resolv.conf"
"&& exec ip netns exec \"${netns}\""
"runuser --pty --shell=$(command -v bash) --command='${run_command}' - ${SUDO_USER}"
)
unshare --mount bash -c "${mountns_command[*]}"
}
show_usage() {
printf 'Usage:\n'
printf ' %s [options] -- <command>\n' "${progname}"
printf ' %s <command>\n\n' "${progname}"
printf 'Run <command> under a network namespace connected to a randomly selected\n'
printf 'Mullvad server over WireGuard as the only visible network device. This\n'
printf 'ensures that the command does not have access to the network except through\n'
printf 'the Mullvad tunnel.\n\nOptions\n'
printf ' -C, --country <regex> use only servers from countries matching the\n'
printf ' given regular expression\n'
printf ' -c, --city <regex> use only servers from cities matching the\n'
printf ' given regular expression\n\n'
printf ' -4, --ipv4 connect to the Mullvad server over IPv4 (the default)\n'
printf ' -6, --ipv6 connect to the Mullvad server over IPv6\n\n'
printf ' -h, --help display this help\n'
}
parse_args() {
# parse command line options
local params
if ! params="$(getopt -o '+C:c:u:46h' -l 'country:,city:,user:,ipv4,ipv6,help' -n "${progname}" -- "${@}")"; then
show_usage
return 1
fi
eval set -- "${params}"
while [[ ${#} -gt 0 ]]; do
case ${1} in
-C|--country) country=${2}; shift;;
-c|--city) city="${2}"; shift;;
-4|--ipv4)
if [[ -n ${ipv6} ]]; then
printf -- '%s: cannot specify both --ipv4 and --ipv6\n' "${progname}" >&2
return 1
fi
ipv4=1
;;
-6|--ipv6)
if [[ -n ${ipv4} ]]; then
printf -- '%s: cannot specify both --ipv4 and --ipv6\n' "${progname}" >&2
return 1
fi
ipv6=1
;;
-h|--help) show_usage; exit 0;;
--) shift; break;;
esac
shift
done
if [[ -z ${*} ]]; then
printf '%s: Must specify a command to run\n' "${progname}"
show_usage
return 1
fi
args=("${@}")
}
main() {
set -o pipefail
local progname="${BASH_SOURCE[0]##*/}"
trap cleanup EXIT
if [[ ${EUID} -ne 0 ]]; then
printf -- '%s: superuser privileges requires\n' "${progname}" >&2
return 1
elif [[ $(id --group) -ne 0 ]]; then
printf -- '%s: must be run with GID 0\n' "${progname}" >&2
return 1
fi
# source the config file if it exists
if [[ -r ${CONFIG_FILE} ]]; then
if [[ ! -O ${CONFIG_FILE} || ! -G ${CONFIG_FILE} ]]; then
printf -- '%s: Config file "%s" must be owned by root:root\n' "${progname}" "${CONFIG_FILE}" >&2
return 1
elif [[ $(($(stat --format='0%a' "${CONFIG_FILE}") & 0122)) -ne 0 ]]; then
printf -- '%s: Config file "%s" must not be writeable by group or other\n' "${progname}" "${CONFIG_FILE}" >&2
return 1
fi
source "${CONFIG_FILE}" || return
fi
if [[ -z ${SUDO_USER} ]]; then
printf '%s: SUDO_USER is unset, cannot run command as user\n' "${progname}" >&2
return 1
fi
local city="${CITY}" country="${COUNTRY}" ipv4 ipv6
local -a args
parse_args "${@}" || return
local private_key public_key
read -r private_key public_key < <(get_wireguard_keys); wait "${!}" || return
local linkname pubkey ipv4_addr ipv6_addr
read -r linkname pubkey ipv4_addr ipv6_addr < \
<(mullvad_select_random_server "${country}" "${city}"); wait "${!}" || return
name_netns || return
local local_ipv4 local_ipv6
mullvad_set_local_ips "${public_key}" || return ${?}
setup_interface || return
setup_mount_namespace "${args[@]}" || return
}
main "${@}"