Dynamic Cloudflare DNS is a small python3 script that automates the process of checking and updating the DNS record of your Cloudflare account.
Most internet service providers (ISPs) dynamically assign ip addresses to residential customers that use their service. Even though this rarely ever changes once assigned, unexpected power loss to the router, or an update/restart can cause the ISP to lease a different IP, thus making your websites inaccessible. The process and cost of getting your ISP to lease a static IP to you can vary wildly between providers, and is usually overkill for self-hosters who just want to host a simple site from their home.
Cloudflare can be used to circumvent this issue with their web-based API. This script will check the current DNS record for your domain and compare it to the host's current web-facing IP address. If the two don't match it will update the DNS record accordingly.
**NOTE:** This guide will assume you already have a Cloudflare account setup as a reverse proxy to your site. This should have involved modifying the nameservers to your domain to whatever Cloudflare recommended when you set it up. If you have never self-hosted before or are confused, just search for 'setting up Cloudflare as a reverse proxy' in your browser, there are plenty of helpful guides.
To setup the python3 environment (for Arch Linux) run the following commands:
- Scroll down to the API section and select 'Get your API Token'
- This may be lableled something different if this is your first time setting up your tokens.
- Click the 'Create Token' button
- Follow the guide and enable the following permissions:
- Zone Settings:Read
- Zone:Read
- DNS:Edit
- When you are finished you should see a token value appear on the screen (do not close the screen yet).
- Paste the onscreen token into your configuration file next to 'cf_api_key'
- Next, go back to the API section of your dashboard (the section from step 2).
- Copy and paste the 'Zone ID' field into 'cf_zone_id'
- Save your configuration file.
- Next run `./run.sh`
- This will print out a bunch of information related to your domain.
- Copy and paste the A record id to 'cf_record_id'
- Once you save the config file, the script will not print out all of the information it did in the previous step. Running the bot without the 'cf_record_id' set is equivilent to running it with `./run.sh --get-record-ids`