首先,cloudflare虽然没说支持ddns,官网也没相关的配置文档和说明。但是其实它是支持ddns的,csdn上面有很多文章,它们的核心思想都是使用cloudflare提供的api进行更新域名ip。比如:
利用CloudFlare自动DDNS - WhatZ - 博客园https://www.cnblogs.com/whatzblog/p/13335269.html利用CloudFlare Api实现DDNS-YuNi Bloghttps://ayw.ink/3057.html 但是找了一圈,都没有找到有人使用ddclient来进行ddns的配置。实测发现,ddclient也是支持cloudflare的。并且因为ddclient是专用于ddns的工具,一来更专业,二来它支持多个平台(比如namecheap、dyndns等等),就可以实现多平台统一管理和批量更新,非常方便。
ddclient工具,它是一个用perl写的脚本,可以直接用记事本软件打开修改,所以各个设备系统里面的ddclient工具的版本不尽相同。即便版本号相同,代码也不一定完全一样。所以我这边上传了一个可用的ddclient工具,传送门。这个版本是从ubnt里面拿出来的,里面含nic_cloudflare_update函数,所以它是支持cloudflare的。
使用如下配置,即可实现cloudflare的ddns更新:
#cat ddclient_pppoe0.conf server=api.cloudflare.com/client/v4,protocol=cloudflare max-interval=28d zone=XXXXX.eu.org login=XXXXXX@163.com password='f952ebac4aa__6558b81dfe84bcb8' XXXXX.eu.org
上面的`server=...`开始的部分,就是配置内容。
这里的server、max-interval是固定值;
zone是需要更新的域名;
login是cloudflare的登录邮箱;
password是cloudflare的全局 Token:
进入 Overview
界面,可以在右下角找到 Get your API token
的链接
点击 Global API Key
的 View
输入密码和验证码,即可获得 你的Token
因为我的设备是ubnt EdgeRouter X,它是用vyatta来配置ddclient的,并非直接手动配置的ddclient。所以这里再额外介绍一下,如何在edgerouterX中设置。
步骤其实也非常简单:
第一步:点config_tree;
第二步:在dns->dynamic->interface->pppoe0->service下面创建菜单,可以取名cloudflare;
第三步:添加配置。注意这里必须要在options里添加zone参数;其他参数怎么填可以看2.1节;
root@ubnt:/usr/sbin# /usr/sbin/ddclient-ubnt -file /etc/ddclient/ddclient_pppoe0.conf -debug -foreground -verbose DEBUG: get_ip: using if, pppoe0 reports 171.88.175.XX INFO: forcing updating XXXX.eu.org because no cached entry exists. DEBUG: DEBUG: nic_cloudflare_update ------------------- INFO: setting IP address to 171.88.175.XX for XXXX.eu.org UPDATE: updating XXXX.eu.org DEBUG: proxy = DEBUG: url = https://api.cloudflare.com/client/v4/zones?name=XXXX.eu.org DEBUG: server = api.cloudflare.com CONNECT: api.cloudflare.com CONNECTED: using SSL SENDING: GET /client/v4/zones?name=XXXX.eu.org HTTP/1.0 SENDING: Host: api.cloudflare.com SENDING: User-Agent: ddclient-ubnt/3.8.3 SENDING: Connection: close SENDING: X-Auth-Email: w__heng@163.com SENDING: X-Auth-Key: f9752ebac4aa7_a6558b81dfe84bcb8 SENDING: Content-Type: application/json SENDING: RECEIVE: HTTP/1.1 200 OK RECEIVE: Date: Mon, 19 Feb 2024 14:28:10 GMT RECEIVE: Content-Type: application/json RECEIVE: Connection: close RECEIVE: CF-Ray: 857f31d98e7_9dc-SJC RECEIVE: CF-Cache-Status: DYNAMIC RECEIVE: Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 RECEIVE: Expires: Sun, 25 Jan 1981 05:00:00 GMT RECEIVE: Set-Cookie: __cflb=0H28vgHxwvgAQtjU_FqYFDiSDreGJnUp7iBHUiqu5X; SameSite=Lax; path=/; expires=Mon, 19-Feb-24 16:58:11 GMT; HttpOnly RECEIVE: Strict-Transport-Security: max-age= RECEIVE: Pragma: no-cache RECEIVE: X-Content-Type-Options: nosniff RECEIVE: X-Frame-Options: SAMEORIGIN RECEIVE: Set-Cookie: __cfruid=0bdb04c42e64adce_329ece-; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None RECEIVE: Server: cloudflare RECEIVE: RECEIVE: {"result":[{"id":"0c03eaca_2c0384e_db8bee3701","name":"XXXX.eu.org","status":"active","paused":false,"type":"full","development_mode":0,"name_servers":["louis.ns.cloudflare.com","ullis.ns.cloudflare.com"],"original_name_servers":["rene.ns.cloudflare.com","katelyn.ns.cloudflare.com"],"original_registrar":null,"original_dnshost":null,"modified_on":"2024-02-18T22:11:25.Z","created_on":"2024-02-18T10:05:04.Z","activated_on":"2024-02-18T22:11:25.Z","meta":{"step":2,"custom_certificate_quota":0,"page_rule_quota":3,"phishing_detected":false,"multiple_railguns_allowed":false},"owner":{"id":null,"type":"user","email":null},"account":{"id":"0469c6c_3903b08151aad350a4d56","name":"W._heng@163.com's Account"},"tenant":{"id":null,"name":null},"tenant_unit":{"id":null},"permissions":["#lb:edit","#lb:read","#waf:read","#waf:edit","#organization:read","#organization:edit","#vectorize:read","#vectorize:edit","#query_cache:read","#query_cache:edit","#integration:edit","#access:read","#access:edit","#integration:read","#integration:install","#waitingroom:read","#waitingroom:edit","#magic:read","#magic:edit","#dex:read","#analytics:read","#dex:edit","#zone_settings:read","#zone_settings:edit","#dns_records:read","#dns_records:edit","#worker:edit","#zone_versioning:read","#zone_versioning:edit","#ssl:edit","#zaraz:publish","#ssl:read","#worker:read","#logs:edit","#billing:read","#fbm:edit","#fbm:read","#fbm_acc:edit","#logs:read","#http_applications:read","#http_applications:edit","#blocks:read","#blocks:edit","#api_gateway:read","#api_gateway:edit","#zaraz:edit","#zaraz:read","#stream:read","#stream:edit","#teams:read","#teams:edit","#healthchecks:read","#web3:read","#web3:edit","#r2_bucket:read","#r2_bucket:edit","#image:read","#image:edit","#healthchecks:edit","#dash_sso:edit","#dash_sso:read","#teams:pii","#page_shield:read","#page_shield:edit","#zone:edit","#zone:read","#billing:edit","#teams:report","#subscription:edit","#app:edit","#subscription:read","#cache_purge:edit","#auditlogs:read","#member:edit","#member:read","#legal:read","#legal:edit"],"plan":{"id":"0feeeeeeeeeeee_eeeeeeeeeeeeeee","name":"Free Website","price":0,"currency":"USD","frequency":"","is_subscribed":false,"can_subscribe":false,"legacy_id":"free","legacy_discount":false,"externally_managed":false}}],"result_info":{"page":1,"per_page":20,"total_pages":1,"count":1,"total_count":1},"success":true,"errors":[],"messages":[]} INFO: zone ID is 0c03eaca_2c0384e_db8bee3701 DEBUG: proxy = DEBUG: url = https://api.cloudflare.com/client/v4/zones/0c03eaca_2c0384e_db8bee3701/dns_records?type=A&name=XXXX.eu.org DEBUG: server = api.cloudflare.com CONNECT: api.cloudflare.com CONNECTED: using SSL SENDING: GET /client/v4/zones/0c03eaca_2c0384e_db8bee3701/dns_records?type=A&name=XXXX.eu.org HTTP/1.0 SENDING: Host: api.cloudflare.com SENDING: User-Agent: ddclient-ubnt/3.8.3 SENDING: Connection: close SENDING: X-Auth-Email: w__heng@163.com SENDING: X-Auth-Key: f9752ebac4aa7_a6558b81dfe84bcb8 SENDING: Content-Type: application/json SENDING: RECEIVE: HTTP/1.1 200 OK RECEIVE: Date: Mon, 19 Feb 2024 14:28:11 GMT RECEIVE: Content-Type: application/json RECEIVE: Connection: close RECEIVE: CF-Ray: 857f31e38b4ecf87-SJC RECEIVE: CF-Cache-Status: DYNAMIC RECEIVE: Set-Cookie: __cflb=0H28vgHxwvgAQtjUG_DiSDreGJnUpT7XK6Mq7cZ; SameSite=Lax; path=/; expires=Mon, 19-Feb-24 16:58:12 GMT; HttpOnly RECEIVE: Vary: Accept-Encoding RECEIVE: Set-Cookie: __cfruid=aecbf18360b338dc_3a63acedd5f-; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None RECEIVE: Server: cloudflare RECEIVE: RECEIVE: {"result":[{"id":"_744b59832de1e47c0b","zone_id":"0c03eaca_2c0384e_db8bee3701","zone_name":"XXXX.eu.org","name":"XXXX.eu.org","type":"A","content":"171.88.175.XX","proxiable":true,"proxied":false,"ttl":60,"locked":false,"meta":{"auto_added":false,"managed_by_apps":false,"managed_by_argo_tunnel":false,"source":"primary"},"comment":null,"tags":[],"created_on":"2024-02-19T01:22:19.Z","modified_on":"2024-02-19T07:50:47.Z"}],"success":true,"errors":[],"messages":[],"result_info":{"page":1,"per_page":100,"count":1,"total_count":1,"total_pages":1}} INFO: DNS record ID is _bb59832de1e47c0b DEBUG: proxy = DEBUG: url = https://api.cloudflare.com/client/v4/zones/0c03eaca_2c0384e_db8bee3701/dns_records/_bb59832de1e47c0b DEBUG: server = api.cloudflare.com CONNECT: api.cloudflare.com CONNECTED: using SSL SENDING: PATCH /client/v4/zones/0c03eaca_2c0384e_db8bee3701/dns_records/_bb59832de1e47c0b HTTP/1.0 SENDING: Host: api.cloudflare.com SENDING: User-Agent: ddclient-ubnt/3.8.3 SENDING: Connection: close SENDING: X-Auth-Email: w__heng@163.com SENDING: X-Auth-Key: f9752ebac4aa7_a6558b81dfe84bcb8 SENDING: Content-Type: application/json SENDING: Content-Length: 27 SENDING: SENDING: {"content":"171.88.175.XX"} RECEIVE: HTTP/1.1 200 OK RECEIVE: Date: Mon, 19 Feb 2024 14:28:12 GMT RECEIVE: Content-Type: application/json RECEIVE: Connection: close RECEIVE: CF-Ray: 857f31ebba7f7af1-SJC RECEIVE: CF-Cache-Status: DYNAMIC RECEIVE: Set-Cookie: __cflb=0H28vgHxwvgAQtjU_iSDreG_pcpCKuBKit5; SameSite=Lax; path=/; expires=Mon, 19-Feb-24 16:58:13 GMT; HttpOnly RECEIVE: Vary: Accept-Encoding RECEIVE: Set-Cookie: __cfruid=aefe83f92_6c73c09e5beb-; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None RECEIVE: Server: cloudflare RECEIVE: RECEIVE: {"result":{"id":"_bb59832de1e47c0b","zone_id":"0c03eaca_2c0384e_db8bee3701","zone_name":"XXXX.eu.org","name":"XXXX.eu.org","type":"A","content":"171.88.175.XX","proxiable":true,"proxied":false,"ttl":60,"locked":false,"meta":{"auto_added":false,"managed_by_apps":false,"managed_by_argo_tunnel":false},"comment":null,"tags":[],"created_on":"2024-02-19T01:22:19.Z","modified_on":"2024-02-19T07:50:47.Z"},"success":true,"errors":[],"messages":[]} SUCCESS: XXXX.eu.org -- Updated Successfully to 171.88.175.XX
(里面关键信息已经使用下划线打码。)
可以看到上面大致是3个步骤:
1. 使用GET /client/v4/zones?name=XXX获取账户的个人信息。主要是提取zone_id;
2. 使用GET /client/v4/zones/0c03eaca_2c0384e_db8bee3701/dns_records?tyXXX获取dns_id;
3. 使用PATCH /client/v4/zones/0c03eaca_2c0384e_db8bee3701/dnsXXXX更新该dns的ip;