diff --git a/scripts/dynu/dynu-environment b/scripts/dynu/dynu-environment new file mode 100644 index 0000000..a1b7dc0 --- /dev/null +++ b/scripts/dynu/dynu-environment @@ -0,0 +1,16 @@ +# Example Dynu DDNS credentials +# Don't use your main account password. Use the IP update password or its hash instead. +# Secure the env file from local users read: +# chmod 600 /etc/conf.d/dynu-environment +# chown root:root /etc/conf.d/dynu-environment + +# No quotes/spaces +# Your Dynu hostname (e.g., subdomain you registered) +DYNU_HOST=your-subdomain.dynu.net + +# Dynu username (account login) +DYNU_USER=your-username + +# Dynu DDNS IP Update Password (not account password): Dynu API supports passwords as plain text or MD5 hashes +DYNU_PASSWORD=ip-update-password-or-hash + diff --git a/scripts/dynu/dynu-ip-update.md b/scripts/dynu/dynu-ip-update.md new file mode 100644 index 0000000..6050b74 --- /dev/null +++ b/scripts/dynu/dynu-ip-update.md @@ -0,0 +1,15 @@ +# Set up instructions and notes + + +```plaintext +timer → service → script + ├─ lock (no concurrency) + ├─ DNS attempt (logged) + ├─ HTTP fallback (logged) + ├─ validate + ├─ compare with state + ├─ update Dynu (bounded time) + ├─ atomic state write + └─ structured logs +``` + diff --git a/scripts/dynu/dynu.service b/scripts/dynu/dynu.service new file mode 100644 index 0000000..497382c --- /dev/null +++ b/scripts/dynu/dynu.service @@ -0,0 +1,14 @@ +# This service is responsible for updating the Dynu DDNS record with the current IP address +# Copy this to /etc/systemd/system/dynu.service +# This script is intended to run via systemd +[Unit] +Description=Dynu IP Monitor & Updater + +[Service] +Type=oneshot +StateDirectory=dynu +EnvironmentFile=/etc/conf.d/dynu-environment +ExecStart=/usr/local/bin/ip-monitor.sh +StandardOutput=journal +StandardError=journal +TimeoutSec=30 \ No newline at end of file diff --git a/scripts/dynu/dynu.timer b/scripts/dynu/dynu.timer new file mode 100644 index 0000000..1626dfc --- /dev/null +++ b/scripts/dynu/dynu.timer @@ -0,0 +1,13 @@ +# This timer is used to run the dynu.service +# Copy this to /etc/systemd/system/dynu.timer +[Unit] +Description=Run Dynu updater periodically + +[Timer] +OnBootSec=1min +OnUnitActiveSec=15min +AccuracySec=10s +Persistent=true + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/scripts/dynu/ip-monitor.sh b/scripts/dynu/ip-monitor.sh new file mode 100755 index 0000000..20864dd --- /dev/null +++ b/scripts/dynu/ip-monitor.sh @@ -0,0 +1,162 @@ +#!/bin/bash +# Dynu DDNS IP monitor and updater +# +# Purpose: +# Detect current public IPv4 address and update Dynu DDNS record if it changed. +# +# Design: +# - Two independent IP sources: +# 1. DNS (dig) → fast, preferred, optional +# 2. HTTP (curl) → reliable fallback, required +# - Strict validation before any state mutation +# - Atomic state updates +# - Structured logging (journald-friendly) +# - Concurrency control via flock +# +# Execution model: +# systemd timer → service → this script +# +# Failure philosophy: +# - Fail fast on misconfiguration +# - Never persist invalid state +# - Prefer fallback over retry loops +# - Always log observable events + +set -euo pipefail + +# --- configuration --- +STATE_DIR="${STATE_DIRECTORY:-/var/lib/dynu}" +LAST_IP_FILE="$STATE_DIR/last_ip" + +DIG_CMD=$(command -v dig || true) # optional +CURL_CMD=$(command -v curl) || { echo "curl not found"; exit 1; } +MKDIR_CMD=$(command -v mkdir) || { echo "mkdir not found"; exit 1; } + +"$MKDIR_CMD" -p "$STATE_DIR" + +# --- environment validation (fail fast) --- +: "${DYNU_HOST:?missing}" +: "${DYNU_USER:?missing}" +: "${DYNU_PASSWORD:?missing}" + +# --- logging --- +# Single-line structured logs for journald +log() { + printf '%s dynu-monitor %s\n' "$(date -Iseconds)" "$*" +} + +# --- concurrency control --- +# Prevent overlapping executions (e.g., slow runs or manual triggers) +exec 9>"$STATE_DIR/lock" +flock -n 9 || { log "event=skip reason=lock_held"; exit 0; } + +# --- validation --- +# Strict IPv4 validation (format + numeric bounds) +is_valid_ipv4() { + local ip=$1 + local a b c d + + [[ $ip =~ ^((0|[1-9][0-9]{0,2})\.){3}(0|[1-9][0-9]{0,2})$ ]] || return 1 + IFS='.' read -r a b c d <<< "$ip" + (( 10#$a <= 255 && 10#$b <= 255 && 10#$c <= 255 && 10#$d <= 255 )) +} + +# --- providers --- +# DNS-based IP discovery (fast, optional) +get_ip_from_dns() { + "$DIG_CMD" +short +time=2 +tries=1 myip.opendns.com @resolver1.opendns.com \ + | head -n1 | tr -d '[:space:]' +} + +# HTTP-based IP discovery (fallback, reliable) +get_ip_from_http() { + "$CURL_CMD" -s --fail --max-time 5 https://ifconfig.me \ + | head -n1 | tr -d '[:space:]' +} + +# --- discovery --- +RAW_IP="" +SOURCE="dns" + +# Attempt DNS if available +if [[ -n "$DIG_CMD" ]]; then + if RAW_IP="$(get_ip_from_dns 2>/dev/null)"; then + log "event=attempt source=dns value='$RAW_IP'" + else + log "event=error source=dns reason=command_failed" + RAW_IP="" + fi +else + log "event=skip source=dns reason=missing_dependency" +fi + +# Fallback to HTTP if DNS result is invalid or missing +if ! is_valid_ipv4 "$RAW_IP"; then + log "event=fallback from=dns reason=invalid_or_missing_response value='$RAW_IP'" + + SOURCE="http" + if RAW_IP="$(get_ip_from_http 2>/dev/null)"; then + log "event=attempt source=http value='$RAW_IP'" + else + log "event=error source=http reason=command_failed" + RAW_IP="" + fi +fi + +# --- final validation (critical invariant) --- +if ! is_valid_ipv4 "$RAW_IP"; then + log "event=error reason=invalid_ip source=$SOURCE value='$RAW_IP'" + exit 1 +fi + +CURRENT_IP="$RAW_IP" +log "event=discovered source=$SOURCE ip=$CURRENT_IP" + +# --- state load --- +LAST_IP="" +if [[ -f "$LAST_IP_FILE" ]]; then + LAST_IP="$(tr -d '[:space:]' < "$LAST_IP_FILE" || true)" + + if ! is_valid_ipv4 "$LAST_IP"; then + log "event=warning reason=invalid_last_ip value='$LAST_IP'" + LAST_IP="" + fi +fi + +# --- compare --- +if [[ "$CURRENT_IP" == "$LAST_IP" ]]; then + log "event=no_change ip=$CURRENT_IP" + exit 0 +fi + +log "event=change old_ip=${LAST_IP:-none} new_ip=$CURRENT_IP" + +# --- update Dynu --- +UPDATE_RESPONSE="$( + "$CURL_CMD" -s --fail --max-time 5 \ + "https://api.dynu.com/nic/update?hostname=${DYNU_HOST}&username=${DYNU_USER}&password=${DYNU_PASSWORD}&myip=${CURRENT_IP}" \ + || true +)" + +log "event=update_response response='$UPDATE_RESPONSE'" + +# --- response validation --- +if [[ -z "$UPDATE_RESPONSE" ]]; then + log "event=error reason=empty_update_response" + exit 1 +fi + +# Dynu success responses: +# good → updated +# nochg → already current +if [[ "$UPDATE_RESPONSE" == good* || "$UPDATE_RESPONSE" == nochg* ]]; then + TMP_FILE=$(mktemp -p "$STATE_DIR" last_ip.XXXXXX) + + echo "$CURRENT_IP" > "$TMP_FILE" + mv "$TMP_FILE" "$LAST_IP_FILE" + + log "event=state_updated ip=$CURRENT_IP" +else + log "event=update_failed response='$UPDATE_RESPONSE'" + exit 1 +fi \ No newline at end of file diff --git a/scripts/dynu/run-once.sh b/scripts/dynu/run-once.sh new file mode 100755 index 0000000..749502b --- /dev/null +++ b/scripts/dynu/run-once.sh @@ -0,0 +1,31 @@ +# Bootstrap: this copies the scripts to the correct locations and enables the services +set -euo pipefail + +# Flow summary: +# copy files +# daemon-reload +# edit config +# enable + start timer +# done + +sudo mkdir -p /etc/conf.d +sudo install -m 755 ip-monitor.sh /usr/local/bin/ip-monitor.sh +sudo cp dynu.service /etc/systemd/system/dynu.service +sudo cp dynu.timer /etc/systemd/system/dynu.timer +sudo cp dynu-environment /etc/conf.d/dynu-environment + +sudo systemctl daemon-reload + +echo "Configure Dynu credentials..." +sudo ${EDITOR:-vi} /etc/conf.d/dynu-environment + +sudo chmod 600 /etc/conf.d/dynu-environment +sudo chown root:root /etc/conf.d/dynu-environment + + +sudo systemctl enable dynu.timer +sudo systemctl start dynu.timer + +echo "Done!" +echo "Last IP: " +echo "Use: journalctl -u dynu.service -f"