Created
July 29, 2024 22:26
-
-
Save mxmauro/5889a086d718789ab7f6cf9bc0446d11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Update CloudFlare DNS record within MikroTik router | |
# (C) Mauro H. Leggieri 2024 | |
# MIT License | |
# | |
# CloudFlare settings | |
:local cfZoneID "####" | |
:local cfDnsRecordID "####" | |
:local cfApiToken "####" | |
;local subDomain "{subdomain}" | |
;local fullDomain "$subDomain.{top-level-domain}" | |
# Online services which respond with your IPv4, two for redundancy | |
:local ipDetectService1 "https://api.ipify.org/" | |
:local ipDetectService2 "https://ipv4.icanhazip.com/" | |
#------------------------------------------------------------------------------- | |
:log info message="CF-DNS-Updater: Starting" | |
# Resolve current subdomain ip address | |
:local previousIP | |
:do { | |
:set previousIP [ :resolve "$fullDomain" ] | |
} on-error={ | |
:log error "CF-DNS-Updater: Could not resolve dns name $fullDomain" | |
:error "CF-DNS-Updater: Quitting with error" | |
}; | |
# Detect our public IP adress useing special services | |
:local currentIP | |
:do { | |
:set currentIP ([/tool fetch url=$ipDetectService1 output=user as-value]->"data") | |
} on-error={ | |
:log error "CF-DNS-Updater: Service does not work: $ipDetectService1" | |
:do { | |
:set currentIP ([/tool fetch url=$ipDetectService2 output=user as-value]->"data") | |
} on-error={ | |
:log error "CF-DNS-Updater: Service does not work: $ipDetectService2" | |
:error "CF-DNS-Updater: Quitting with error" | |
}; | |
}; | |
:log info "CF-DNS-Updater: DNS IP ($previousIP), current internet IP ($currentIP)" | |
:if ($currentIP != $previousIP) do={ | |
:log info "CF-DNS-Updater: Current IP $currentIP is not equal to previous IP, update needed" | |
:log info "CF-DNS-Updater: Sending update for $fullDomain" | |
:local requestUrl "https://api.cloudflare.com/client/v4/zones/$cfZoneID/dns_records/$cfDnsRecordID" | |
:local requestHeaders "Authorization:Bearer $cfApiToken,Content-Type:application/json" | |
:local requestPayload "{\"type\":\"A\",\"name\":\"$subDomain\",\"content\":\"$currentIP\",\"ttl\":300,\"proxied\":false}" | |
:local response | |
:do { | |
:set response ([/tool fetch url="$requestUrl" mode=https http-method=put http-header-field="$requestHeaders" http-data="$requestPayload" output=user as-value]->"data") | |
} on-error={ | |
:log error "DuckDNS: could not send GET request to the DuckDNS server. Going to try again in a while." | |
:delay 5m; | |
:do { | |
:set response ([/tool fetch url="$requestUrl" mode=https http-method=put http-header-field="$requestHeaders" http-data="$requestPayload" output=user as-value]->"data") | |
} on-error={ | |
:log error "CF-DNS-Updater: Unable to send request to CloudFlare" | |
:error "CF-DNS-Updater: Quitting with error" | |
} | |
} | |
} else={ | |
:log info "CF-DNS-Updater: No need to update" | |
} | |
:log info message="CF-DNS-Updater: Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment