[
  {
    "path": ".gitattributes",
    "content": "# Auto detect text files and perform LF normalization\n* text=auto\n"
  },
  {
    "path": "README-EN.md",
    "content": "# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution \n\n### update[ServerChan version](https://github.com/wherelse/cloudFlare-ddns-script/tree/ServerPush)\n\nEnglish | [中文](/README.md)\n\n### Overview\nThis script is a DDNS script based on the cloudflare API, which supports IPV4 and IPV6. The IP address of the script installation host can be obtained through the network and local methods. Theoretically supports all hosts using the Linux system. It has been tested and available on debian and ubuntu.\n\n### What to do before using a script\n1. A liunx device that can be connected to the Internet.\n2. Have a domain name.\n3. Register a CloudFlare account ( www.cloudflare.com ), add the domain to the account. After the configuration is complete, add an AAAA resolution to the IPV6 address of the service device as required, and set it to perform DNS resolution only.\n4. Query the Globe API Key of CloudFlare account and record it for subsequent configuration.\n\n### Instructions\nOpen a terminal window and execute the following procedure:\n```shell\nwget https://raw.githubusercontent.com/wherelse/cloudflare-ddns-script/master/cloudflare-ddns.sh\nsudo chmod +x /home/username/cloudflare-ddns.sh #Directory changes based on actual users, you should change the username.\n```\nThe personal configuration information in the script needs to be changed, and the directory is consistent with the previous command\n```shell\nsudo nano /home/username/cloudflare-ddns.sh\n#or\nsudo vi /home/username/cloudflare-ddns.sh\n```\nFind the following to make changes\n```shell\nauth_email=\"xxxxxxx@xxxx.com\"  #Your CloudFlare account email\nauth_key=\"*****************\"   #Yout cloudflare account Globel ID \nzone_name=\"Your main Domain\"   #Your zone domain name\nrecord_name=\"Your Full Domain\" #Your full record name \n\nip_index=\"local\"   #Domain acquisition method, local or network         \n#use \"internet\" or \"local\",Use local or network to obtain the address\neth_card=\"eth0\"    \n#The network card bound when using the local acquisition method, the network method can be used without change.         \n#Get the network card bound by ip in local mode, default is eth0, only local mode is effective\n```\nTake any domain name as an example, the domain name ipv6.google.com, the zone_name is `google.com` and the record_name is` ipv6.google.com`. After modification, save and exit.\n\nEnter the following at the terminal to run the script:\n```shell\nbash /home/username/cloudflare-ddns.sh\n```\nIf it says `IP changed to: xxxxx` or` IP has not changed.`, the configuration is successful.\n\n#### Schedule script\nIn order to achieve dynamic domain name resolution, the script must be kept running to obtain the IP status. Here the system crontab is used for timing.\nEnter the `crontab -e` at the terminal,Add the following at the end of the file:\n```shell\n*/5 * * * *  /home/username/cloudflare-ddns.sh >/dev/null 2>&1\n```\nSave and exit after making changes.Set the script here to execute the `cloudflare-ddns.sh` script every five minutes to achieve dynamic domain name resolution.\n\n### FAQ\nWhen the error log is:\n`API UPDATE FAILED. DUMPING RESULTS:`\n`{\"success\":false,\"errors\":[{\"code\":7001,\"message\":\"Method PUT not available for that URI.\"}],\"messages\":[],\"result\":null}`\nDelete the `cloudflare.ids` file in the script running directory, and then try to run again.\n"
  },
  {
    "path": "README.md",
    "content": "# CloudFlare DDNS script/Raspberry pi IPV6 DDNS Solution \n\n#### cloudflare ddns 脚本/树莓派IPV6 DDNS解决方案\n\n中文 | [English](/README-EN.md)\n\n## 更新[Server酱支持版本](https://github.com/wherelse/Raspberrypi-IPV6-DDNS-Solution/tree/ServerPush) \n\n### 概述\n本脚本是基于cloudflare API的DDNS脚本，支持IPV4和IPV6，可通过网络方式和本地方式获取脚本安装主机的IP地址，理论支持所有使用linux系统的主机，已在debian和ubuntu上测试可用。\n\n### 使用脚本前需要做的\n1. 一台可联网的liunx设备\n2. 拥有一个域名，免费的或者收费的都可以（中国大陆的域名需要备案）\n3. 注册一个CloudFlare账户 ( www.cloudflare.com ), 并将需要使用的域名添加到账户上，完成配置后根据需要添加服务设备的IPV6地址添加一个AAAA解析，并设为仅进行DNS解析\n4. 查询CloudFlare账户的Globel API Key并记录下来，用于后续配置\n\n### 使用方法\n打开命令窗口，执行以下程序：\n```shell\nwget https://raw.githubusercontent.com/wherelse/cloudflare-ddns-script/master/cloudflare-ddns.sh\nsudo chmod +x /home/username/cloudflare-ddns.sh #目录根据实际用户等进行更改\n```\n需要对脚本内的个人配置信息进行更改，目录和上一条命令保持一致\n```shell\nsudo nano /home/username/cloudflare-ddns.sh\n#或\nsudo vi /home/username/cloudflare-ddns.sh\n```\n找到如下内容进行更改\n```shell\nauth_email=\"xxxxxxx@xxxx.com\"  #你的CloudFlare注册账户邮箱\nauth_key=\"*****************\"   #你的cloudflare账户Globel ID \nzone_name=\"Your main Domain\"   #你的域名\nrecord_name=\"Your Full Domain\" #完整域名\n\nip_index=\"local\"   #域名获取方式，本地或者网络         \n#use \"internet\" or \"local\",使用本地方式还是网络方式获取地址\neth_card=\"eth0\"    #使用本地获取方式时绑定的网卡，使用网络方式可不更改         \n#使用本地方式获取ip绑定的网卡，默认为eth0，仅本地方式有效\n```\n以任意一个域名为例，ipv6.google.com 这个域名，zone_name为 `google.com` 和record_name则为 `ipv6.google.com` ，修改完成后，保存并退出。\n\n在命令行中输入以下内容运行脚本：\n```shell\nbash /home/username/cloudflare-ddns.sh\n```\n如果提示 `IP changed to: xxxxx` 或 `IP has not changed.` 则说明配置成功了\n\n**定时运行脚本**\n为了实现动态域名解析，必须让脚本保持运行以获取IP状态，这里使用系统crontab定时\n在命令行输入：`crontab -e` 后在文件最后添加以下内容\n```shell\n*/5 * * * *  /home/username/cloudflare-ddns.sh >/dev/null 2>&1\n```\n更改完成后保存并退出。\n在这里将脚本设置为每五分钟执行一次 `cloudflare-ddns.sh` 脚本，就可以实现动态域名解析了。\n\n### 结束\n该脚本不仅适用于树莓派，在其他Linux服务器上也适用，使用时都需要根据自己的实际情况更改以上配置时使用的路径\n\n### FAQ\n错误日志为以下内容时：\n`API UPDATE FAILED. DUMPING RESULTS:`\n`{\"success\":false,\"errors\":[{\"code\":7001,\"message\":\"Method PUT not available for that URI.\"}],\"messages\":[],\"result\":null}`\n删除脚本运行目录下的`cloudflare.ids`文件，然后再次尝试运行。\n"
  },
  {
    "path": "cloudflare-ddns.sh",
    "content": "#!/bin/bash\n\n# CHANGE THESE\nauth_email=\"xxxxxxx@xxxx.com\"  #你的CloudFlare注册账户邮箱,your cloudflare account email address\nauth_key=\"*****************\"   #你的cloudflare账户Globel ID ,your cloudflare Globel ID\nzone_name=\"Your main Domain\"   #你的域名,your root domain address\nrecord_name=\"Your Full Domain\" #完整域名,your full domain address\nrecord_type=\"AAAA\"             #A or AAAA,ipv4 或 ipv6解析\n\nip_index=\"local\"            #use \"internet\" or \"local\",使用本地方式还是网络方式获取地址\neth_card=\"eth0\"             #使用本地方式获取ip绑定的网卡，默认为eth0，仅本地方式有效,the default ethernet card is eth0\n\nip_file=\"ip.txt\"            #保存地址信息,save ip information in the ip.txt\nid_file=\"cloudflare.ids\"\nlog_file=\"cloudflare.log\"\n\n\nif [ $record_type = \"AAAA\" ];then\n    if [ $ip_index = \"internet\" ];then\n        ip=$(curl -6 ip.sb)\n    elif [ $ip_index = \"local\" ];then\n        if [ \"$user\" = \"root\" ];then\n            ip=$(ifconfig $eth_card | grep 'inet6' | cut -f2 | awk '{ print $2}' |  grep -v '^::1$' | grep -v '^fe80' | grep -v '^f[d|c]' | head -1)\n        else\n            ip=$(/sbin/ifconfig $eth_card | grep 'inet6' | cut -f2 | awk '{ print $2}' |  grep -v '^::1$' | grep -v '^fe80' | grep -v '^f[d|c]' | head -1)\n        fi\n    else \n        echo \"Error IP index, please input the right type\"\n        exit 0\n    fi\nelif [ $record_type = \"A\" ];then\n    if [ $ip_index = \"internet\" ];then\n        ip=$(curl -4 ip.sb)\n    elif [ $ip_index = \"local\" ];then\n        if [ \"$user\" = \"root\" ];then\n            ip=$(ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')\n        else\n            ip=$(/sbin/ifconfig $eth_card | grep 'inet'| grep -v '127.0.0.1' | grep -v 'inet6'|cut -f2 | awk '{ print $2}')\n        fi\n    else \n        echo \"Error IP index, please input the right type\"\n        exit 0\n    fi\nelse\n    echo \"Error DNS type\"\n    exit 0\nfi\n\n# 日志 log file\nlog() {\n    if [ \"$1\" ]; then\n        echo -e \"[$(date)] - $1\" >> $log_file\n    fi\n}\n\n# SCRIPT START\nlog \"Check Initiated\"\n\n#判断ip是否发生变化,check the ip had been changed?\nif [ -f $ip_file ]; then\n    old_ip=$(cat $ip_file)\n    if [ $ip == $old_ip ]; then\n        echo \"IP has not changed.\"\n        exit 0\n    fi\nfi\n\n#获取域名和授权 get the domain and authentic\nif [ -f $id_file ] && [ $(wc -l $id_file | cut -d \" \" -f 1) == 2 ]; then\n    zone_identifier=$(head -1 $id_file)\n    record_identifier=$(tail -1 $id_file)\nelse\n    zone_identifier=$(curl -s -X GET \"https://api.cloudflare.com/client/v4/zones?name=$zone_name\" \\\n        -H \"X-Auth-Email: $auth_email\" \\\n        -H \"X-Auth-Key: $auth_key\" \\\n        -H \"Content-Type: application/json\" | grep -Po '(?<=\"id\":\")[^\"]*' | head -1 )\n    record_identifier=$(curl -s -X GET \"https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=${record_type}&name=$record_name\" \\\n        -H \"X-Auth-Email: $auth_email\" \\\n        -H \"X-Auth-Key: $auth_key\" \\\n        -H \"Content-Type: application/json\"  | grep -Po '(?<=\"id\":\")[^\"]*')\n    echo \"$zone_identifier\" > $id_file\n    echo \"$record_identifier\" >> $id_file\nfi\n\n#更新DNS记录 update the dns\nupdate=$(curl -s -X PUT \"https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier\" \\\n    -H \"X-Auth-Email: $auth_email\" \\\n    -H \"X-Auth-Key: $auth_key\" \\\n    -H \"Content-Type: application/json\" \\\n    --data \"{\\\"type\\\":\\\"$record_type\\\",\\\"name\\\":\\\"$record_name\\\",\\\"content\\\":\\\"$ip\\\",\\\"ttl\\\":1,\\\"proxied\\\":false}\")\n\n\n#反馈更新情况 gave the feedback about the update statues\nif [[ $update == *\"\\\"success\\\":true\"* ]]; then\n    message=\"IP changed to: $ip\"\n    echo \"$ip\" > $ip_file\n    log \"$message\"\n    echo \"$message\"\nelse\n    message=\"API UPDATE FAILED. DUMPING RESULTS:\\n$update\"\n    log \"$message\"\n    echo -e \"$message\"\n    exit 1\nfi\n"
  }
]