wg-installer: delete old interfaces
authorNick Hainke <vincent@systemli.org>
Sat, 13 Mar 2021 19:55:02 +0000 (20:55 +0100)
committerNick Hainke <vincent@systemli.org>
Sun, 9 Jan 2022 13:13:06 +0000 (14:13 +0100)
Add "wg_check_interfaces" and specify a timeout in the config file.
This allows to delete not used wireguard-interfaces automatically.

For example a cronjob can be installed that calls:
  . /usr/share/wginstaller/wg_functions.sh && wg_check_interfaces

Signed-off-by: Nick Hainke <vincent@systemli.org>
(cherry picked from commit 36dc9b3f797dd04d075d9b6c36fece68d03470fe)

net/wg-installer/wg-server/config/wgserver.conf
net/wg-installer/wg-server/lib/wg_functions.sh

index 532fb4c6904169b6679089affa6ff65fadaf5fa0..638ac19d89d6d24b6de25206779bd70b337c0974 100644 (file)
@@ -5,3 +5,4 @@ config server
     option base_v4prefix '10.0.0.1/24'
     option wg_key '/root/wg.key'
     option wg_pub '/root/wg.pub'
+    option timeout_handshake '600'
index f9b720576db165e8dc5ff154c01509f7920e1971..639052484e705046f4f2ef1f298daa06e0446c3d 100644 (file)
@@ -1,6 +1,33 @@
 . /usr/share/libubox/jshn.sh
 . /usr/share/wginstaller/wg.sh
 
+wg_timeout () {
+       local int=$1
+
+       handshake=$(wg show $int latest-handshakes | awk '{print $2}')
+       timeout=$(uci get wgserver.@server[0].timeout_handshake)
+
+       if [ $handshake -ge $timeout ]; then
+               echo "1"
+       else
+               echo "0"
+       fi
+}
+
+wg_check_interface () {
+       local int=$1
+       if [ $(wg_timeout $int) -eq "1" ]; then
+               ip link del dev $int
+       fi
+}
+
+wg_check_interfaces () {
+       wg_interfaces=$(wg show interfaces)
+       for interface in $wg_interfaces; do
+               wg_check_interface $interface
+       done
+}
+
 wg_get_usage () {
        num_interfaces = $(wg show interfaces | wc -w)
        json_init