2020-12-19 16:15:45 +01:00
|
|
|
#!/usr/bin/env bash
|
2024-01-06 01:19:35 +01:00
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Simon Bruder <simon@sbruder.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2021-05-31 23:02:11 +02:00
|
|
|
# This reads wg-quick compatible configuration files from
|
2022-09-30 12:02:46 +02:00
|
|
|
# /etc/wireguard/mlv-LOCATION.conf
|
2021-05-31 23:02:11 +02:00
|
|
|
#
|
|
|
|
# Since they are autogenerated by nix and therefore world-readable, they do not
|
|
|
|
# include secrets like the private key and client address. Instead, they are
|
|
|
|
# manually added after wg-quick set up the tunnel by retrieving them with
|
|
|
|
# pass(1) from web/mullvad.net/wireguard.
|
|
|
|
#
|
|
|
|
# Format of pass entry:
|
|
|
|
# PrivateKey: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=
|
|
|
|
# Address4: 10.0.0.1/32
|
|
|
|
# Address6: fd00::1/128
|
|
|
|
set -euo pipefail
|
|
|
|
|
2020-12-19 16:15:45 +01:00
|
|
|
if (( $# < 1 )); then
|
|
|
|
echo "USAGE: $0 LOCATION|off" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-09-30 12:02:46 +02:00
|
|
|
INTERFACE="mlv-$1"
|
2021-05-31 23:02:11 +02:00
|
|
|
|
|
|
|
cmd() {
|
|
|
|
echo "[#] $*" >&2
|
|
|
|
sudo "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
for interface in /sys/class/net/*; do
|
|
|
|
interface="${interface#/sys/class/net/}"
|
2022-09-30 12:02:46 +02:00
|
|
|
[[ $interface =~ ^mlv-(v6-)?[a-z]{2}(-[a-z]{3}-)?[0-9]*$ ]] && cmd wg-quick down "$interface"
|
2020-12-19 16:15:45 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$1" != "off" ]; then
|
2021-06-01 11:37:59 +02:00
|
|
|
# Make sure gpg-agent is unlocked so the period where the interface exists but
|
|
|
|
# no private key is set is minised.
|
|
|
|
pass web/mullvad.net/wireguard >/dev/null
|
|
|
|
|
2021-05-31 23:02:11 +02:00
|
|
|
cmd wg-quick up "$INTERFACE"
|
|
|
|
pass web/mullvad.net/wireguard | while read -r line; do
|
|
|
|
key="${line%%: *}"
|
|
|
|
value="${line#*: }"
|
|
|
|
case "$key" in
|
|
|
|
PrivateKey)
|
|
|
|
cmd wg set "$INTERFACE" private-key /dev/stdin <<< "$value"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
Address4)
|
|
|
|
cmd ip -4 address add "$value" dev "$INTERFACE"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
Address6)
|
|
|
|
cmd ip -6 address add "$value" dev "$INTERFACE"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid key '$key'"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
done
|
2020-12-19 16:15:45 +01:00
|
|
|
fi
|