Giter Club home page Giter Club logo

aliyun-ddns's Introduction

aliyun-ddns

利用阿里云解析的 API 实现动态域名解析的功能(类似花生壳,例如定时地将自己的域名解析更新为家中当前的 IP 地址)。

没有 npm 依赖, 只用了三个原生 Node.js 的模块: http, crypto, url

目前该工具分为两种模式的版本:

  • client-mode,这种模式下只需要在客户端定时执行一个 Node.js 程序,当前地址的公网 IP 是借助访问公共的 API 来获取的
  • server-mode,这种模式需要在服务端部署一个 Node.js 程序,并且在客户端定时请求服务端以使得服务端获知当前客户端所在地址的公网 IP

client-mode 推荐使用该模式,部署更为简单,无需云服务器资源,但要求客户端有 Node.js 的执行环境。

server-mode 适合一些客户端无法安装 Node.js 环境的场景(例如路由器)

License

LICENSE

使用前提

  • 域名是由阿里云/万网托管的
  • 如果要将 IP 设置为客户端所在的公网 IP, 要确保客户端被当地 ISP 分配的不是大内网的 IP(如果是大内网的 IP, 可以给客服打电话要求更换)

Client Mode

使用场景

  • 在本地(例如家里的 NAS、树莓派等)定时执行客户端,通过公共 API 获取当前网络环境的公网 IP,然后更新 aliyun 中的 DNS 记录

客户端部署(crontab 定时调用)

  1. 复制 client-mode/config-sample.json 并命名为 client-mode/config.json
  2. 修改 client-mode/config.json 中的内容, 参数说明见下面
  3. 参照 client-mode/client.sh 写个 shell script
  4. 让脚本可运行: chmod 775 client-mode/client.sh
  5. 编辑 crontab: crontab -e
  6. 添加记录, 让脚本 5 分钟调用一次: */5 * * * * /path/to/client-mode/client.sh
  7. 重启 cron 服务

client-mode/config.json 参数说明

  • AccessKeyId 和 AccessKeySecret 在阿里云的控制台获取, 注意保密
  • hostnames 是待更新的多个 DNS (子)域名

Server Mode

使用场景

  • 部署本服务在阿里云、AWS 等公有云上
  • 在本地(例如家里的 NAS、路由器、树莓派等)设定定时任务:访问服务端,以更新当前本地的 IP 地址

服务器端程序部署

  1. 复制 server-mode/config-sample.json 并命名为 server-mode/config.json
  2. 修改 server-mode/config.json 中的内容, 参数说明见下面
  3. 用 pm2 或其他方式启动 server-mode/app.js;如果运行环境有 Docker,也可以直接 docker-compose up 来启动服务
  4. 进行必要的网路配置(譬如 Nginx 反向代理, 防火墙等), 确保应用服务能在公网中被访问到

config.json 参数说明

  • AccessKeyId 和 AccessKeySecret 在阿里云的控制台获取, 注意保密
  • clientIpHeader 属性和反向代理有关(例如在 Nginx 后面), 用于从 header 中获取客户端的 IP,如果无反响代理,可以不填
  • path 是自定义的访问路径, 默认为 /hack
  • port 是自定义的服务器端监听的端口

客户端手动调用

调用的原理和花生壳类似, 假设在 config.json 中 path 属性是默认的 /hack, 有两种调用方法:

  1. 在客户端调用 /hack?hostname=foo.bar.com 来设定 foo.bar.com 解析为当前客户端的公网 IP
  2. 在客户端调用 /hack?hostname=foo.bar.com&ip=xxx.xxx.xxx.xxx 来设定 foo.bar.com 解析为 xxx.xxx.xxx.xxx

客户端 crontab 定时调用

  1. 参照 server-mode/client.sh 写个 shell script
  2. 让脚本可运行: chmod 775 server-mode/client.sh
  3. 编辑 crontab: crontab -e
  4. 添加记录, 让脚本 5 分钟调用一次: */5 * * * * /path/to/server-mode/client.sh
  5. 重启 cron 服务

安全事项

由于在 server-mode 中,服务端暴露的 API 未加任何身份验证措施,相当于是把阿里云解析的修改、添加 API 暴露在了外界,所以一定要注意入口地址的隐藏。

aliyun-ddns's People

Contributors

yyqian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aliyun-ddns's Issues

群晖 DDNS 计划任务,请不要带{},否则修改域名IP不会生效

在下小白,这不是个问题贴,而是想通过这里帮助其他人解决一个问题:

我是在群晖NAS里,通过DDNS-aliyun的sh脚本配置一个自定义域名

按照sh aliyun.sh {AccessKeyId} {AccessKeySecret} example.com www 这个案例试了N次,结果都是脚本执行后,日志都是正常的将新IP指向我的域名,但在aliyun后台可以看到域名解析始终是没变化的,甚至删掉之后也不会新增;

遇到相似问题,把上面的{}都去掉再试试...注意保留空格;

以上,打扰了。。。

IPv6

可否添加支持AAAA记录的功能?

同一子域名有不同类型记录时更新出错

例如子域名@.example.com既有A记录又有MX记录
应该是查到了其他类型记录,将alidns.js中131行加上&& record.Type === updateParmas.Type就对了。顺便吐槽一下updateParmas的拼写 😄

小白請教

使用server-mode出錯,服務器有ipv6,抓到的地址前面有::ffff:,請教怎麼改?我是小白
Fri Nov 08 2019 15:42:22 GMT+0800 (CST): [error] {"hostname":"abc.def.com","ip":"::ffff:113.52.13.27"}

API 调用BUG

一个问题:
同一域名下:有 txt格式的@域名,也会被改成A格式的域名;

路由器 pandorabox 这个要怎么弄?

是不是必须 要求 node.js 环境才能用? 还有就是 server-mode这个模式没搞懂是怎么使用的,感觉应该用客户端就可以了吧?
为什么还要用服务端?

关于部署

请问服务端和客户端都安装在树莓派本地,可以实现动态域名解析嘛?

子域名更新的问题

使用时发现更新子域名时会出错 "Code":"IncorrectDomainUser","Message":"The domain name does not belong to this user.",下面是我的解决办法:

因为有多级子域名,所以 RR 的获取方式改为

target.hostname.split('.').slice(0, -2).join('.')

DomainName 的获取方式改为

target.hostname.split('.').slice(-2).join('.')

更新子域名时,describeParams 要使用

const describeParams = {              
  Action: 'DescribeSubDomainRecords', 
  SubDomain: target.hostname          
};                                    

讨论 Server 多余性问题

我的理解是只需要客户端调用阿里云的 API 修改 DNS 解析即可,为什么还要 Server,请帮忙解惑一下,可能我忽略了某些细节,谢谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.