Archive for ‘ December, 2015

Cloudflare updater script

Today I’m going to bring to you a simple Php script to update a DNS entry for a domain that is under Cloudflare; this way Cloudflare can became a sort of Dynamic DNS service like No-IP or DynDNS, with the difference that you can use your own domain/subdomain (for example home.example.org). Unlike other services Cloudflare has a free plan and also a nice set of API that allows you to easily change ip (and other things).

If you need an explanation of what is a Dynamic DNS I’ll leave here the wikipedia page ( https://en.wikipedia.org/wiki/Dynamic_DNS ) but to make things short it’s a way to access a machine (a server, NAS or simply a Raspberry Pi) connected to the Internet without knowing the IP, and this IP changes over time so you can’t just add it do a static DNS entry.

After you created an account on Cloudflare and added you domain (I’m skipping this part because their tutorials are way better 😛 ) the first thing we need to do is to obtain an API key. First step, from the top menu click on the email and click on “My Setting”.

Cattura

 

Then scroll all the way down until you see the API Key section and click on “View API key”.

Cattura2

Finally a popup will show, remember that the API key is like a password so it should never be known by other people (from the previous page you can regenerate another one if needed).Cattura3

Now that we have an API key we can start modifing the script:

  • line 8: you can change the default timezone (here’s a list of supported timezones http://php.net/manual/en/timezones.php)
  • line 11: $apiKey this is the key we just obtained
  • line 12: $myDomain the domain that we want to update (for example: example.com); this domain needs to be configured on Cloudflare panel
  • line 13: $emailAddress the email address of your account
  • line 14: $ddnsAddresses an array that contains the subdomains that needs to be updated with the current ip of the machine that is executing the script (for example home.example.com)
  • line 17: a simple webservice that returns the current ip of the machine without any markup (feel free to use the default one)
  • line 20: the url of the API endpoint, this should not be changed unless Cloudflare changes it
  • line 23: the path of the log where you can find the updates of the IP (needs to be writable so give 777 or other permissions)
  • line 24: the path of the error log (needs to be writable so give 777 or other permissions)

This script needs only Php to be installed on the server/nas/whatever and should be added in crontab to execute every few minutes (for example every five minutes). The script automatically checks if the IP has changed and if there’s no connection so there shouldn’t any problems with API usage.

Feel free to contact me on my email or by posting a comment in the box below if you encounter any problems, I’ll try to respond to all the comments/emails.

return top