forked from docker-home-server/mikrotik-cloudflare-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-script
executable file
·107 lines (86 loc) · 2.7 KB
/
gen-script
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
HOST=api.cloudflare.com
if [ -z "$TOKEN" ]
then
echo TOKEN is not set. Cannot continue.
exit 2
fi
function dns_ids_v4() {
curl -s "https://$HOST/client/v4/zones/$ZONE_ID/dns_records" \
-H "Accept: application/json" \
-H "Authorization: Bearer $TOKEN" \
| jq -r ".result[] | select(.name == (\"*.$DOMAIN\", \"$DOMAIN\") and .type == \"A\") | .id + \":\" + .name"
}
function dns_ids_v6() {
curl -s "https://$HOST/client/v4/zones/$ZONE_ID/dns_records" \
-H "Accept: application/json" \
-H "Authorization: Bearer $TOKEN" \
| jq -r ".result[] | select(.name == (\"*.$DOMAIN\", \"$DOMAIN\") and .type == \"AAAA\") | .id + \":\" + .name"
}
DNS_IDS_V4=`dns_ids_v4`
if [ ! -z "$INTERFACE_V6" ]
then
DNS_IDS_V6=`dns_ids_v6`
fi
cat <<EOT
:global mcdPreviousIPv4
:local mcdCurrentIPv4
:local mcdCurrentIPv4Unparsed
:set mcdCurrentIPv4Unparsed [/ip address get [:pick [find where interface=$INTERFACE_V4] 0] address ]
:for i from=( [:len \$mcdCurrentIPv4Unparsed ] - 1) to=0 do={:if ( [:pick \$mcdCurrentIPv4Unparsed \$i] = "/") do={:set mcdCurrentIPv4 [:pick \$mcdCurrentIPv4Unparsed 0 \$i]}}
EOT
if [ ! -z "$INTERFACE_V6" ]
then
cat <<EOT
:global mcdPreviousIPv6
:local mcdCurrentIPv6
:local mcdCurrentIPv6Unparsed
:set mcdCurrentIPv6Unparsed [/ipv6 address get [:pick [find where interface=$INTERFACE_V6] 1] address ]
:for i from=( [:len \$mcdCurrentIPv6Unparsed ] - 1) to=0 do={:if ( [:pick \$mcdCurrentIPv6Unparsed \$i] = "/") do={:set mcdCurrentIPv6 [:pick \$mcdCurrentIPv6Unparsed 0 \$i]}}
EOT
fi
cat <<EOT
:if (\$mcdCurrentIPv4 != \$mcdPreviousIPv4) do={
:log info "mcd: new IPv4 \$mcdCurrentIPv4 (was \$mcdPreviousIPv4)"
:set mcdPreviousIPv4 \$mcdCurrentIPv4
EOT
for line in $DNS_IDS_V4
do
id=${line%:*}
name=${line#*:}
cat <<EOT
/tool fetch mode=https \\
http-method=put \\
url="https://$HOST/client/v4/zones/$ZONE_ID/dns_records/$id" \\
http-header-field="content-type: application/json,Authorization: Bearer $TOKEN" \\
http-data="{\"type\":\"A\",\"name\":\"$name\",\"content\":\"\$mcdCurrentIPv4\"}" \\
output=none
EOT
done
cat <<EOT
}
EOT
if [ ! -z "$INTERFACE_V6" ]
then
cat <<EOT
:if (\$mcdCurrentIPv6 != \$mcdPreviousIPv6) do={
:log info "mcd: new IPv6 \$mcdCurrentIPv6 (was \$mcdPreviousIPv6)"
:set mcdPreviousIPv6 \$mcdCurrentIPv6
EOT
for line in $DNS_IDS_V6
do
id=${line%:*}
name=${line#*:}
cat <<EOT
/tool fetch mode=https \\
http-method=put \\
url="https://$HOST/client/v4/zones/$ZONE_ID/dns_records/$id" \\
http-header-field="content-type: application/json,Authorization: Bearer $TOKEN" \\
http-data="{\"type\":\"AAAA\",\"name\":\"$name\",\"content\":\"\$mcdCurrentIPv6\"}" \\
output=none
EOT
done
cat <<EOT
}
EOT
fi