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
- Clone or download this repository
- 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
- Email: Your Cloudflare account email address
- 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
- Zone ID:
- Go to your domain's overview page in Cloudflare
- Find "Zone ID" in the right sidebar
- Record Names:
- List of subdomains you want to update (e.g.,
subdomain.example.com,www.example.com)
- List of subdomains you want to update (e.g.,
Quick Start
-
Copy the example configuration file:
cp cloudflare_updater_util_example.sh cloudflare_updater_util.sh -
Edit
cloudflare_updater_util.shwith your actual credentials:nano cloudflare_updater_util.sh -
Make the script executable:
chmod +x cloudflare_updater_util.sh -
Run the updater:
./cloudflare_updater_util.sh
Automated Updates with Cron
To keep your DNS records updated automatically, set up a cron job:
-
Open your crontab:
crontab -e -
Add a line to run the updater every 5 minutes:
*/5 * * * * /path/to/your/cloudflare_updater_util.shOr every 10 minutes:
*/10 * * * * /path/to/your/cloudflare_updater_util.sh -
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
- IP Detection: The application queries
api.ipify.orgto get your current public IP address - Record Check: For each specified DNS record, it checks if an A record already exists
- 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.shfile 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
- "Failed to load config": Check that all required environment variables are set
- "Failed to check if record exists": Verify your API credentials and Zone ID
- "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.