freyanet/bin/inet2.sh

106 lines
2.5 KiB
Bash
Raw Normal View History

2024-11-17 04:01:08 +00:00
#!/bin/sh
2023-11-11 00:44:48 +00:00
2024-11-17 04:01:08 +00:00
. ${INET2_PREFIX}/share/inet2/inet2.sh
2023-11-11 00:44:48 +00:00
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() {
2023-12-30 20:23:30 +00:00
step "Starting inet2"
2023-11-15 04:25:25 +00:00
2023-12-30 20:23:30 +00:00
step "Removing old Wireguard interfaces"
2023-11-15 04:25:25 +00:00
for file in /sys/class/net/*; do # Clear all wireguard interfaces
type=$(cat "$file/type")
if [ "$type" = "65534" ]; then
2023-12-30 20:23:30 +00:00
ifname="$(basename $file)"
run ip link del "$ifname"
2023-11-15 04:25:25 +00:00
fi
done
2023-12-30 20:23:30 +00:00
rm -fr /run/inet2/wg 2> /dev/null
mkdir -p /run/inet2/wg
2024-11-17 04:01:08 +00:00
${INET2_PREFIX}/bin/mkbirdconfig.sh
2023-12-30 20:23:30 +00:00
step "Setting loopback addresses"
2023-11-11 00:44:48 +00:00
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"
2024-11-17 04:01:08 +00:00
run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter"
2023-11-11 00:44:48 +00:00
# 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() {
2023-12-30 20:23:30 +00:00
step "Stopping inet2"
step "Removing loopback"
getval Loopback | while read -r addr; do
run ip addr del "$addr" dev lo
done
2023-11-11 00:44:48 +00:00
2023-12-30 20:23:30 +00:00
getval interface | while read -r inter; do
runscripts PreDown "$inter"
2023-11-11 00:44:48 +00:00
2023-12-30 20:23:30 +00:00
step "Bringing $inter down"
run ip link del "$inter"
runscripts PostDown "$inter"
done
2023-11-11 00:44:48 +00:00
}
2023-12-30 20:23:30 +00:00
reload() {
getval interface | while read -r inter; do
step "Generating config for $inter"
2024-11-17 04:01:08 +00:00
run ${INET2_PREFIX}/bin/mkwgconfig.sh "$inter" /run/inet2/wg/"$inter" /etc/inet2.conf
2023-12-30 20:23:30 +00:00
step "Setting Wireguard config for $inter"
run wg setconf "$inter" /run/inet2/wg/"$inter"
done
}
restart() {
stop
start
2023-11-11 00:44:48 +00:00
}
2023-12-30 20:23:30 +00:00
$1