Skip to content

Automatically update Cloudflare DNS records with your current public IP. Supports multiple records, cron automation, and secure API authentication.

Notifications You must be signed in to change notification settings

AmanTahiliani/cloudflare-dns-updater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare DNS Updater

A simple Go application that automatically updates Cloudflare DNS records with your current public IP address. Perfect for dynamic IP scenarios where you need to keep your domain pointing to your current IP.

Features

  • 🔄 Automatically detects your current public IP address
  • 📝 Updates existing DNS A records or creates new ones
  • ⚡ Supports multiple DNS records in a single run
  • 🛡️ Uses Cloudflare API with secure authentication
  • 🔧 Configurable via environment variables
  • 🚀 Lightweight and fast

Prerequisites

  • Go 1.24.0 or later
  • A Cloudflare account with API access
  • A domain managed by Cloudflare

Installation

  1. Clone or download this repository
  2. Build the application:
    go build -o cloudflare-dns-updater main.go config.go dns.go

Configuration

Environment Variables

The application uses the following environment variables:

Variable Description Required
CLOUDFLARE_EMAIL Your Cloudflare account email Yes
CLOUDFLARE_AUTH_KEY Your Cloudflare API token Yes
CLOUDFLARE_ZONE_ID The Zone ID of your domain Yes
CLOUDFLARE_RECORD_NAMES Comma-separated list of DNS record names to update Yes
CLOUDFLARE_FORCE_IP Force a specific IP address (optional) No

Getting Your Cloudflare Credentials

  1. Email: Your Cloudflare account email address
  2. Auth Key:
    • Go to Cloudflare Dashboard
    • Click "Create Token"
    • Use "Custom token" template
    • Permissions: Zone:Zone:Read, Zone:DNS:Edit
    • Zone Resources: Include specific zone or all zones
  3. Zone ID:
    • Go to your domain's overview page in Cloudflare
    • Find "Zone ID" in the right sidebar
  4. Record Names:
    • List of subdomains you want to update (e.g., subdomain.example.com,www.example.com)

Quick Start

  1. Copy the example configuration file:

    cp cloudflare_updater_util_example.sh cloudflare_updater_util.sh
  2. Edit cloudflare_updater_util.sh with your actual credentials:

    nano cloudflare_updater_util.sh
  3. Make the script executable:

    chmod +x cloudflare_updater_util.sh
  4. Run the updater:

    ./cloudflare_updater_util.sh

Automated Updates with Cron

To keep your DNS records updated automatically, set up a cron job:

  1. Open your crontab:

    crontab -e
  2. Add a line to run the updater every 5 minutes:

    */5 * * * * /path/to/your/cloudflare_updater_util.sh

    Or every 10 minutes:

    */10 * * * * /path/to/your/cloudflare_updater_util.sh
  3. Save and exit. The cron job will now run automatically.

Example Cron Schedules

  • Every 5 minutes: */5 * * * *
  • Every 10 minutes: */10 * * * *
  • Every hour: 0 * * * *
  • Every 6 hours: 0 */6 * * *

Usage Examples

Basic Usage

export CLOUDFLARE_EMAIL="your-email@example.com"
export CLOUDFLARE_AUTH_KEY="your-api-token"
export CLOUDFLARE_ZONE_ID="your-zone-id"
export CLOUDFLARE_RECORD_NAMES="home.example.com,server.example.com"
./cloudflare-dns-updater

With Forced IP

export CLOUDFLARE_EMAIL="your-email@example.com"
export CLOUDFLARE_AUTH_KEY="your-api-token"
export CLOUDFLARE_ZONE_ID="your-zone-id"
export CLOUDFLARE_RECORD_NAMES="home.example.com"
export CLOUDFLARE_FORCE_IP="192.168.1.100"
./cloudflare-dns-updater

How It Works

  1. IP Detection: The application queries api.ipify.org to get your current public IP address
  2. Record Check: For each specified DNS record, it checks if an A record already exists
  3. Update or Create:
    • If the record exists and the IP is different, it updates the record
    • If the record doesn't exist, it creates a new A record
    • If the IP is the same, no action is taken

Logging

The application provides detailed logging:

  • Configuration details (without sensitive information)
  • Cloudflare API URLs being called
  • Record existence status
  • Update/create operations
  • Error messages

Error Handling

The application handles various error scenarios:

  • Network connectivity issues
  • Invalid API credentials
  • Cloudflare API errors
  • JSON parsing errors
  • Missing environment variables

Security Notes

  • Never commit your cloudflare_updater_util.sh file with real credentials
  • Use Cloudflare API tokens instead of global API keys when possible
  • Limit API token permissions to only what's necessary
  • Consider using environment variables in production instead of hardcoded values

Troubleshooting

Common Issues

  1. "Failed to load config": Check that all required environment variables are set
  2. "Failed to check if record exists": Verify your API credentials and Zone ID
  3. "Failed to update/create DNS": Check API token permissions and record names

Debug Mode

Add logging to see detailed API responses:

export CLOUDFLARE_EMAIL="your-email@example.com"
export CLOUDFLARE_AUTH_KEY="your-api-token"
export CLOUDFLARE_ZONE_ID="your-zone-id"
export CLOUDFLARE_RECORD_NAMES="home.example.com"
go run main.go config.go dns.go

License

This project is open source. Feel free to modify and distribute as needed.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Support

If you encounter any issues, please check the troubleshooting section above or create an issue in the repository.

About

Automatically update Cloudflare DNS records with your current public IP. Supports multiple records, cron automation, and secure API authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published