From 29b96374f228ea4ed7a703832f30f44f2b26399d Mon Sep 17 00:00:00 2001 From: bergware Date: Tue, 15 Aug 2023 14:16:33 +0200 Subject: [PATCH] Auto update wireguard tunnels when disabling/enabling bridging --- emhttp/plugins/dynamix/scripts/netconfig | 20 ++++++- etc/rc.d/rc.wireguard | 70 ++++++++++++------------ 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/emhttp/plugins/dynamix/scripts/netconfig b/emhttp/plugins/dynamix/scripts/netconfig index e2b937518..13b1314d5 100755 --- a/emhttp/plugins/dynamix/scripts/netconfig +++ b/emhttp/plugins/dynamix/scripts/netconfig @@ -1,7 +1,7 @@ #!/usr/bin/php -q /dev/null 2>&1"); exec("/usr/local/sbin/create_network_ini $ifname >/dev/null 2>&1 &"); + update_wireguard($ifname); } exit(0); ?> diff --git a/etc/rc.d/rc.wireguard b/etc/rc.d/rc.wireguard index 42aba4891..d4ed71fae 100755 --- a/etc/rc.d/rc.wireguard +++ b/etc/rc.d/rc.wireguard @@ -1,7 +1,10 @@ #!/bin/bash # Start/stop wireguard interfaces -log=/var/log/wg-quick.log +SYSTEM=/sys/class/net +WIREGUARD=/etc/wireguard +LOG=/var/log/wg-quick.log +TMP=/tmp/wg-quick.tmp start() { if ! iptables -S | grep -qom1 "WIREGUARD$"; then @@ -12,59 +15,58 @@ start() { ip6tables -N WIREGUARD ip6tables -A FORWARD -j WIREGUARD fi - if [[ ! -d /etc/wireguard ]]; then + if [[ ! -d $WIREGUARD ]]; then mkdir -p /boot/config/wireguard ln -s /boot/config/wireguard /etc fi - tmp=/tmp/wg-quick.tmp - autostart=$(cat /etc/wireguard/autostart 2>/dev/null) + # get active interface + [[ -e $SYSTEM/bond0 ]] && NIC=bond0 || NIC=eth0 + [[ -e $SYSTEM/br0 ]] && NIC=br0 + AUTOSTART=$(cat $WIREGUARD/autostart 2>/dev/null) # Loop thru all configured WG tunnels - for WG in $(ls --indicator-style=none /etc/wireguard/*.conf 2>/dev/null); do + for WG in $(ls --indicator-style=none $WIREGUARD/*.conf 2>/dev/null); do # remove path and extension - WG=${WG##*/}; WG=${WG%.*} + WG=$(basename -s .conf $WG) # create routing table for network used by docker containers - index=$((${WG:2}+200)) - network="172.31.$index.0/24" - if [[ -z $(ip rule|grep -Pom1 "from $network") ]]; then - ip -4 rule add from $network table $index - ip -4 route add unreachable default table $index + TABLE=$((${WG:2}+200)) + NETWORK="172.31.$TABLE.0/24" + if [[ -z $(ip rule|grep -Pom1 "from $NETWORK") ]]; then + ip -4 rule add from $NETWORK table $TABLE + ip -4 route add unreachable default table $TABLE + fi + # interface has changed? + if ! grep -qm1 "dev $NIC " $WIREGUARD/$WG.conf; then + # update wireguard configuration + logger -t $(basename $0) "updated wireguard $WG configuration" + sed -ri "s/dev (br0|bond0|eth0) /dev $NIC /" $WIREGUARD/$WG.conf fi # autostart WG tunnel? - if [[ $autostart == *"$WG"* ]]; then + if [[ $AUTOSTART =~ $WG ]]; then # Get gateway IP address - gw=$(grep -Pom1 '^PostUp=ip -4 route add [\d\.]+/\d+ via \K[\d\.]+' /etc/wireguard/$WG.conf) - if [[ -n $gw ]]; then - timer=10 + GW=$(grep -Pom1 '^PostUp=ip -4 route add [\d\.]+/\d+ via \K[\d\.]+' $WIREGUARD/$WG.conf) + if [[ -n $GW ]]; then + TIMER=10 # wait for gateway to become reachable (max 10 seconds) - while [[ -z $(ip -4 route show default|grep -Pom1 "$gw ") && $timer -gt 0 ]]; do - ((timer--)) + while [[ -z $(ip -4 route show default|grep -Pom1 "$GW ") && $TIMER -gt 0 ]]; do sleep 1 + ((TIMER--)) done fi # start WG tunnel - wg-quick up "$WG" 2>$tmp - echo "wg-quick up $WG (autostart)" >>$log - cat $tmp >>$log - echo >>$log - # WG tunnel for docker container? - if grep -qm1 '^TYPE:1="8"' /etc/wireguard/$WG.cfg; then - # update routing table for WG tunnels used by containers - table=$(grep -Pom1 'fwmark \K[\d]+' $tmp) - route=$(grep -Pom1 '^Address=\K.+$' /etc/wireguard/$WG.conf) - sleep 1 - ip -4 route flush table $table - ip -4 route add $route dev $WG table $table - fi + wg-quick up $WG 2>$TMP + echo "wg-quick up $WG (autostart)" >>$LOG + cat $TMP >>$LOG + echo >>$LOG fi done - rm -f $tmp + rm -f $TMP } stop() { for WG in $(wg show interfaces); do - echo "wg-quick down $WG (autostop)" >>$log - wg-quick down "$WG" 2>>$log - echo >>$log + echo "wg-quick down $WG (autostop)" >>$LOG + wg-quick down $WG 2>>$LOG + echo >>$LOG done }