Go to file
2024-02-18 02:44:50 -08:00
default Modified example domain 2022-07-30 19:46:37 -07:00
src Removed useless class variables 2024-02-18 02:44:50 -08:00
.gitignore Initial Commit 2022-07-19 19:41:40 -07:00
LICENSE.txt Added LICENSE.txt 2022-07-30 19:45:52 -07:00
README.md Updated docs since cloudflare will no longer set an A record to 1.1.1.1 2022-10-21 14:15:11 -07:00
requierments.txt Initial Commit 2022-07-19 19:41:40 -07:00
run.sh Minor update to header 2023-04-24 20:47:41 -07:00

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 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 Dynamic 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 your 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 95.217.163.246
    • The address used in the example above is for archlinux.org
    • Your site should become inaccessible and you should see the change reflected in the A record of your domain in your Cloudflare account.
    • To see if the changes took effect in the terminal you can run ./run.sh --get-record-ids, and look for the address.
  • 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 several 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, run the following commands:

  • EDITOR=nvim crontab -e
  • */2 * * * * /absolute/path/to/run.sh >> "${HOME}/cron/DynamicCloudflareDNS.log" 2>&1
    • Modify the paths above to suit your preferances
  • 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

BTC: bc1q8w04c6xt0sm2nskkqatp372llyx8ed33at4k4g

ETH & BAT: 0x2B1C49cc1C93e5bba59a5eD4D0E0a4Da8D6Ce482

XMR: 493HynLQA4z71b3j9ZDXRNQudpdFW8GxeBGD8ahRctKn97RRurMVd35DqVqdAdjv68TcXTJWUwm6ee81ujDXGcmtKFixwQk