Custom Dynamic DNS with CloudFlare

Foreword



A picture to attract attention For personal needs, he raised VSphere, on which I twist a virtual router and Ubuntu server as a media server and a bunch of goodies, and this server should be accessible from the Internet. But the problem is that my provider gives statics for money, which you can always find a more useful application. Therefore, I used a bunch of ddclient + cloudflare.



Everything was fine until ddclient stopped working. After digging it a bit, I realized that the time had come for crutches and bicycles, since the time to find the problem began to take too much. In the end, everything turned into a small demon that just works, but I don’t need it anymore.

Who cares - welcome to cat.



Tools used and how β€œit” works



So the first thing I learned on the cloudflare site is everything you need to know about the API . And I was already sitting down to implement everything in Python (after getting to know Python, I use it more often for some simple tasks or when I need to quickly make a prototype), when I suddenly stumbled upon an almost ready-made implementation.

In general, the wrapper python-cloudflare was taken as a basis.



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



The logic is as follows:



  1. The script receives a list of zones from the configuration file and passes through them in a loop
  2. In each zone, the script loops through each DNS record of type A or AAAA and checks the Public IP with the record
  3. If IP is different, change it; if not, skip the loop iteration and move on to the next
  4. Falls asleep at the time specified in the config


Installation and setup



Probably it would be possible to make a .deb package, but I'm not strong at that, and it's not that complicated.

I described the process in detail in README.md on the 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 necessary 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, nothing is complicated by the settings - specifically described as an example 2 different zones in it.



What is hidden behind the installers?



install.sh for Linux:



  1. A user is created to start the daemon, without creating a home directory and login capabilities.



     sudo useradd -r -s /bin/false zen-cf-ddns
          
          





  2. A log file is created in / var / log /
  3. We make the owner of the log file of the newly created user
  4. Files are copied in 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. Copy executable file and configuration file to 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 have to kill the pythonw process and re-run the script (it is very lazy for me to write a service under Windows in C #):



 taskkill /im pythonw.exe
      
      





On this installation and configuration is completed, use your health.



For those who don't want to see the most beautiful Python code, here is a repository on GitHub .



MIT license, so do whatever you want with this good.



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



UPD: 10/10/2019 17:37

I found one 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 from under the service user the modules will not be visible, but I would not want to force users to install modules under sudo, and this is not correct.

How to make it beautiful?

UPD: 10/11/2019 19:16 The problem is solved using venv.

It turned out a few changes. The next release will be the other day.



All Articles