4

OpenWRT 实现 Cloudflare 动态域名 dynamic DNS

 2 years ago
source link: https://blog.est.im/202101/stdout-002
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

OpenWRT 实现 Cloudflare 动态域名 dynamic DNS

Posted 2021-01-05 | stdout

首先得有公网 IP (废话),然后你得 OpenWRT 路由器桥接直拨。

这个方法的核心是通过 ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address' 命令得到本机 WAN 的公网 IP。

根据官方的说明

  1. 创建 API Token 。权限选 All Zones,得到 TOKEN
  2. 验证 API 是否 ok
    curl - X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
    -H "Authorization: Bearer TOKEN" \
    -H "Content-Type:application/json"
  3. 获取 :zone_identifier。找那个长得像 ID 的
    curl - X GET "https://api.cloudflare.com/client/v4/zones"\
    -H "Authorization: Bearer TOKEN" \
    -H "Content-Type:application/json"
  4. 因为脑残的 RESTful 风格 API 无法实现 upsert,先创建个DNS。得到 :identifier
    curl -svk -X POST "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records"\
    -H "Authorization: Bearer TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"type":"A","name":"my_ddns.est.im","content":"'$(ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address')'","ttl":600,"proxied":false}'
  5. 日常更新 DNS
    curl -svk -X PUT "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier" \
    -H "Authorization: Bearer TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"type":"A","name":"my_ddns.est.im","content":"'$(ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address')'","ttl":600,"proxied":false}'

感觉挺麻烦。而且没法 API 网关自动把调用者的 IP 设为 A 记录。但是聊胜于无吧。高版本的 OpenWRT /usr/lib/ddns/services
直接支持 Cloudflare 了不用这么麻烦了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK