From 7bb498680f631edf09dd4e3577884d2d904eac7b Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Tue, 29 Apr 2025 10:19:11 +0200 Subject: [PATCH] fix: change script --- modules/cloonar-assistant/updns/default.nix | 84 ++++++++++----------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/modules/cloonar-assistant/updns/default.nix b/modules/cloonar-assistant/updns/default.nix index 38e2ee9..33e3691 100644 --- a/modules/cloonar-assistant/updns/default.nix +++ b/modules/cloonar-assistant/updns/default.nix @@ -11,48 +11,48 @@ }; ### 3) Write the check‐script into /etc/external-ip/check.sh (0400, executable) - environment.etc."updns-client/run.sh".text = lib.mkIf config.cloonar-assistant.updns-client.enable lib.concatStringsSep "\n" [ - "#!/usr/bin/env bash" - "set -euo pipefail" - "" - "# Where our secret lives (encrypted)" - "SECRET=${config.cloonar-assistant.updns-client.secretFile}" - "# Where we record the last‐seen IP" - "LAST_IP_FILE=/var/lib/updns-client/last-ip" - "" - "# Decrypt the API key at runtime" - "API_KEY=$(cat \"$SECRET\")" - "" - "# Fetch current external IP" - "IP=$(curl -fsSL https://ifconfig.me)" - "" - "# Ensure state directory exists" - "mkdir -p \"$(dirname \"$LAST_IP_FILE\")\"" - "" - "# Read old IP (if any)" - "if [[ -f \"$LAST_IP_FILE\" ]]; then" - " OLD_IP=$(< \"$LAST_IP_FILE\")" - "else" - " OLD_IP=\"\"" - "fi" - "" - "# If it's changed, notify the API and update the file" - "if [[ \"$IP\" != \"$OLD_IP\" ]]; then" - "" - " PAYLOAD=$(jq -n \\" - " --arg key \"${config.cloonar-assistant.updns-client.key}\" \\" - " --arg secret \"$SECRET\" \\" - " --arg host \"${config.cloonar-assistant.domain}\" \\" - " --arg ip \"$IP\" \\" - " '{key: $key, secret: $secret, host: $host, ip: $ip}')" - "" - " curl -fsS -X POST https://updns-client.cloonar.com/update \\" - " -H \"Content-Type: application/json\" \\" - " -d \"$PAYLOAD\"" - "" - " echo \"$IP\" > \"$LAST_IP_FILE\"" - "fi" - ]; + environment.etc."updns-client/run.sh".text = lib.mkIf config.cloonar-assistant.updns-client.enable '' + #!/usr/bin/env bash + set -euo pipefail + + # Where our secret lives (encrypted) + SECRET=${config.cloonar-assistant.updns-client.secretFile} + # Where we record the last‐seen IP + LAST_IP_FILE=/var/lib/updns-client/last-ip + + # Decrypt the API key at runtime + API_KEY=$(cat "$SECRET") + + # Fetch current external IP + IP=$(curl -fsSL https://ifconfig.me) + + # Ensure state directory exists + mkdir -p "$(dirname \"$LAST_IP_FILE\")" + + # Read old IP (if any) + if [[ -f "$LAST_IP_FILE" ]]; then + OLD_IP=$(< "$LAST_IP_FILE") + else + OLD_IP="" + fi + + # If it's changed, notify the API and update the file + if [[ "$IP" != "$OLD_IP" ]]; then + + PAYLOAD=$(jq -n \ + --arg key \"${config.cloonar-assistant.updns-client.key}" \ + --arg secret "$SECRET" \ + --arg host "${config.cloonar-assistant.domain}" \ + --arg ip "$IP" \ + '{key: $key, secret: $secret, host: $host, ip: $ip}') + + curl -fsS -X POST https://updns-client.cloonar.com/update \ + -H "Content-Type: application/json" \ + -d "$PAYLOAD" + + echo "$IP" > "$LAST_IP_FILE" + fi + ''; environment.etc."updns-client/run.sh".mode = "0500"; ### 4) Ensure /var/lib/external-ip exists on boot