Your own dynamic DNS with CloudFlare

foreword

Your own dynamic DNS with CloudFlare For personal needs at home, I raised VSphere, on which I run a virtual router and an Ubuntu server as a media server and a bunch of other goodies, and this server should be accessible from the Internet. But the problem is that my ISP provides static for money, which can always be put to better use. Therefore, I used a bunch of ddclient + cloudflare.

Everything was fine until ddclient stopped working. After picking it up a bit, I realized that the time had come for crutches and bicycles, since it began to take too much time to search for a problem. In the end, everything turned into a small demon that just works, but I don’t need more.
Who cares - welcome under cat.

Tools used and how "it" works

So the first thing I learned on the cloudflare website is everything you need to know about API. And I already sat down to implement everything in Python (after getting acquainted with Python, I increasingly use it for some simple tasks or when I need to quickly make a prototype), when I suddenly came across an almost ready-made implementation.
In general, the wrapper was taken as the basis python-cloudflare.

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

The logic is the following:

  1. The script receives a list of zones from the configuration file and loops through them
  2. In each zone, the script loops through each DNS record of type A or AAAA and checks the Public IP against the record
  3. If the IP is different, it changes it; if not, it skips the iteration of the loop and moves on to the next one.
  4. Sleeps for the time specified in the config

Installation and Setup

Probably it would be possible to make a .deb package, but I'm not strong in this, and it's not all that difficult.
I described the process in detail in README.md on repository page.

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

  1. Make sure you have python3 and python3-pip installed, if not, install (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 install 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 nothing complicated - I specifically described 2 different zones in it as an example.

What's behind the installers?

install.sh for Linux:

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

  2. A file is created to write the log to /var/log/
  3. Set the owner of the log file to the newly created user
  4. Files are copied to their places (config in /etc, executable file in /usr/bin, service file in /lib/systemd/system)
  5. Service is activated

windows_install.bat for Windows:

  1. Copies the executable and configuration file to a user-specified folder
  2. Creates a task in the scheduler to run a 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 everything is simple 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 re-run the script (I'm too lazy to write a service under Windows in C #):

taskkill /im pythonw.exe

This completes the installation and configuration, use it to your health.

For those who want to look at the not-so-pretty Python code, here is repository on GitHub.

MIT license, so do with it what you want.

PS: I understand that it turned out a little crutch, but it copes with its task with a bang.

UPD: 11.10.2019/17/37 XNUMX:XNUMX
I found 1 more problem, and if someone tells me how to solve it, I will be very grateful.
The problem is that if you install dependencies without sudo python -m pip install -r ..., then the modules will not be visible from the service user, and I would not want to force users to install modules under sudo, and this is not correct.
How to make it beautiful?
UPD: 11.10.2019/19/16 XNUMX:XNUMX The problem was solved using venv.
There have been several changes. The next release will be in a few days.

Source: habr.com

Add a comment