So recently I setup myself a little web server at home so I could mess around with it and have a place where I could store and later show the projects to friends and/or clients. But I was facing a problem with my little idea: I have a dynamic IP at home. So I started looking around for solutions and I remembered the DynDNS and that they had a free version of their dynamic dns plan (sadly that is not the case anymore). So I looked for an alternative and found the NoIP. They have a free plan which gives you an ability to pick your hostname with one of their free domains (ex yourname.ddns.net).
This was a great find but unfortunately for me it had an clause in their free plan that meant I had to click on a link in my email and go trough the verification process to confirm that I'm still using my hostname and that it shouldn't be removed. I wasn't able to automate this process as it had several steps in it and a google recaptcha to make sure I wasn't using a script or a bot to do this for me.
So I started looking around for other options while keeping the NoIP hostname alive to allow me to access the server from the outside world. One other alternative I found was using my friends servers and domain to establish a ssh tunnel via autossh and have a port forwarding setup so that I could access it. This was also nice but it had some downsides to it. One was that I had to use my friends domain and ports that he would allow me to use so that they don't conflict with ports he is using.
So again we set it all up and I used it for a while to see how things would go and everything was nice (I still use some parts of it, for security) but for the most part I found an alternative. I remembered that my own domain was being routed trough the CloudFlare and that I saw links to some API documentation. So I logged in to my account and found the documentation and started to read it. Quickly found the documentation for the dns zones and how to get a list of them and how to update a specific zone.
Now came the fun part
I opened up nano (don't judge me :) ) on my server and started writing and googling on how to do stuff in shell/bash (not much experience before this). One of the first things I had to lookup was how to get my public IP and store it to a variable.
A big help in this was a nixCraft site and a thank you to @nixCraft for writing such awesome and easy to understand tutorials you are the RMVP :)
After that I started to look around for ways to get the information from CloudFlare and used Postman and its awesome feature to generate code from your request. I stored the output of the call into a variable and than used jq to parse the JSON output into 2 other variables. One was for storing the result from the success response and the other one was for storing the error message.
After that I looked up how to send email with the shell and looked at mail command. It was nice but in order to send it to the real world I needed something else (or at least I didn't know how to configure it to do this). So I settled with sSMTP which allowed me to send emails to myself if there was a problem with updating the DNS record.
You can checkout the full source code on my github:
gigili / Cloudflare-dns-update
Allows me to update my CloudFlare dns record so that I can have access to my server with a dynamic IP
Cloudflare dns update
Allows me to update my Cloudflare dns record so that I can have access to my server with a dynamic IP
Since I have a dynamic IP at home, but have setup myself with a little web server I needed a way to have nice domain to access it and not worry about the IP changes. There are many great options out there such as ddns or noip, but why pay for something or go trough the trouble of confirming your account should you choose the free version when you can try and learn something new and create your own script that would mimic the actions of those clients :)
Also the good thing about this is that you can use it for any of your domains / sub domains hosted trough Cloudflare and not having to pick one of those free domains.
Requirements:
- Basic knowledge ofโฆ
Top comments (4)
I too wrote a cloudflare DNS updater: github.com/mark-wagner/cfdc
Please note that IPv6 addresses use the AAAA DNS Record type.
Thank you for pointing that out. This was made for IPv4 but I was thinking of making it a bit more dynamic instead of having hardcoded values
Change Cloduflare to Cloudflare