This project provides a Cloudflare Worker solution for managing dynamic DNS updates, described by my Tame Your Dynamic IP: A Cloudflare Worker Solution for Unifi article.
The Cloudflare Worker handles HTTP requests to update DNS records dynamically. It verifies the request, authenticates using basic authentication, and updates the DNS record with the provided IP address.
- Secure HTTPS connection requirement.
- Basic authentication for request verification.
- DNS record update using Cloudflare API.
- Error handling for various scenarios.
- Cloudflare account.
- Cloudflare API token with permissions to read and edit DNS records and read Zones. Note: For more details, see the Create a Cloudflare API Token section from my article.
-
Clone the repository:
git clone https://github.com/tdharris/cloudflare-worker-dyndns.git cd cloudflare-worker-dyndns -
Install dependencies:
npm install
Deploy the Cloudflare Worker:
npm run deployFor more details, see the Cloudflare docs Workers Getting Started.
To update a DNS record, send any HTTP request to the worker URL with the following details:
Query parameters:
hostname: The DNS record to update.ip: The new IP address.zone: The Cloudflare zone name.
Authorization header:
username: The Cloudflare API email address.password: The Cloudflare API token.
Example request:
curl -X GET "https://your-worker-url/update?hostname=dns.example.com&ip=1.2.3.4&zone=example.com" \
-H "Authorization: Basic $(echo -n 'username:password' | base64)"To configure the Unifi controller to update the DNS record automatically, see the Configure Unifi Dynamic DNS section from my article.
The worker handles various error scenarios and returns appropriate HTTP status codes and messages:
400 Bad Request: Missing or invalid query parameters.401 Unauthorized: Invalid or missing authorization.404 Not Found: Unknown paths.500 Internal Server Error: Unknown errors.
To run the tests, use the following command:
npm testContributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.