DynDNS: Allow multiple domains

This commit is contained in:
fruchti 2023-08-26 11:11:12 +02:00
parent 845efe9e8f
commit 8dbb7477be

View file

@ -11,9 +11,9 @@ in
description = "Identifier of the network interface to use"; description = "Identifier of the network interface to use";
}; };
domain = lib.mkOption { domains = lib.mkOption {
type = lib.types.str; type = lib.types.listOf lib.types.str;
description = "Domain name to update"; description = "Domain names to update";
}; };
server = lib.mkOption { server = lib.mkOption {
@ -41,7 +41,7 @@ in
enable = true; enable = true;
after = [ "network.target" ]; after = [ "network.target" ];
unitConfig = { unitConfig = {
Description = "Update AAAA records for ${cfg.domain} via DynDNS"; Description = "Update AAAA records via DynDNS";
}; };
serviceConfig = { serviceConfig = {
DynamicUser = true; DynamicUser = true;
@ -53,22 +53,15 @@ in
ReadWriteDirectories = [ homeDirectory ]; ReadWriteDirectories = [ homeDirectory ];
StateDirectory = stateDirectory; StateDirectory = stateDirectory;
}; };
# preStart = ''
# #!${pkgs.bash}/bin/bash
# [ -d "${homeDirectory}" ] || mkdir -p "${homeDirectory}"
# '';
script = '' script = ''
#!${pkgs.bash}/bin/bash #!${pkgs.bash}/bin/bash
set -eu set -eu
host="${cfg.domain}"
interface="${cfg.interface}" interface="${cfg.interface}"
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)
@ -77,27 +70,32 @@ in
exit 1 exit 1
fi fi
if [ ! -f "$state_file" ] ; then for host in ${builtins.concatStringsSep " " cfg.domains} ; do
echo "No state file found, determining currently set IP via DNS query." echo "Checking $host."
${pkgs.dig}/bin/dig aaaa +short "$host" > "$state_file"
fi
current_ip=$(cat "$state_file") state_file="${homeDirectory}/current_ipv6_$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
if [ -z "$current_ip" ] ; then current_ip=$(cat "$state_file")
echo "Could not determine current AAAA record."
exit 1
fi
if [ "$current_ip" = "$new_ip" ] ; then if [ -z "$current_ip" ] ; then
echo "Current AAAA record is already $current_ip, no update needed." echo "Could not determine current AAAA record."
exit 0 exit 1
fi fi
echo "Updating IP to $new_ip." if [ "$current_ip" = "$new_ip" ] ; then
${pkgs.curl}/bin/curl "https://$dyndns_user:$dyndns_password@$dyndns_server/?hostname=$host&myip=$new_ip" echo "Current AAAA record is already $current_ip, no update needed."
continue
fi
echo "$new_ip" > "$state_file" echo "Updating IP to $new_ip."
${pkgs.curl}/bin/curl "https://$dyndns_user:$dyndns_password@$dyndns_server/?hostname=$host&myip=$new_ip"
echo "$new_ip" > "$state_file"
done
''; '';
}; };