apiVersion: v1 kind: ConfigMap metadata: name: cloudflare-ddns-script namespace: cloudflare-ddns data: update-dns.sh: | #!/bin/sh set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo "Starting Cloudflare DDNS update..." echo "Timestamp: $(date)" # Check required environment variables if [ -z "$API_TOKEN" ]; then echo "${RED}ERROR: API_TOKEN is not set${NC}" exit 1 fi if [ -z "$ZONE_ID" ]; then echo "${RED}ERROR: ZONE_ID is not set${NC}" exit 1 fi if [ -z "$DOMAINS" ]; then echo "${RED}ERROR: DOMAINS is not set${NC}" exit 1 fi # Get current public IP address echo "Fetching current public IP address..." PUBLIC_IP=$(curl -s https://api.ipify.org) if [ -z "$PUBLIC_IP" ]; then echo "First attempt failed, trying alternative service..." PUBLIC_IP=$(curl -s https://ifconfig.me) fi if [ -z "$PUBLIC_IP" ]; then echo "${RED}ERROR: Could not determine public IP address${NC}" exit 1 fi echo "${GREEN}Current public IP: $PUBLIC_IP${NC}" # Process each domain echo "$DOMAINS" | tr ',' '\n' | while IFS= read -r DOMAIN; do # Trim whitespace DOMAIN=$(echo "$DOMAIN" | xargs) echo "" echo "Processing domain: $DOMAIN" # Get DNS record ID for the domain echo "Fetching DNS record for $DOMAIN..." RECORD_RESPONSE=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?name=$DOMAIN&type=A" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json") # Check if the request was successful SUCCESS=$(echo "$RECORD_RESPONSE" | grep -o '"success":[^,]*' | cut -d':' -f2) if [ "$SUCCESS" != "true" ]; then echo "${RED}ERROR: Failed to fetch DNS record for $DOMAIN${NC}" echo "$RECORD_RESPONSE" continue fi # Extract record ID and current IP RECORD_ID=$(echo "$RECORD_RESPONSE" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4) CURRENT_IP=$(echo "$RECORD_RESPONSE" | grep -o '"content":"[^"]*"' | head -1 | cut -d'"' -f4) if [ -z "$RECORD_ID" ]; then echo "${YELLOW}WARNING: DNS record not found for $DOMAIN. Creating new record...${NC}" # Create new DNS record CREATE_RESPONSE=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ --data "{\"type\":\"A\",\"name\":\"$DOMAIN\",\"content\":\"$PUBLIC_IP\",\"ttl\":120,\"proxied\":false}") CREATE_SUCCESS=$(echo "$CREATE_RESPONSE" | grep -o '"success":[^,]*' | cut -d':' -f2) if [ "$CREATE_SUCCESS" = "true" ]; then echo "${GREEN}Successfully created DNS record for $DOMAIN with IP $PUBLIC_IP${NC}" else echo "${RED}ERROR: Failed to create DNS record for $DOMAIN${NC}" echo "$CREATE_RESPONSE" fi continue fi echo "Current DNS IP: $CURRENT_IP" # Check if IP needs to be updated if [ "$CURRENT_IP" = "$PUBLIC_IP" ]; then echo "${GREEN}IP address is already up to date for $DOMAIN${NC}" continue fi echo "${YELLOW}Updating DNS record for $DOMAIN from $CURRENT_IP to $PUBLIC_IP${NC}" # Update DNS record UPDATE_RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ --data "{\"type\":\"A\",\"name\":\"$DOMAIN\",\"content\":\"$PUBLIC_IP\",\"ttl\":120,\"proxied\":false}") UPDATE_SUCCESS=$(echo "$UPDATE_RESPONSE" | grep -o '"success":[^,]*' | cut -d':' -f2) if [ "$UPDATE_SUCCESS" = "true" ]; then echo "${GREEN}Successfully updated DNS record for $DOMAIN to $PUBLIC_IP${NC}" else echo "${RED}ERROR: Failed to update DNS record for $DOMAIN${NC}" echo "$UPDATE_RESPONSE" fi done echo "" echo "Cloudflare DDNS update completed at $(date)"