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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
:local TOKEN "你的DDNS token"
:local ZONEID "你的ZONEID"
:local RECORD6ID "你的RECORD6ID"
:local RECORD6NAME "*.你的v6域名.com"
:local WANIF6 "pppoe-out1或bridge1"
:local url6 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD6ID/"
:local RECORD4ID "你的RECORD4ID"
:local RECORD4NAME "*.你的v4域名.com"
:local WANIF4 "pppoe-out1"
:local url4 "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORD4ID/"
:if ([/interface get $WANIF6 value-name=running]) do={
:local currentIP6 [/ipv6/address/get [:pick [find global interface=$WANIF6] 0 ] address]
:local IP6NEW [:pick $currentIP6 0 [:find $currentIP6 "/"]]
:log info "当前IPv6地址: $IP6NEW"
:local cfRecord [/tool fetch mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json"]
:local cfContent ($cfRecord->"data")
:local startIndex [:find $cfContent "\"content\":\""]
:local cfIP6 [:pick $cfContent ($startIndex + 11) [:find $cfContent "\"" ($startIndex + 11)]]
:log info "CF IPv6地址: $cfIP6"
:if ($IP6NEW != $cfIP6) do={
:log info "检测到 IP 变化:Cloudflare=$cfIP6, 本地=$IP6NEW"
:local cfUpdate6 [/tool fetch http-method=put mode=https url=$url6 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
http-data="{\"type\":\"AAAA\",\"name\":\"$RECORD6NAME\",\"content\":\"$IP6NEW\",\"ttl\":120,\"proxied\":false}"]
:log info "已更新 Cloudflare IPv6 为 $IP6NEW"
} else={
:log info "当前 Cloudflare 记录 ($cfIP6) 与本机 IP 相同,无需更新。"
}
} else={
:log error "$WANIF6 接口未运行,无法获取本地 IPv6 地址"
}
:delay 300s
:if ([/interface get $WANIF4 value-name=running]) do={
:local currentIP4 [/ip/address get [/ip/address find interface=$WANIF4] address]
:local IP4NEW [:pick $currentIP4 0 [:find $currentIP4 "/"]]
:log info "当前IPv4地址: $IP4NEW"
:local cfRecord [/tool fetch mode=https url=$url4 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json"]
:local cfContent ($cfRecord->"data")
:local startIndex [:find $cfContent "\"content\":\""]
:local cfIP4 [:pick $cfContent ($startIndex + 11) [:find $cfContent "\"" ($startIndex + 11)]]
:log info "CF IPv4地址: $cfIP4"
:if ($IP4NEW != $cfIP4) do={
:log info "检测到 IP 变化:Cloudflare=$cfIP4, 本地=$IP4NEW"
:local cfUpdate4 [/tool fetch http-method=put mode=https url=$url4 check-certificate=no output=user as-value \
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
http-data="{\"type\":\"A\",\"name\":\"$RECORD4NAME\",\"content\":\"$IP4NEW\",\"ttl\":120,\"proxied\":false}"]
:log info "已更新 Cloudflare IPv4 为 $IP4NEW"
} else={
:log info "当前 Cloudflare 记录 ($cfIP4) 与本机 IP 相同,无需更新。"
}
} else={
:log error "$WANIF4 接口未运行,无法获取本地 IPv4 地址"
}
|