1
0

profiles/wg-clerie: Convert systemd timer into a service with sleep

This commit is contained in:
2025-05-08 11:34:05 +02:00
parent 1c087b0c9f
commit 69ccc0c692
2 changed files with 36 additions and 34 deletions

View File

@@ -180,45 +180,16 @@ in
};
systemd.services."wg-clerie-endpoint-refresh" = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
Type = "simple";
Restart = "always";
RestartSec = 5;
};
path = [ pkgs.wireguard-tools pkgs.iproute2 ];
script = ''
set -euo pipefail
# Don't do anything as long as interface is not configured
if ! wg show wg-clerie endpoints > /dev/null; then
exit 0
fi
endpoint=""
if ip route get 2a01:4f8:c0c:15f1::1 ipproto udp dport 51820 &>/dev/null; then
endpoint="[2a01:4f8:c0c:15f1::1]:51820"
else
endpoint="78.47.183.82:51820"
fi
wg set wg-clerie peer "2p1Jqs3bkXbXHFWE6vp1yxHIFoUaZQEARS2nJzbkuBA=" endpoint "''${endpoint}"
'';
requires = [ "network-online.target" ];
after = [ "network-online.target" ];
};
systemd.timers."wg-clerie-endpoint-refresh" = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "*-*-* *:*:0/5";
RandomizedDelaySec = "5s";
};
requires = [ "network-online.target" ];
after = [ "network-online.target" ];
script = builtins.readFile ./wg-clerie-endpoint-refresh.sh;
};
environment.systemPackages = [ pkgs.wireguard-tools ];

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -euo pipefail
prev_endpoint=""
while true; do
if ! wg show wg-clerie endpoints &>/dev/null; then
if [[ "${prev_endpoint}" != "" ]]; then
echo "Interface wg-clerie unavailable, doing nothing"
prev_endpoint=""
fi
sleep 5
continue
fi
if ip route get 2a01:4f8:c0c:15f1::1 ipproto udp dport 51820 &>/dev/null; then
new_endpoint="[2a01:4f8:c0c:15f1::1]:51820"
else
new_endpoint="78.47.183.82:51820"
fi
if [[ "${new_endpoint}" != "${prev_endpoint}" ]]; then
echo "Switching endpoint for wg-clerie to ${new_endpoint}"
wg set wg-clerie peer "2p1Jqs3bkXbXHFWE6vp1yxHIFoUaZQEARS2nJzbkuBA=" endpoint "${new_endpoint}"
prev_endpoint="${new_endpoint}"
fi
sleep 5
done