Skip to content

Instantly share code, notes, and snippets.

@cachenow
Forked from icyleaf/README.md
Created June 6, 2022 10:33
Show Gist options
  • Save cachenow/fa3b6de7f87a57bf72d8972660e5acd5 to your computer and use it in GitHub Desktop.
Save cachenow/fa3b6de7f87a57bf72d8972660e5acd5 to your computer and use it in GitHub Desktop.

Revisions

  1. @icyleaf icyleaf revised this gist Sep 28, 2019. 3 changed files with 9 additions and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    ## 依赖

    - ddns-script
    - curl

    ## 教程

    4 changes: 4 additions & 0 deletions beta-cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,10 @@
    [ -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
    4 changes: 4 additions & 0 deletions cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,10 @@
    [ -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
  2. @icyleaf icyleaf revised this gist Aug 8, 2019. 2 changed files with 78 additions and 0 deletions.
    77 changes: 77 additions & 0 deletions beta-cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/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'"

    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
    1 change: 1 addition & 0 deletions cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@
    [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"

    API_URL="https://api.cloudflare.com/client/v4/"
    API_DOMAIN=$domain
    API_EMAIL=$username
    API_KEY=$password
    API_ZONE_ID="{zone_id}"
  3. @icyleaf icyleaf revised this gist Aug 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ 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: '221.218.213.144' send
    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)
    ```

  4. @icyleaf icyleaf revised this gist Aug 7, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,8 @@ 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]: cloudflare: IP update accepted by DDNS Provider
    Wed Aug 7 22:21:41 2019 user.err ddns-scripts[19525]: Forced update successful - IP: '221.218.213.144' send
    223732 : Waiting 600 seconds (Check Interval)
    ```


  5. @icyleaf icyleaf revised this gist Aug 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ API_URL="https://api.cloudflare.com/client/v4/"
    API_EMAIL=$username
    API_KEY=$password
    API_ZONE_ID="{zone_id}"
    API_DNS_RECORD_ID="{dns_record_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" \
  6. @icyleaf icyleaf created this gist Aug 7, 2019.
    35 changes: 35 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Cloudflare DDNS 更新脚本 for Openwrt/Lede

    ## 依赖

    - ddns-script

    ## 教程

    如果你安装 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]: cloudflare: IP update accepted by DDNS Provider
    ```



    32 changes: 32 additions & 0 deletions cloudflare_ddns_update.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/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'"

    API_URL="https://api.cloudflare.com/client/v4/"
    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