5.0 KiB
Readme
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.
This project is licensed under the GPLv3.
Installing
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:
sudo pacman -Syu
sudo pacman -S --needed python git
git clone 'https://git.yandere.cc/Anon/DynamicCloudflareDNS.git'
cd DynamicCloudflareDNS
python -m venv venv
source ./venv/bin/activate
pip install --upgrade pip
pip install -r requierments.txt
deactivate
./run.sh -h
If everything worked correctly you should see usage information for Dyanmic Cloudflare DNS.
Next, copy over the template configuration file and open it with your editor of choice (in this example I'm using nvim)
cp -vn ./default/.credentials.secret .
chmod 600 .credentials.secret
nvim .credentials.secret
Enter your website's domain in the domain_name
field.
Next you need to login to your Cloudflare account and generate the API tokens for your domain:
- From you Cloudflare dashboard, click on the domain for your site.
- 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
- Save and close the file.
- To make sure everything worked correctly, you can manually update your A record to a random IP address
./run.sh --set 1.1.1.1
- FYI 1.1.1.1 is the IP address for Cloudflares DNS servers
- Your site should become inaccessible and you should see the change reflected in the A record of your domain in your Cloudflare account.
- Run
./run.sh
to change it back. It should auto detect your web-facing IP and update the A record accordingly.
Automate DynamicCloudflareDNS
There are a myriad of ways to automate calling a script at regular intervals on Linux. For brevity I will be covering the cron way.
First off, install cron if it's not already installed:
sudo pacman -Syu
sudo pacman -S --needed cronie
sudo systemctl enable cronie
sudo systemctl start cronie
Next, instead of calling run.sh with cron, we will be calling cron.sh. This is basically a wrapper script that will call run.sh, and pipe the output to $HOME/cron/DynamicCloudflareDNS.log
so we can keep track of IP changes, as well as any errors that are generated.
To do this follow the instructions below:
- Make any changes you want to the log path in cron.sh
- Run
./cron.sh
and make sure that it generated normal output to the log path (nothing should appear in the terminal window) - If everything worked, run the following commands replacing nvim with your editor of choice:
EDITOR=nvim crontab -e
*/2 * * * * /absolute/path/to/cron.sh
- Save and exit
If you followed the steps above the cron job should run every two minutes. Tail the log file to ensure it's working correctly.
Donate
XMR: 493HynLQA4z71b3j9ZDXRNQudpdFW8GxeBGD8ahRctKn97RRurMVd35DqVqdAdjv68TcXTJWUwm6ee81ujDXGcmtKFixwQk