Your dynamic DNS with CloudFlare

Preface

Your dynamic DNS with CloudFlare For personal use at home, I set up VSphere, on which I run a virtual router and an Ubuntu server as a media server, along with a bunch of other nice features, and this server needs to be accessible from the Internet. However, the problem is that my provider charges for static IPs, which can always be better utilized. Therefore, I used the ddclient + cloudflare combination.

Everything was fine until ddclient stopped working. After digging into it a bit, I realized it was time for some hacks and workarounds since too much time was being spent finding the problem. In the end, it all resulted in a small daemon that just works, and that's all I needed.
For anyone interested, welcome under the cut.

Tools used and how "this" works

So first of all, I learned everything needed on the cloudflare website about API. I was already set to implement everything in Python (after getting to know Python, I increasingly use it for simple tasks or when I need to quickly make a prototype) when I suddenly stumbled upon a nearly ready implementation.
In general, the basis was taken from a wrapper python-cloudflare.

I took one of the examples for updating DNS and added the use of a configuration file and the ability to update multiple A records within a zone, and of course, an unlimited number of zones.

The logic is as follows:

  1. The script reads the list of zones from the configuration file and iterates through them in a loop.
  2. In each zone, the script loops through each DNS A or AAAA record and compares the Public IP with the record.
  3. If the IP differs, it changes it; if not, it skips the iteration and moves on to the next one.
  4. It sleeps for the time specified in the config.

Installation and setup

It might be possible to make a .deb package, but I'm not strong in that, and it's not too complicated.
I described the process in detail in the README.md on the repository page..

But just in case, I will describe it in general terms in Russian:

  1. Make sure you have python3 and python3-pip installed; if not, install them (on Windows, python3-pip is installed with Python).
  2. Clone or download the repository.
  3. Install the required dependencies.
    python3 -m pip install -r requirements.txt

  4. Run the installation script.
    For Linux:
    chmod +x install.sh
    sudo ./install.sh

    For Windows: windows_install.bat

  5. Edit the configuration file.
    For Linux:
    sudoedit /etc/zen-cf-ddns.conf

    For Windows:

    Open the zen-cf-ddns.conf file in the folder where you installed the script.

    This is a regular JSON file; the settings are not complicated – I specifically described two different zones in it as an example.

What lies behind the installers?

install.sh for Linux:

  1. A user is created to run the daemon, without creating a home directory and login capabilities.
    sudo useradd -r -s /bin/false zen-cf-ddns

  2. A file is created for logging in /var/log/
  3. We make the recently created user the owner of the log file
  4. Files are copied to their respective places (config in /etc, executable file in /usr/bin, service file in /lib/systemd/system)
  5. The service is activated

windows_install.bat for Windows:

  1. Copies the executable file and the configuration file to the folder specified by the user
  2. Creates a task in the scheduler to run the script at system startup
    schtasks /create /tn "CloudFlare Update IP" /tr "%newLocation%" /sc onstart

After changing the config, the script needs to be restarted; in Linux, this is straightforward and familiar:

sudo service zen-cf-ddns start
sudo service zen-cf-ddns stop
sudo service zen-cf-ddns restart
sudo service zen-cf-ddns status

for Windows, you will have to kill the pythonw process and restart the script (I’m too lazy to write a service for Windows in C#):

taskkill /im pythonw.exe

This concludes the installation and setup; enjoy using it.

For those who want to see not the prettiest Python code, here it is repository on GitHub.

MIT License, so feel free to do what you want with it.

P.S.: I understand it turned out a bit of a hack, but it fulfills its task with flying colors.

UPD: 10/11/2019 5:37 PM
I found one more problem, and if anyone can suggest how to solve it—I would be very grateful.
The issue is that if dependencies are installed without sudo python -m pip install -r …, the modules will not be visible under the service user, and I wouldn’t want to force users to install modules with sudo; it’s not right.
How can I do it correctly so that it looks nice?
UPD: 10/11/2019 7:16 PM The problem was resolved using venv.
There were several changes. A new release will be coming soon.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster