From 09abbdfcf07f2e4e021bc47bf4f3f767ff17a219 Mon Sep 17 00:00:00 2001 From: Arminas Date: Fri, 10 Feb 2023 15:23:15 +0200 Subject: [PATCH] Created restart-wg.sh file Created wireguard restart script, which compares wg0.conf part between [Interface] and first peer with previous version for changes, and, if there is any - restarts whole wireguard, if the only changes are done to peers, it calls "wg syncconf", eliminating client disconnection after pressing apply config problem. --- restart-wg.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 restart-wg.sh diff --git a/restart-wg.sh b/restart-wg.sh new file mode 100644 index 0000000..dbbe864 --- /dev/null +++ b/restart-wg.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +config="/etc/wireguard/wg0.conf" +old_config="/etc/wireguard/wg0.conf.old" + +if [ ! -f $old_config ]; then + awk '/Interface/,/# ID:/' $config | head -n -1 | tail -n +2 > $old_config + echo "No old config found, restarting wireguard (wg-quick)" + wg-quick down wg0 + systemctl restart wg-quick@wg0.service + exit 0 +fi + +difference=$(diff <(awk '/Interface/,/# ID:/' $config | head -n -1 | tail -n +2) <(cat $old_config)) + +if [ -n "$difference" ]; then + awk '/Interface/,/# ID:/' $config | head -n -1 | tail -n +2 > $old_config + echo "Changes to interface detected, restarting wireguard (wg-quick)" + wg-quick down wg0 + systemctl restart wg-quick@wg0.service + exit 0 +else + awk '/Interface/,/# ID:/' $config | head -n -1 | tail -n +2 > $old_config + echo "No changes to interface detected, restarting wireguard (wg syncconf)" + wg syncconf wg0 <(wg-quick strip wg0) + exit 0 +fi