mmendescortes

Want to use Cloudflare DDNS on your Mikrotik?

TL;DR

Two weeks ago I bought a hAP Lite to bridge on the default ISP router as to do some advanced stuff I need to get work properly done at home, facinated with the amazing things a small RouterBoard can do and how I can use it to tweek my home office I started to think about setting up a local server with a small Raspberry Pi Zero I bought on my birthday from… Ahem! Dad’s money. *Shameless stuffing my chest like it’s something to be proud of*

As I extensively use Cloudflare services on Melvillei’s and my projects I thought it would be good to use Cloudflare’s DDNS instead of Mikrotik Cloud service which come as default on all Mikrotik RouterBoards.

Got my Global API key, got the Zone ID, curl-ed up my Zone Records and got the Record ID from the returned JSON, everything nice so far, I just need to find a working script on the Internet and use it on my RouterBoard without even understanding what’s going on at all, right?

Easy then, I just need to google it and copy-paste the best result, right? Oh, look! There are already topics about it on Mikrotik’s Forum, I should be able to get it done in no time at all!

Well, sad for me that’s not what happened.

Everything I found on the Internet was people like me trying to get it done but to no avail, questions about setting multiple headers for Cloudflare’s Authentication and setting the Content-Type to application/json where everywhere with no solution whatsover.

There was only one thought on my mind back then, “Gosh! Why is it so hard to setup a DDNS on this shit!” was like a mantra I spelled every day on my struggle to get it working…

At least this situation forced me to learn a lot about the fetch tool and while I had been clear benefits from the situation I still thank’s god I got it done before I had lose my sanity.

One of the problems was that in the LTS (6.42.12) version there was a Content-Type option but no option to setup multiple headers and in the Stable (6.44) version there were no Content-Type version but multiple headers can be set up.

So it turn out that given the nature of token based authentication it’s only possible to access Cloudflare’s API on a RouterOS 6.44 or higher given lack of multiple headers functionality on older fetch tools.

I could waste more of your time babbling about how I did it or more useless shit like that, but guess what! I won’t!

The code is pretty simple, just change the variables and you are ready to go, if you still didn’t understand how to use this script just go to my post How to use Cloudflare DDNS on a Mikrotik RouterOS.

Here is the code:


:global cfu do={\
:local cfi "THE_ZONE_ID";\
:local cfr "THE_RECORD_ID";\
:local cfe "ACCOUNT_EMAIL_ADDRESS";\
:local cfk "API_GLOBAL_KEY";\
:local cfd "THE_RECORD_DOMAIN";\
:local cfa [/ip cloud get public-address];\
:local cfp true;\
/tool fetch mode=https\
http-method=put\
url="https://api.cloudflare.com/client/v4/zones/$cfi/dns_records/$cfr"\
http-header-field="content-type:application/json,X-Auth-Email:$cfe,X-Auth-Key:$cfk"\
http-data="{\"type\":\"A\",\"name\":\"$cfd\",\"content\":\"$cfa\",\"proxied\":$cfp}"\
output=none\
}

You can also find it at my Mikrotik’s Forum post