CloudFlare as dynamic DNS solution

Solution similar to presented here: http://forum.mikrotik.com/t/got-http-to-update-ddns-but-cant-figure-scrip-to-do-this/48373/15 but uses no external services to check IP - it’s extracted from WAN.

Enjoy:
https://gist.github.com/kiler129/3a436488ebc6bd79c233 :wink:

you are awesome thank you very much! i will try it.

kiler129: thank you so much, this helped me a lot. I took the liberty to improve it a bit, now it supports updating multiple DNS records and allows you to set whether you want each domain to be proxied by CloudFlare or not.

Thought I’d share in case someone else needs the same thing:

################# CloudFlare variables #################
:global WANInterface "pppoe";

:local CFemail "your.email@address.com";
:local CFtkn "..."; # Your CloudFlare API key comes here

:local CFrecordType "A";
:local CFzoneid "..."; # Your DNS zone ID comes here (a hash of 32 chars)

# An associative array of domain names to domainIds (32 chars each, you'll need to query CF to get them)
:local CFdomains { "my.domain.org"="..."; \
                   "my.2nd.dns.record.org"="..." };
# An associative array of domain names to whether you want to enable CF proxying or not
:local CFproxied { "my.domain.org"="true"; \
                   "my.2nd.dns.record.org"="false" };
# You'll need to have at least one domain that's proxied=false, otherwise resolve will keep getting CF's proxy IP instead of yours
:local CFunproxiedDomain "my.2nd.dns.record.org";

#########################################################################
########################  DO NOT EDIT BELOW  ############################
#########################################################################

################# Internal variables #################
:local resolvedIP "";
:global WANip "";
 
################# Resolve and set IP-variables #################
:local currentIP [/ip address get [/ip address find interface=$WANInterface ] address];
:set WANip [:pick $currentIP 0 [:find $currentIP "/"]];
:set resolvedIP [:resolve $CFunproxiedDomain];
 
######## Compare and update CF if necessary #####
:if ($resolvedIP != $WANip) do={
  :log info "cloudflare: $CFunproxiedDomain $resolvedIP => $WANip";
  :foreach domainName,domainId in=$CFdomains do={
    :local proxied ($CFproxied->$domainName);
    :local url "https://api.cloudflare.com/client/v4/zones/$CFzoneid/dns_records/$domainId";
    :local header "X-Auth-Email:$CFemail,X-Auth-Key:$CFtkn,content-type:application/json";
    :local data "{\"type\":\"$CFrecordType\",\"name\":\"$domainName\",\"content\":\"$WANip\",\"proxied\":$proxied}";
    :local result [ /tool fetch http-method=put mode=https url=$url http-header-field=$header http-data=$data output=user as-value ];
    :if ([len $result] > 1) do={
      :log info "cloudflare: $result";
    }
  };
} else={
  :log info "cloudflare: no update needed"
};