Din dynamiske DNS med CloudFlare

Forord

Din dynamiske DNS med CloudFlare For personal needs, I set up VSphere at home, where I run a virtual router and an Ubuntu server as a media server along with a bunch of other goodies, and this server needs to be accessible from the Internet. But the problem is that my provider charges for static IPs, which I could always find more useful applications for. So I used the combination of ddclient + cloudflare.

Everything was fine until ddclient stopped working. After poking around, I realized it was time for hacks and workarounds, as I was spending too much time searching for the problem. In the end, it all turned into a small daemon that just works, and that’s all I needed.
For those interested – welcome under the cut.

Tools used and how "it" works

So the first thing I did was check the cloudflare site for everything I needed to know about API. I had already started to implement everything in Python (after getting acquainted with Python, I increasingly use it for simple tasks or when I need to quickly create a prototype), when suddenly I stumbled upon a nearly complete implementation.
Overall, the base was taken from the wrapper python-cloudflare.

I took one of the examples for updating DNS and added support for 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 retrieves a list of zones from the configuration file and loops through them
  2. In each zone, the script loops through each A or AAAA DNS record and checks the Public IP against the record
  3. If the IP is different — it changes it; if not, it skips the loop iteration and moves to the next
  4. Sleeps for the time specified in the config

Installasjon og konfigurasjon

Probably it would be possible to create a .deb package, but I'm not strong in that, and it’s not that complicated anyway.
I described the process quite thoroughly 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 (in 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.

    Dette er en vanlig JSON-fil, med enkle innstillinger – jeg har spesifikt beskrevet to forskjellige soner som eksempel.

Hva skjuler seg bak installatørene?

install.sh for Linux:

  1. En bruker opprettes for å kjøre demonen, uten opprettelse av hjemmekatalog og mulighet for innlogging.
    sudo useradd -r -s /bin/false zen-cf-ddns

  2. En fil opprettes for å skrive logger til /var/log/
  3. Vi gjør den nylig opprettede brukeren til eier av loggfila
  4. Filene kopieres til sine plasser (konfigurasjon til /etc, kjørbar fil til /usr/bin, tjenestefil til /lib/systemd/system)
  5. Tjenesten aktiveres

windows_install.bat for Windows:

  1. Kopierer den kjørbare filen og konfigurasjonsfilen til den angitte brukermappen
  2. Oppretter en oppgave i planleggeren for å kjøre skriptet ved systemoppstart
    schtasks /create /tn "CloudFlare Update IP" /tr "%newLocation%" /sc onstart

Etter endring av konfigurasjonen må skriptet startes på nytt, i Linux er alt enkelt og kjent:

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 må prosessen pythonw drepes og skriptet startes på nytt (jeg er veldig lat til å skrive en tjeneste for Windows i C#):

taskkill /im pythonw.exe

Dermed er installasjonen og konfigurasjonen fullført, bruk den med glede.

For de som ønsker å se ikke den peneste Python-koden, her er den repository på GitHub.

MIT-lisens, så gjør hva du vil med dette.

P.S.: Jeg forstår at det ble litt klønete, men det løser oppgaven min perfekt.

UPD: 11.10.2019 17:37
Jeg har funnet ett problem til, og om noen kan foreslå hvordan det kan løses – vil jeg være svært takknemlig.
Problemet er at hvis man installerer avhengigheter uten sudo python -m pip install -r …, vil modulene ikke være synlige for brukeren av tjenesten, og jeg ønsker ikke å tvinge brukerne til å installerere moduler med sudo, for det er ikke riktig.
Hvordan kan man gjøre det riktig, slik at det er pent?
UPD: 11.10.2019 19:16 Problemet er løst ved bruk av venv.
Det ble gjort flere endringer. En ny utgivelse vil komme om dager.

Kilde: habr.com

Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere 🔥 Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere | ProHoster