本文主要讲解如何借助cloudflare的API接口来实现RouterOS(以下简称ROS)的DDNS动态解析。
1、创建API密钥 Global API Key
访问cloudflare后台,API令牌管理:https://dash.cloudflare.com/profile/api-tokens
1.1 token模式,用户API令牌,点击创建令牌,选择编辑区域DNS,再选择域名,生成API token令牌
1.2 API Email + API Key,使用cloudflare用户邮箱和Global API key
2、PHP脚本
接口地址
- http(s)://api.77bx.com/cfdns/
请求方式
- POST/GET
请求参数
参数名称 | 必选 | 类型 | 描述 |
---|---|---|---|
token | 是 | String | cloudflare API令牌 |
是 | String | cloudflare账号邮箱(跟token二选一) | |
key | 是 | String | cloudflare Global API Key(跟token二选一) |
domain | 是 | String | 域名。如77bx.com |
record | 否 | String | 主机记录。如不传默认为@ |
type | 否 | String | 记录类型。如不传默认为A记录 |
ip | 否 | String | IP地址。如不传默认为来源IPv4地址 |
ttl | 否 | int | 生存时间。免费版为1分钟~1天(60~86400),默认为5分钟 。 |
proxied | 否 |
String |
是否开启代理模式。填写true/false |
format | 否 | String | 输出模式。json/xml/text |
请求事例
curl -X POST http://api.77bx.com/cfdns/ -d "token=Token&domain=77bx.com&record=op&ip=1.2.3.4&type=A"
curl -X POST http://api.77bx.com/cfdns/ -d "email=admin@77bx.com&key=SecretKey&domain=77bx.com&record=op&ip=1.2.3.4&type=A"
请求输出
返回提示 | 描述 |
---|---|
0 | 表示更新成功(当前IP地址和域名IP地址相同也返回0) |
1 | 参数错误 |
2 | 域名或API密钥错误 |
3 | 主机记录或API密钥错误 |
安全说明
1、本接口未记录所有的解析token和解析记录,但是服务器的访问日志会有链接记录(按照政策要求需保存6个月的日志)。虽然有日志但是本接口不提供任何查询。
2、由于使用本接口出现的任何安全问题,本人概不负责。这边只能保证本人不会去做任何后门行为。这边建议如果使用了本接口请使用token模式。
源代码
暂时不提供
3、RouterOS脚本
3.1 Token模式脚本命令:
#PPPoE :local pppoe "pppoe-out1" #cloudflare :local token "cloudflare token" #域名 :local record "routeros" :local domain "77bx.com" #以下非专业人士请勿修改 :global ipold :local ipnew [/ip address get [/ip address find interface=$pppoe] address] :set ipnew [:pick $ipnew 0 ([len $ipnew] -3)] :if ($ipnew != $ipold ) do={ :local postdata "format=ros&token=$token&domain=$domain&record=$record&ip=$ipnew" :local response [/tool fetch http-method="post" url="http://api.77bx.com/cfdns/" http-data=$postdata as-value output=user] :if ($response->"data" = "0") do={ :log info "DDNS: changed $ipold to $ipnew" :set ipold $ipnew } }
以上脚本需要修改pppoe(宽带拨号的名称),token,record和domain
3.2 API Email + API Key模式脚本命令:
#PPPoE :local pppoe "pppoe-out1" #cloudflare :local email "cloudflare用户邮箱" :local key "cloudflare Global API Key" #域名 :local record "routeros" :local domain "77bx.com" #以下非专业人士请勿修改 :global ipold :local ipnew [/ip address get [/ip address find interface=$pppoe] address] :set ipnew [:pick $ipnew 0 ([len $ipnew] -3)] :if ($ipnew != $ipold ) do={ :local postdata "format=ros&email=$email&key=$key&domain=$domain&record=$record&ip=$ipnew" :local response [/tool fetch http-method="post" url="http://api.77bx.com/cfdns/" http-data=$postdata as-value output=user] :if ($response->"data" = "0") do={ :log info "DDNS: changed $ipold to $ipnew" :set ipold $ipnew } }
以上脚本需要修改pppoe(宽带拨号的名称),email,key,record和domain
注意:ROS可以设置定时1分钟执行一次脚本,脚本中带有ip是否相同判断,相同就不执行;然后是我做的API接口后台中也有ip是否相同的判断。
目前有 0 条评论