freyanet/bin/inet2.sh

105 lines
2.5 KiB
Bash
Executable file

#!/bin/sh
. ${INET2_PREFIX}/share/inet2/inet2.sh
runscripts() {
if [ -n "$(getval "interface $2" "$1")" ]; then
step "Running $1 for $2"
getval "interface $2" "$1" | while read -r line; do
(eval "$line")
done
fi
}
start() {
step "Starting inet2"
step "Removing old Wireguard interfaces"
for file in /sys/class/net/*; do # Clear all wireguard interfaces
type=$(cat "$file/type")
if [ "$type" = "65534" ]; then
ifname="$(basename $file)"
run ip link del "$ifname"
fi
done
rm -fr /run/inet2/wg 2> /dev/null
mkdir -p /run/inet2/wg
${INET2_PREFIX}/bin/mkbirdconfig.sh
step "Setting loopback addresses"
getval Loopback | while read -r addr; do
run ip addr add "$addr" dev lo
done
getval interface | while read -r inter; do
step "Generating config for $inter"
run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter"
# create the wireguard interface *in the default namespace*
step "Adding Wireguard interface $inter"
run ip link add name "$inter" type wireguard
# set up the new network from the config
step "Setting Wireguard config for $inter"
run wg setconf "$inter" /run/inet2/wg/"$inter"
# the config doesn't actually add any addresses, do that here
step "Adding host addresses for $inter"
getval "interface $inter" Address | while read -r addr; do
run ip addr add "$addr" dev "$inter"
done
runscripts PreUp "$inter"
step "Bringing interface up"
run ip link set dev "$inter" up
getval "interface $inter" Route | while read -r line; do
read -r route via addr2 < <(printf "%s" "$line")
if [ "$via" = "via" ]; then
run ip route add "$route" via "$addr2" dev "$inter"
else
run ip route add "$route" dev "$inter"
fi
done
runscripts PostUp "$inter"
done
step "Done!"
}
stop() {
step "Stopping inet2"
step "Removing loopback"
getval Loopback | while read -r addr; do
run ip addr del "$addr" dev lo
done
getval interface | while read -r inter; do
runscripts PreDown "$inter"
step "Bringing $inter down"
run ip link del "$inter"
runscripts PostDown "$inter"
done
}
reload() {
getval interface | while read -r inter; do
step "Generating config for $inter"
run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" /etc/inet2.conf
step "Setting Wireguard config for $inter"
run wg setconf "$inter" /run/inet2/wg/"$inter"
done
}
restart() {
stop
start
}
$1