fix: change script

This commit is contained in:
2025-04-29 10:19:11 +02:00
parent ddbc35c690
commit 7bb498680f

View File

@@ -11,48 +11,48 @@
}; };
### 3) Write the checkscript into /etc/external-ip/check.sh (0400, executable) ### 3) Write the checkscript 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" [ environment.etc."updns-client/run.sh".text = lib.mkIf config.cloonar-assistant.updns-client.enable ''
"#!/usr/bin/env bash" #!/usr/bin/env bash
"set -euo pipefail" set -euo pipefail
""
"# Where our secret lives (encrypted)" # Where our secret lives (encrypted)
"SECRET=${config.cloonar-assistant.updns-client.secretFile}" SECRET=${config.cloonar-assistant.updns-client.secretFile}
"# Where we record the lastseen IP" # Where we record the lastseen IP
"LAST_IP_FILE=/var/lib/updns-client/last-ip" LAST_IP_FILE=/var/lib/updns-client/last-ip
""
"# Decrypt the API key at runtime" # Decrypt the API key at runtime
"API_KEY=$(cat \"$SECRET\")" API_KEY=$(cat "$SECRET")
""
"# Fetch current external IP" # Fetch current external IP
"IP=$(curl -fsSL https://ifconfig.me)" IP=$(curl -fsSL https://ifconfig.me)
""
"# Ensure state directory exists" # Ensure state directory exists
"mkdir -p \"$(dirname \"$LAST_IP_FILE\")\"" mkdir -p "$(dirname \"$LAST_IP_FILE\")"
""
"# Read old IP (if any)" # Read old IP (if any)
"if [[ -f \"$LAST_IP_FILE\" ]]; then" if [[ -f "$LAST_IP_FILE" ]]; then
" OLD_IP=$(< \"$LAST_IP_FILE\")" OLD_IP=$(< "$LAST_IP_FILE")
"else" else
" OLD_IP=\"\"" OLD_IP=""
"fi" fi
""
"# If it's changed, notify the API and update the file" # If it's changed, notify the API and update the file
"if [[ \"$IP\" != \"$OLD_IP\" ]]; then" if [[ "$IP" != "$OLD_IP" ]]; then
""
" PAYLOAD=$(jq -n \\" PAYLOAD=$(jq -n \
" --arg key \"${config.cloonar-assistant.updns-client.key}\" \\" --arg key \"${config.cloonar-assistant.updns-client.key}" \
" --arg secret \"$SECRET\" \\" --arg secret "$SECRET" \
" --arg host \"${config.cloonar-assistant.domain}\" \\" --arg host "${config.cloonar-assistant.domain}" \
" --arg ip \"$IP\" \\" --arg ip "$IP" \
" '{key: $key, secret: $secret, host: $host, ip: $ip}')" '{key: $key, secret: $secret, host: $host, ip: $ip}')
""
" curl -fsS -X POST https://updns-client.cloonar.com/update \\" curl -fsS -X POST https://updns-client.cloonar.com/update \
" -H \"Content-Type: application/json\" \\" -H "Content-Type: application/json" \
" -d \"$PAYLOAD\"" -d "$PAYLOAD"
""
" echo \"$IP\" > \"$LAST_IP_FILE\"" echo "$IP" > "$LAST_IP_FILE"
"fi" fi
]; '';
environment.etc."updns-client/run.sh".mode = "0500"; environment.etc."updns-client/run.sh".mode = "0500";
### 4) Ensure /var/lib/external-ip exists on boot ### 4) Ensure /var/lib/external-ip exists on boot