Loading... <div class="tip share">请注意,本文编写于 1788 天前,最后修改于 1541 天前,其中某些信息可能已经过时。</div> ## 准备工作 --- 首先,你需要有个域名。 然后你得有个群辉 然后你还得注册了CF 然后你得将域名的Name Server更改为Cloudflare的解析服务器地址 ## 获取各种API秘钥 --- 在Cloudflare的域名右侧找到ZoneID记下 然后点击Get your API key 在Global API keys栏里 找到Global API key 点View查看记下 还需要获得上面要DDNS的域名的Record ID 这个用Cloudflare的API获取 用上面得到的API key和Zone ID 在终端执行一下代码 返回结果中的ID便是需要用到的域名Record ID 在官方文档中我们可以得知 使用以下命令来查询自己的Record ID 在替换信息的时候记得删除 `[ ]` ```bash curl -X GET "https://api.cloudflare.com/client/v4/zones/[YOUR-ZONE-ID]/dns_records?type=A&name=example.com&content=127.0.0.1&page=1&per_page=20&order=type&direction=desc&match=all" \ -H "X-Auth-Email: [user@example.com]" \ -H "X-Auth-Key: [YOUR-API-KEY]" \ -H "Content-Type: application/json" ``` 将上文中的"user@example.com"替换为你自己的注册邮箱 并将"YOUR-API-KEY"和YOUR-ZONE-ID替换为自己的key即可 在输入以上命令后 如果没出什么岔子的话 你会得到这样的回复,找到并记下 Record ID。 <div class="tip inlineBlock share"> ``` { "success": true, "errors": [], "messages": [], "result": [ { "id": "xxxxxxxxxxxxxxxxxxxxxxxxxx", #上面一行便是你的Record ID "type": "A", "name": "example.com", "content": "198.51.100.4", "proxiable": true, "proxied": false, "ttl": {}, "locked": false, "zone_id": "YOUR_ZONE_ID", "zone_name": "USER@example.com", "created_on": "2014-01-01T05:20:00.12345Z", "modified_on": "2014-01-01T05:20:00.12345Z", "data": {} } ] } ``` </div> ## 配置DDNS --- 使用Root权限登陆群晖 输入以下命令 下载脚本到 "/sbin" 目录中 `````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` wget https://raw.githubusercontent.com/joshuaavalon/SynologyCloudflareDDNS/master/cloudflareddns.sh -O /sbin/cloudflaredns.sh `````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` 给与执行权限 ```````````````````````````````` chmod +x /sbin/cloudflaredns.sh ```````````````````````````````` 并修改其中的CloudFlare Config配置 需要修改的已经在下文给出 <div class="tip inlineBlock share"> \# CloudFlare Config \__RECTYPE__="A" \#DNS记录类型 \__RECID__="" \#获取到的Record ID \__ZONE_ID__="" \#Zone ID \__TTL__="1" \#TTL=1时为自动 这一行不用管 \__PROXY__="ture" \#是否使用CF的代理 </div> ```bash cat >> /etc.defaults/ddns_provider.conf << 'EOF' [Cloudflare] modulepath=/sbin/cloudflaredns.sh queryurl=https://www.cloudflare.com/ EOF ``` 修改群晖的DDNS配置文件 添加Cloudflare项 可以执行以下代码添加 也可以直接编辑/etc.defaults/ddns_provider.conf文件 登陆群晖的Web>控制面板>外部访问>DDNS,然后抄下面作业就行了 <div class="tip inlineBlock info"> 服务提供商选Cloudflare 主机名称是DDNS域名 用户名是Cloudflare的注册邮箱 密码是API key </div> ## 脚本备份 --- <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-30205a172252ec6b6652eb2cc5e9a9a884" aria-expanded="true"><div class="accordion-toggle"><span style="">点我点我</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-30205a172252ec6b6652eb2cc5e9a9a884" class="collapse collapse-content"><p></p> 如果你没有富强工具的话,你大概率连不上raw.githubusercontent.com~~吃枣药丸~~,这里作为一个备份给没有富强工具的小伙伴。 ```bash #!/bin/sh # DSM Config __USERNAME__="$(echo ${@} | cut -d' ' -f1)" __PASSWORD__="$(echo ${@} | cut -d' ' -f2)" __HOSTNAME__="$(echo ${@} | cut -d' ' -f3)" __MYIP__="$(echo ${@} | cut -d' ' -f4)" # log location __LOGFILE__="/var/log/cloudflareddns.log" # CloudFlare Config __RECTYPE__="A" __RECID__="" __ZONE_ID__="" __TTL__="1" __PROXY__="true" log() { __LOGTIME__=$(date +"%b %e %T") if [ "${#}" -lt 1 ]; then false else __LOGMSG__="${1}" fi if [ "${#}" -lt 2 ]; then __LOGPRIO__=7 else __LOGPRIO__=${2} fi logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}" echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__} } __URL__="https://api.cloudflare.com/client/v4/zones/${__ZONE_ID__}/dns_records/${__RECID__}" # Update DNS record: log "Updating with ${__MYIP__}..." __RESPONSE__=$(curl -s -X PUT "${__URL__}" \ -H "X-Auth-Email: ${__USERNAME__}" \ -H "X-Auth-Key: ${__PASSWORD__}" \ -H "Content-Type: application/json" \ --data "{\"type\":\"${__RECTYPE__}\",\"name\":\"${__HOSTNAME__}\",\"content\":\"${__MYIP__}\",\"ttl\":${__TTL__},\"proxied\":${__PROXY__}}") # Strip the result element from response json __RESULT__=$(echo ${__RESPONSE__} | grep -Po '"success":\K.*?[^\\],') echo ${__RESPONSE__} case ${__RESULT__} in 'true,') __STATUS__='good' true ;; *) __STATUS__="${__RESULT__}" log "__RESPONSE__=${__RESPONSE__}" false ;; esac log "Status: ${__STATUS__}" printf "%s" "${__STATUS__}" ``` <p></p></div></div></div> 最后修改:2020 年 10 月 02 日 © 允许规范转载 赞 给点.jpg