快捷搜索:   nginx

利用Cloudflare API+智能路由器实现域名绑定宽带变动实现固定IP DDNS

自己搭建NAS,在外面经常需要访问家庭云盘上的内容,平时是需要通过IP访问,但是如果家里IP因为各种原因变动,那就麻烦了,所以需要通过域名绑定变动的IP,这个需求可以通过CF的DNS api接口来实现,目前家庭宽带上网,一般分配是内网IP,公网IP没分配给你,可以打电话给客服让改公网IP,家里通过路由器不断监测本地公网IP更新域名A记录实现动态路由。


配置需求:


1.有公网IP的宽带一条


2.域名一个,随便什么域名都可以,比如免费的eu.org


3.cloudflare帐号,免费版就行


4.一台可以运行shell的路由器,或者家用NAS


使用方法


1、将域名添加到cloudflare,在cloudflare服务里dns服务添加一条A记录,填上你本地的公网IP。

2、使用cloudflare的API更新公网IP,需要用到cloudflare的zone id 和 key,使用api获取当前域名设置的A记录ID号。


curl -X GET "https://api.cloudflare.com/client/v4/zones/你的zoneid/dns_records?type=A&name=example.com&content=127.0.0.1&page=1&per_page=20&order=type&direction=desc&match=all"

     -H "X-Auth-Email: user@example.com"

     -H "X-Auth-Key: 你的APIKEY"

     -H "Content-Type: application/json"

运行上面命令会返回


{

  "success": true,

  "errors": [],

  "messages": [],

  "result": [

    {

      "id": "372***********************",

      "type": "A",

      "name": "example.com",

      "content": "127.0.0.1",

      "proxiable": true,

      "proxied": false,

      "ttl": 120,

      "locked": false,

      "zone_id": "你的ZONEID",

      "zone_name": "example.com",

      "created_on": "2020-01-01T05:20:00.12345Z",

      "modified_on": "2020-01-01T05:20:00.12345Z",

      "data": {}

    }

  ],

  "result_info": {

    "page": 1,

    "per_page": 20,

    "count": 1,

    "total_count": 2000

  }

}

返回id就是添加的A记录的标示符

下一步定时更新公网IP到cloudflare


#获取公网IP

a1=$(curl -s ipecho.net/plain)

 

#更新到cloudflare

curl -X PUT "https://api.cloudflare.com/client/v4/zones/你的ZONEID/dns_records/你的域名ID"

     -H "X-Auth-Email: user@example.com"

     -H "X-Auth-Key: 你的APIKEY"

     -H "Content-Type: application/json"

     --data '{"type":"A","name":"example.com","content":"'"$a1"'","ttl":120,"proxied":false}'

返回如下说明就已经成功了:


{

  "success": true,

  "errors": [],

  "messages": [],

  "result": {

    "id": "372e6**********",

    "type": "A",

    "name": "example.com",

    "content": "127.0.0.1",

    "proxiable": true,

    "proxied": false,

    "ttl": 120,

    "locked": false,

    "zone_id": "你的zoneid",

    "zone_name": "example.com",

    "created_on": "2020-01-01T05:20:00.12345Z",

    "modified_on": "2020-01-01T05:20:00.12345Z",

    "data": {}

  }

}

这样就可以通过域名访问了,然后自己在路由里进入SHELL,设置个计划任务,一分钟执行一次上面的脚本就行了


cloudflare DNS API参考:https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record



顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论