Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Last active December 24, 2023 11:24
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save icyleaf/8fc867003cb3c868bfae855e722ce392 to your computer and use it in GitHub Desktop.
Save icyleaf/8fc867003cb3c868bfae855e722ce392 to your computer and use it in GitHub Desktop.
Cloudflare DDNS 更新脚本 for Openwrt/Lede

Cloudflare DDNS 更新脚本 for Openwrt/Lede

依赖

  • ddns-script
  • curl

教程

如果你安装 ddns-script 在添加到供应商看到了 cloudflare 那就不需要本脚本了,如果没有看到那么接着往下看。

先把本脚本 cloudflare_ddns_update.sh 上传只 openwrt 的 /root 目录下面。

再从 Cloudflare 获取 Global API Key 备用(记作为 API_KEY),预先添加一个 A 记录,IP 随意写比如 127.0.0.1, 添加后点击看下面的 API 能够从 APIs 链接获取到 Zone id(记作为 API_ZONE_ID),然后需要通过 curl 获取 dns records 列表接口查看刚才新添加的 A 记录的 DNS Record id(记作为 API_DNS_RECORD_ID)

在 openwrt 的 ddns 添加出供应商选择 Custom,更新脚本填写 /root/cloudflare_ddns_update.sh, 域名填写你的域名,用户名填写你的注册邮箱,密码填写 API_KEY,由于下面额外的参数没有搞懂怎么用, API_ZONE_ID 和 API_DNS_RECORD_ID 获取的值需要填写在脚本对应的变量后面。填写完成后点击保存并应用。

最后点击启用并启动即可。你可以通过系统日志看到类似的日志就说明完成了!

Wed Aug  7 22:21:36 2019 user.notice ddns-scripts[19525]: cloudflare: PID '19525' started at 2019-08-07 22:21
Wed Aug  7 22:21:39 2019 daemon.err uhttpd[5445]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Wed Aug  7 22:21:39 2019 daemon.err uhttpd[5445]:                                  Dload  Upload   Total   Spent    Left  Speed
Wed Aug  7 22:21:40 2019 daemon.err uhttpd[5445]: 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   326    0   250  100    76    201     61  0:00:01  0:00:01 --:--:--   317
Wed Aug  7 22:21:41 2019 user.err ddns-scripts[19525]: Forced update successful - IP: 'xxx.xxx.xxx.xxx' send
 223732       : Waiting 600 seconds (Check Interval)
#!/bin/sh
# 参考资料:
# https://github.com/openwrt/packages/blob/master/net/ddns-scripts/samples/update_sample.sh
# https://github.com/anjianshi/cloudxns-ddns/blob/master/cloudxns.sh
#
# Requirements:
# curl
# egrep
#
# 这是更智能化的脚本版本
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
if test ! $(which curl); then
write_log 14 "Service missing curl dependency"
fi
API_URL="https://api.cloudflare.com/client/v4/"
API_DOMAIN=$domain
API_SUBDOMAIN=$domain
API_EMAIL=$username
API_KEY=$password
API_DNS_RECORD_TYPE=A
API_IP=$__IP
API_ZONE_ID=
API_DNS_RECORD_ID=
API_ENDPOINT_API="https://api.cloudflare.com/client/v4/zones"
get_domain_zone_id() {
local url="${API_ENDPOINT_API}?name=${API_DOMAIN}"
local result=$(request_get $url)
API_ZONE_ID=$(echo $result| egrep -o "\"id\":\"[0-9a-f]{32}\"" | egrep -o "[0-9a-f]{32}" | head -n1)
}
get_dns_record_id() {
local url="${API_ENDPOINT_API}/${API_ZONE_ID}/dns_records?name=${API_SUBDOMAIN}&type=${API_DNS_RECORD_TYPE}"
local result=$(request_get $url)
API_DNS_RECORD_ID=$(echo $result | egrep -o "\"id\":\"[0-9a-f]{32}\"" | egrep -o "[0-9a-f]{32}" | head -n1)
}
update_dns_record() {
local url="${API_ENDPOINT_API}/${API_ZONE_ID}/dns_records/${API_DNS_RECORD_ID}"
local data='{"type":"A","name":"'${API_SUBDOMAIN}'","content":"'${API_IP}'","ttl":120,"proxied":false})'
local result=$($request_put $url $data)
}
request_get() {
local url="$1"
curl -s -X GET $url \
-H "X-Auth-Email: ${API_EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
-H "Content-Type: application/json"
}
request_put() {
local url="$1"
local data="$2"
curl -s -X PUT $url \
-H "X-Auth-Email: ${API_EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
-H "Content-Type: application/json" \
--data ${data}
}
get_domain_zone_id
get_dns_record_id
$RESULT=$(update_dns_record)
if [ $(echo -n "$RESULT"|grep -o "\"success\":true"|wc -l) = 1 ];then
return 0
else
return 1
fi
#!/bin/sh
# 参考资料:
# https://github.com/openwrt/packages/blob/master/net/ddns-scripts/samples/update_sample.sh
# https://github.com/anjianshi/cloudxns-ddns/blob/master/cloudxns.sh
#
# Requirements:
# curl
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
if test ! $(which curl); then
write_log 14 "Service missing curl dependency"
fi
API_URL="https://api.cloudflare.com/client/v4/"
API_DOMAIN=$domain
API_EMAIL=$username
API_KEY=$password
API_ZONE_ID="{zone_id}"
API_DNS_RECORD_ID="{dns_record_id}"
RESULT=$(curl -X PUT "${API_URL}/zones/${API_ZONE_ID}/dns_records/${API_DNS_RECORD_ID}" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: ${API_EMAIL}" \
-H "X-Auth-Key: ${API_KEY}" \
--data '{"type":"A","name":"'${API_DOMAIN}'","content":"'${__IP}'","ttl":120,"proxied":false}')
write_log 7 "answered:\n$RESULT"
if [ $(echo -n "$RESULT"|grep -o "\"success\":true"|wc -l) = 1 ];then
return 0
else
return 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment