#!/bin/bash

domain='t.900930.xyz'
zone='4ab5451fcf61f4ca49e7a97b0e6a9991'

email='linhongyun2020@gmail.com'
key='b5be6ca1c63dc521fbd7f1f4917ab4fd9fa5e'

res="$( curl "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records?name=${domain}" --silent -X GET -H "X-Auth-Email: ${email}" -H "X-Auth-Key: ${key}" -H "Content-Type: application/json" )"
old_ip="$( echo "${res}" | jq -Mjr '.result | .[].content' )"
id="$( echo "${res}" | jq -Mjr '.result | .[].id' )"

ip="$( curl -s ip.sb )"

[[ "${old_ip}" == "${ip}" ]] && exit 0

par='{"type":"A","name":"'${domain}'","content":"'${ip}'","ttl":1,"proxied":false}'

curl "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records/${id}" --silent -X PUT -H "Content-Type: application/json" -H "X-Auth-Email: ${email}" -H "X-Auth-Key: ${key}" -d "${par}"
