-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare-ddns
86 lines (71 loc) · 3.94 KB
/
cloudflare-ddns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# This is a Dynamic DNS script to read and update cloudflare entries via API based on IPs from this device
# This script does the following:
# 1. Calls Cloudlfare API for the current domain of the one specified in the variables below
# 2. Iterates over the two arrays to properly send updates via API to update cloudflare entries if
# the current IP is different than the pulled IP in step 1.
#
# This script depends on the JSON Parser from https://github.com/Winand/mikrotik-json-parser
#
######################## Edit the following variables with your information#############
# Enter interface to get public IP to watch
:global WANInterface "WAN";
# Enter email and global API key below
:local CFemail "emailaddress";
:local CFtkn "globalAPIkey";
# Enter Zone ID below, you can use the Cloudflare API guide
# RECOMMENDED: Use the included cloudflare-api.py to get your domain IDs
# Can also be found on the main domain page under "API: Zone ID" on the right column in the Cloudfalre Dashboard
:local CFrecordType "A";
:local CFzoneid "domainZoneID";
# An associative array of domain names to domainIds (32 chars each, you'll need to query CF to get them)
# Use the included cloudflare-api.py to get your IDs
# Keep formatting consistent (quotes and semi-colons. Make sure to end a line preceding a new one with a \
:local CFdomains { "domain.com"="recordID"; \
"sub.domain.com"="recordID"; \
"sub2.domain.com"="recordID" };
# An associative array of domain names to whether you want to enable CF proxying or not
:local CFproxied { "domain.com"="true"; \
"sub.domain.com"="true"; \
"sub2.domain.com"="false" };
# Define the variable below with one of your domains and domain record IDs from CFdomains above
:local domainCheck "domain.com";
:local domainIdCheck "recordID";
#########################################################################
######################## DO NOT EDIT BELOW ################################
#########################################################################
######################## Load in JSON Parser #################################
/system/script run JParseFunctions
:global JSONLoad;
:global JSONLoads;
:global JSONUnload;
:global JSONIn;
######################## Get Current IP and set IP variables ########################
:local currentIP [/ip address get [/ip address find interface=$WANInterface ] address];
:local WANip [:pick $currentIP 0 [:find $currentIP "/"]];
######################## Get current record from CloudFlare via API call ###############
:local checkURL "https://api.cloudflare.com/client/v4/zones/$CFzoneid/dns_records/$domainIdCheck";
:local checkHeader "X-Auth-Email:$CFemail,X-Auth-Key:$CFtkn,content-type:application/json";
/tool fetch dst-path=cf-check.txt http-method=get mode=https url=$checkURL http-header-field=$checkHeader http-data=$data
:local checkedIP ([$JSONLoad "cf-check.txt"]->"result"->"content")
:log info "cloudflare: checked IP: $checkedIP, WAN IP: $WANip"
####################### Compare and update Cloudflare if necessary #################
:if (($checkedIP != $WANip) and ([len $domainIdCheck] > 1)) do={
:log info "cloudflare: Checked IP is different from WAN IP, updating";
: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\",\"ttl\":\"1\",\"proxied\":$proxied}";
:local result [/tool fetch output=user http-method=put mode=https url=$url http-header-field=$header http-data=$data];
:if ([len $data] > 1) do={
:log info "Updated $domainName";
}
};
:log info "cloudflare: update complete"
} else={
:log info "cloudflare: no update needed"
};
# unload JSON Parser
$JSONUnload
# delete cf-check.txt
/file/remove cf-check.txt