DynDNS: Store and compare to IP from last update

This commit is contained in:
fruchti 2023-06-23 21:33:42 +02:00
parent 1a543d6067
commit 774eea3eb3

View file

@ -33,7 +33,11 @@ in
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
systemd.services.dyndns = { systemd.services.dyndns = let
stateDirectory = "dyndns";
homeDirectory = "/var/lib/${stateDirectory}";
in
{
enable = true; enable = true;
after = [ "network.target" ]; after = [ "network.target" ];
unitConfig = { unitConfig = {
@ -46,7 +50,14 @@ in
ProtectSystem = true; ProtectSystem = true;
ProtectHome = true; ProtectHome = true;
NoNewPrivileges = true; NoNewPrivileges = true;
ReadWriteDirectories = [ homeDirectory ];
StateDirectory = stateDirectory;
}; };
# preStart = ''
# #!${pkgs.bash}/bin/bash
# [ -d "${homeDirectory}" ] || mkdir -p "${homeDirectory}"
# '';
script = '' script = ''
#!${pkgs.bash}/bin/bash #!${pkgs.bash}/bin/bash
@ -57,6 +68,7 @@ in
dyndns_server="${cfg.server}" dyndns_server="${cfg.server}"
dyndns_user="${cfg.username}" dyndns_user="${cfg.username}"
dyndns_password="$(cat "${cfg.passwordFile}")" dyndns_password="$(cat "${cfg.passwordFile}")"
state_file="${homeDirectory}/current_ipv6"
new_ip=$(${pkgs.iproute}/bin/ip -6 a show scope global -temporary dev "$interface" | ${pkgs.gnused}/bin/sed -n -E 's/^\ *inet6\ (2001(:[0-9a-f]+)+).*$/\1/p' | head -1) new_ip=$(${pkgs.iproute}/bin/ip -6 a show scope global -temporary dev "$interface" | ${pkgs.gnused}/bin/sed -n -E 's/^\ *inet6\ (2001(:[0-9a-f]+)+).*$/\1/p' | head -1)
@ -65,7 +77,12 @@ in
exit 1 exit 1
fi fi
current_ip=$(${pkgs.dig}/bin/dig aaaa +short "$host") if [ ! -f "$state_file" ] ; then
echo "No state file found, determining currently set IP via DNS query."
${pkgs.dig}/bin/dig aaaa +short "$host" > "$state_file"
fi
current_ip=$(cat "$state_file")
if [ -z "$current_ip" ] ; then if [ -z "$current_ip" ] ; then
echo "Could not determine current AAAA record." echo "Could not determine current AAAA record."
@ -79,6 +96,8 @@ in
echo "Updating IP to $new_ip." echo "Updating IP to $new_ip."
${pkgs.curl}/bin/curl "https://$dyndns_user:$dyndns_password@$dyndns_server/?hostname=$host&myip=$new_ip" ${pkgs.curl}/bin/curl "https://$dyndns_user:$dyndns_password@$dyndns_server/?hostname=$host&myip=$new_ip"
echo "$new_ip" > "$state_file"
''; '';
}; };