Code Monkey home page Code Monkey logo

aliyun-ddns's Introduction

aliyun-ddns

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

没有 npm 依赖, 用到的原生 Node.js 模块有:

  • http
  • url
  • crypto

使用场景

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

使用前提

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

服务器端程序部署

  1. 复制 config-sample.json 并命名为 config.json
  2. 修改 config.json 中的内容, 参数说明见下面
  3. 用 pm2 或其他方式启动 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. 参照 client.sh 写个 shell script
  2. 让脚本可运行: chmod 775 client.sh
  3. 编辑 crontab: crontab -e
  4. 添加记录, 让脚本 5 分钟调用一次: 0/5 * * * * /path/to/client.sh
  5. 重启 cron 服务: sudo service cron restart

安全事项

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

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.