Forord
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 . 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 .
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:
- The script retrieves a list of zones from the configuration file and loops through them
- In each zone, the script loops through each A or AAAA DNS record and checks the Public IP against the record
- If the IP is different — it changes it; if not, it skips the loop iteration and moves to the next
- 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 .
But just in case, I will describe it in general terms in Russian:
- Make sure you have python3 and python3-pip installed; if not – install them (in Windows, python3-pip is installed with Python)
- Clone or download the repository
- Install the required dependencies.
python3 -m pip install -r requirements.txt - Run the installation script
For Linux:chmod +x install.sh sudo ./install.shFor Windows: windows_install.bat
- Edit the configuration file
For Linux:sudoedit /etc/zen-cf-ddns.confFor 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:
- En bruker opprettes for å kjøre demonen, uten opprettelse av hjemmekatalog og mulighet for innlogging.
sudo useradd -r -s /bin/false zen-cf-ddns - En fil opprettes for å skrive logger til /var/log/
- Vi gjør den nylig opprettede brukeren til eier av loggfila
- Filene kopieres til sine plasser (konfigurasjon til /etc, kjørbar fil til /usr/bin, tjenestefil til /lib/systemd/system)
- Tjenesten aktiveres
windows_install.bat for Windows:
- Kopierer den kjørbare filen og konfigurasjonsfilen til den angitte brukermappen
- 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.exeDermed er installasjonen og konfigurasjonen fullført, bruk den med glede.
For de som ønsker å se ikke den peneste Python-koden, her er den .
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
