:triangular_flag_on_post: 自动更新域名解析到本机IP(支持dnspod,阿里DNS,CloudFlare,华为云,DNSCOM...)
NewFuture, updated
🕥
2023-02-20 15:44:16
自动更新 DNS 解析 到本机 IP 地址,支持 ipv4 和 ipv6 以 本地(内网)IP 和 公网 IP。
代理模式,支持自动创建域名记录。

Features
- 兼容和跨平台:
- [x] 可执行文件(无需 python 环境)
- [x] 多系统兼容

- [x] python2 和 python3 支持

- [x] PIP 安装

- [x] Docker 支持(@NN708)
- 域名支持:
- [x] 多个域名支持
- [x] 多级域名解析
- [x] 自动创建新记录
- IP 类型:
- [x] 内网 IPv4 / IPv6
- [x] 公网 IPv4 / IPv6 (支持自定义 API)
- [x] 自定义命令(shell)
- [x] 正则选取支持(@rufengsuixing)
- 网络代理:
- [x] http 代理支持
- [x] 多代理自动切换
- 服务商支持:
- [x] DNSPOD
- [x] 阿里 DNS
- [x] DNS.COM(@loftor-git)
- [x] DNSPOD 国际版
- [x] CloudFlare(@tongyifan)
- [x] HE.net(@NN708) (不支持自动创建记录)
- [x] 华为云(@cybmp3)
- 其他:
- [x] 可设置定时任务
- [x] TTL 配置支持
- [x] 本地文件缓存(减少 API 请求)
- [x] 地址变更时触发自定义回调API(与 DDNS 功能互斥)
使用
① 安装
根据需要选择一种方式: 二进制
版,pip
版,源码
运行,或者Docker
-
pip 安装(需要 pip 或 easy_install)
- 安装 ddns:
pip install ddns
或 easy_install ddns
- 运行:
ddns
-
二进制版(单文件,无需 python)
- Windows ddns.exe
- Linux (仅 Ubuntu 测试) ddns
- Mac OSX ddns-osx
-
源码运行(无任何依赖, 需 python 环境)
- clone 或者下载此仓库并解压
- 运行./run.py (widnows 双击
run.bat
或者运行python run.py
)
-
Docker(需要安装 Docker)
- 使用环境变量:
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=ddns.newfuture.cc \
-e DDNS_IPV6=ddns.newfuture.cc \
--network host \
newfuture/ddns
- 使用配置文件:
docker run -d \
-v /path/to/config.json:/config.json \
--network host \
newfuture/ddns
② 快速配置
-
申请 api token
,填写到对应的id
和token
字段:
-
DNSPOD(国内版)创建 token
- 阿里云 accesskey
- DNS.COM API Key/Secret
- DNSPOD(国际版)
- CloudFlare API Key (除了
email + API KEY
,也可使用Token
需要列出 Zone 权限)
- HE.net DDNS 文档(仅需将设置的密码填入
token
字段,id
字段可留空)
- 华为 APIKEY 申请(点左边访问密钥,然后点新增访问密钥)
-
自定义回调的参数填写方式请查看下方的自定义回调配置说明
-
修改配置文件,ipv4
和ipv6
字段,为待更新的域名,详细参照配置说明
详细配置
所有字段可通过三种方式进行配置
- 命令行参数
ddns --key=value
(ddns -h
查看详情),优先级最高
- JSON配置文件(值为null认为是有效值,会覆盖环境变量的设置,如果没有对应的key则会尝试试用环境变量)
- 环境变量DDNS_前缀加上key 全大写或者全小写 (
${ddns_key}
或 ${DDNS_KEY}
)
config.json 配置文件
- 首次运行会自动生成一个模板配置文件
- 可以使用 `-c`使用指定的配置文件 (默认读取当前目录的 config.json)
- 推荐使用 vscode 等支持 JsonSchema 的编辑器编辑配置文件
```bash
ddns -c path/to/config.json
# 或者源码运行
python run.py -c /path/to/config.json
```
#### 配置参数表
| key | type | required | default | description | tips |
| :----: | :----------------: | :------: | :---------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
| id | string | √ | 无 | api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)
HE.net 可留空 |
| token | string | √ | 无 | api 授权 token | 部分平台叫 secret key , **反馈粘贴时删除** |
| dns | string | No | `"dnspod"` | dns 服务商 | 阿里 DNS 为`alidns`,
Cloudflare 为 `cloudflare`,
dns.com 为 `dnscom`,
DNSPOD 国内为 `dnspod`,
DNSPOD 国际版为 `dnspod_com`,
HE.net 为`he`,
华为 DNS 为`huaweidns`,
自定义回调为`callback` |
| ipv4 | array | No | `[]` | ipv4 域名列表 | 为`[]`时,不会获取和更新 IPv4 地址 |
| ipv6 | array | No | `[]` | ipv6 域名列表 | 为`[]`时,不会获取和更新 IPv6 地址 |
| index4 | string\|int\|array | No | `"default"` | ipv4 获取方式 | 可设置`网卡`,`内网`,`公网`,`正则`等方式 |
| index6 | string\|int\|array | No | `"default"` | ipv6 获取方式 | 可设置`网卡`,`内网`,`公网`,`正则`等方式 |
| ttl | number | No | `null` | DNS 解析 TTL 时间 | 不设置采用 DNS 默认策略 |
| proxy | string | No | 无 | http 代理`;`分割 | 多代理逐个尝试直到成功,`DIRECT`为直连 |
| debug | bool | No | `false` | 是否开启调试 | 运行异常时,打开调试输出,方便诊断错误 |
| cache | string\|bool | No | `true` | 是否缓存记录 | 正常情况打开避免频繁更新,默认位置为临时目录下`ddns.cache`,
也可以指定一个具体文件实现自定义文件缓存位置 |
#### index4 和 index6 参数说明
- 数字(`0`,`1`,`2`,`3`等): 第 i 个网卡 ip
- 字符串`"default"`(或者无此项): 系统访问外网默认 IP
- 字符串`"public"`: 使用公网 ip(使用公网 API 查询,url 的简化模式)
- 字符串`"url:xxx"`: 打开 URL `xxx`(如:`"url:http://ip.sb"`),从返回的数据提取 IP 地址
- 字符串`"regex:xxx"` 正则表达(如`"regex:192.*"`): 提取`ifconfig`/`ipconfig`中与之匹配的首个 IP 地址,**注意 json 转义**(`\`要写成`\\`)
- `"192.*"`表示 192 开头的所有 ip
- 如果想匹配`10.00.xxxx`应该写成`"regex:10\\.00\\..\*"`(`"\\"`json 转义成`\`)
- 字符串`"cmd:xxxx"`: 执行命令`xxxx`的 stdout 输出结果作为目标 IP
- 字符串`"shell:xxx"`: 使用系统 shell 运行`xxx`,并把结果 stdout 作为目标 IP
- `false`: 强制禁止更新 ipv4 或 ipv6 的 DNS 解析
- 列表:依次执行列表中的index规则,并将最先获得的结果作为目标 IP
- 例如`["public", "172.*"]`将先查询公网API,未获取到IP后再从本地寻找172开头的IP
#### 自定义回调配置说明
- `id` 字段填写回调地址,以 HTTP 或 HTTPS 开头,推荐采用 HTTPS 方式的回调 API ,当 `token` 字段非空且 URL 参数包含下表所示的常量字符串时,常量会被程序替换为实际值
- `token` 字段为 POST 参数,本字段为空或不存在则使用 GET 方式发起回调,回调参数采用 JSON 格式编码,当 JSON 的首层参数值包含下表所示的常量字符串时,常量会被程序替换为实际值
| 常量名称 | 常量内容 | 说明 |
| ---------------- | ---------------------- | -------- |
| `__DOMAIN__` | DDNS 域名 | |
| `__RECORDTYPE__` | DDNS 记录类型 | |
| `__TTL__` | DDNS TTL | |
| `__TIMESTAMP__` | 请求发起时间戳 | 包含小数 |
| `__IP__` | 获取的对应类型的IP地址 | |
#### 配置示例
```json
{
"$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
"id": "12345",
"token": "mytokenkey",
"dns": "dnspod 或 dnspod_com 或 alidns 或 dnscom 或 cloudflare 或 he 或 huaweidns 或 callback",
"ipv4": ["ddns.newfuture.cc", "ipv4.ddns.newfuture.cc"],
"ipv6": ["ddns.newfuture.cc", "ipv6.ddns.newfuture.cc"],
"index4": 0,
"index6": "public",
"ttl": 600,
"proxy": "127.0.0.1:1080;DIRECT",
"debug": false
}
```
定时任务
可以通过脚本设置定时任务(默认每5分钟检查一次ip,自动更新)
#### Windows
- [推荐]以系统身份运行,右键"以管理员身份运行"`task.bat`(或者在管理员命令行中运行)
- 以当前用户身份运行定时任务,双击或者运行`task.bat` (执行时会闪黑框)
#### Linux
- 使用init.d和crontab:
`sudo ./task.sh`
- 使用systemd:
```bash
安装:
sudo ./systemd.sh install
卸载:
sudo ./systemd.sh uninstall
```
该脚本安装的文件符合 [Filesystem Hierarchy Standard (FHS)](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard):
可执行文件所在目录为 `/usr/share/DDNS`
配置文件所在目录为 `/etc/DDNS`
#### Docker
Docker镜像在无额外参数的情况下,已默认启用每5分钟执行一次的定时任务
FAQ
Windows Server [SSL: CERTIFICATE_VERIFY_FAILED]
> Windows Server 默认安全策略会禁止任何未添加的信任 ssl 证书,可手动添加一下对应的证书 [#56](https://github.com/NewFuture/DDNS/issues/56#issuecomment-487371078)
使用系统自带的 IE 浏览器访问一次对应的 API 即可
- alidns 打开:
- cloudflare 打开:
- dns.com 打开:
- dnspod.cn 打开:
- dnspod 国际版:
- 华为 DNS
问题排查反馈
1. 先确认排查是否是系统/网络环境问题
2. 在[issues](https://github.com/NewFuture/DDNS/issues)中搜索是否有类似问题
3. 前两者均无法解决或者确定是 bug,[在此新建 issue](https://github.com/NewFuture/DDNS/issues/new)
- [ ] 开启 debug 配置
- [ ] 附上这些内容 **运行版本和方式**,**系统环境**, **出错日志**,**去掉 id/token**的配置文件
- [ ] 源码运行注明使用的 python 环境
Issues
opened on 2023-03-12 10:17:43 by RookieTerry
描述问题 (Describe the bug)
如题
版本信息 (version info)
- DDNS Version: 2.11.5
- OS Version: Win10工作站专业版
- Type(运行方式): Binary
- related issues (相关问题): 无法读取相同目录下自定义配置文件
复现步骤 (To Reproduce)
电脑没有安装Python环境,在cmd命令行下运行:"C:\Users\xxx\AppData\Local\ddns.exe" -c "C:\Users\xxx\AppData\Local\config.json",其中配置文件和ddns.exe
在同一目录下。
配置文件 (config file)
json
{
"$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
"debug": false,
"dns": "cloudflare",
"index4": "url:https://myip4.ipip.net/",
"index6": "url:https://myip6.ipip.net/",
"ipv4": [
"myurl"
],
"ipv6": [
"myurl",
],
"proxy": null,
"token": "mytoken",
"ttl": null
}
调试输出 (debug output)
``sh
ERROR:root: Config file
config.json` does not exist!
Traceback (most recent call last):
File "util\config.py", line 74, in __load_config
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "run.py", line 159, in
File "run.py", line 120, in main
File "util\config.py", line 64, in init_config
File "util\config.py", line 82, in __load_config
PermissionError: [Errno 13] Permission denied: 'config.json'
[10576] Failed to execute script 'run' due to unhandled exception!
Traceback (most recent call last):
File "", line 1, in
Traceback (most recent call last):
ValueError: underlying buffer has been detached
File "", line 1, in
ValueError: underlying buffer has been detached
```
opened on 2023-03-04 10:35:13 by lghilb-yu
描述问题 (Describe the bug)
配置文件修改完毕后无法运行
版本信息 (version info)
- DDNS Version: [email protected]:11:57+00:00
- OS Version: Windows 10 22H2
- Type(运行方式): Python3.11.1
- related issues (相关问题):
复现步骤 (To Reproduce)
- 运行
python config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
- 运行
pip install ddns
- 在
%USERPROFILE%/DESKTOP/DDNS
运行ddns
,配置模板成功生成。
- 在dnspod申请了id和token
- 填写配置文件
- 再次运行
ddns
- 报错(Python报错)
配置文件 (config file)
json
{
"$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
"debug": true,
"dns": "dnspod",
"id": "123456",(非真实)
"index4": "url:https://ip.sb",
"index6": "default",
"ipv4": [],
"ipv6": [
"mypc.example.com"(非真实)
],
"proxy": null,
"token": "abcdefghijklmnopqrstuvwxyz012345",(非真实)
"ttl": null
}
调试输出 (debug output)
sh
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Program Files\Python311\Scripts\ddns.exe\__main__.py", line 7, in <module>
File "C:\Program Files\Python311\Lib\site-packages\run.py", line 121, in main
dns = getattr(__import__('dns', fromlist=[dns_provider]), dns_provider)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'dns' has no attribute 'dnspod'
根本没有输出……直接python报错……我是不是下到假的了……
补充说明 (Additional context)
没有补充说明
opened on 2023-02-20 15:41:52 by LeadroyaL
原先代码存在逻辑问题,在cache配置为False情况下仍然创建了Cache结构体,导致认为创建出的Cache对象是合法对象,析构时open(False)抛出异常。
崩溃栈回溯如下,意思是调用了析构函数 __del__
时触发崩溃:
Exception ignored in: <function Cache.__del__ at 0x0000022A7420F160>
Traceback (most recent call last):
File "D:\python\python38\lib\site-packages\util\cache.py", line 140, in __del__
self.close()
File "D:\python\python38\lib\site-packages\util\cache.py", line 95, in close
self.sync()
File "D:\python\python38\lib\site-packages\util\cache.py", line 83, in sync
with open(self.__filename, 'wb') as data:
ValueError: Cannot open console input buffer for writing
跟踪代码,如果140行的cache是False,则142行的对象仍然被创建,因此144行的分支永远不可达。
python
140 cache = get_config('cache', True)
141 cache = cache is True and path.join(gettempdir(), 'ddns.cache') or cache
142 cache = Cache(cache)
143 if cache is False:
144 info("Cache is disabled!")
因此这里存在逻辑问题,已通过重构代码的方式修复。
opened on 2023-02-06 12:54:08 by shanhot
[email protected]:~# ./ddns
-bash: ./ddns: cannot execute binary file: Exec format error
[email protected]:~#
opened on 2023-01-30 15:30:39 by fannwong001
描述问题 (Describe the bug)
取本地ipv6地址时出错,想要直接取本地网卡(ens33)的ipv6地址(光猫直接分配的),在getaddrinfo时出错。
版本信息 (version info)
- DDNS Version: [email protected]
- OS Version: ubuntu18.04
- Type(运行方式): Python3
- related issues (相关问题):
复现步骤 (To Reproduce)
配置config.json,执行ddns -c config.json
ip addr 执行显示ens33是第二个网卡
配置文件 (config file)
config.json
{
"token": "xxxxxxxxxxxxxx",
"dns": "cloudflare",
"ipv4": [],
"ipv6": ["xxx.yyyyy.com"],
"index6": 1,
"debug": true
}
调试输出 (debug output)
Traceback (most recent call last):
File "/usr/local/bin/ddns", line 8, in
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/run.py", line 151, in main
update_ip('6', cache, dns, proxy_list)
File "/usr/local/lib/python3.8/dist-packages/run.py", line 97, in update_ip
address = get_ip(ip_type, index_rule)
File "/usr/local/lib/python3.8/dist-packages/run.py", line 51, in get_ip
value = getattr(ip, "local_v" + ip_type)(index)
File "/usr/local/lib/python3.8/dist-packages/util/ip.py", line 38, in local_v6
info = getaddrinfo(gethostname(), 0, AF_INET6)
File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname
补充说明 (Additional context)
/etc/hosts中配置为127.0.1.1 ubuntu
手动执行getaddrinfo(gethostname(), 0, AF_INET)时返回的是127.0.0.1,好像ipv4也没有达到获取某网卡地址的功能,这个功能原想法应该不是想要获取lo回环地址吧?
现在暂时用default方式实现的。
opened on 2023-01-10 06:23:41 by Townie-au
Hi
Would it possible to add a feature to this that also updates the zero trust gateway address?
https://api.cloudflare.com/#zero-trust-gateway-locations-update-zero-trust-gateway-location
Releases
release v2.12.0 2022-12-04 04:04:33
v2.11.5 2022-07-28 14:45:51

使用二进制文件 
各平台下载/使用方式
1. 下载ddns
curl https://github.com/NewFuture/DDNS/releases/download/v2.11.5/ddns -#SLo ddns && chmod +x ddns
2. [可选] 定时任务(当前目录):
curl -sSL https://github.com/NewFuture/DDNS/releases/download/v2.11.5/create-task.sh | bash
* ### Mac OSX
sh
命令行下载
curl https://github.com/NewFuture/DDNS/releases/download/v2.11.5/ddns-osx -#SLo ddns && chmod +x ddns
```
使用PIP 安装

Pypi 安装当前版本或者更新最新版本
- 安装当前版本[current version]:
pip install ddns=v2.11.5
- 更新最新版[update latest version]:
pip install -U ddns
What's Changed
- Support environment variables in Docker image by @NN708 in https://github.com/NewFuture/DDNS/pull/304
- feat(cf): keep proxied config in cloudflare by @NewFuture in https://github.com/NewFuture/DDNS/pull/314
- Fix #324 by @Anankke in https://github.com/NewFuture/DDNS/pull/325
- (doc):remove interface by @NewFuture in https://github.com/NewFuture/DDNS/pull/327
- fix(dnspod_com): resolve #286 by @NewFuture in https://github.com/NewFuture/DDNS/pull/329
- publish pypi with github actions by @NewFuture in https://github.com/NewFuture/DDNS/pull/331
- remove azure pipelines by @NewFuture in https://github.com/NewFuture/DDNS/pull/339
- Replace version by @NewFuture in https://github.com/NewFuture/DDNS/pull/340
- doc(readme):update release badge by @NewFuture in https://github.com/NewFuture/DDNS/pull/341
- ci(publish.yml): update action-gh-releas by @NewFuture in https://github.com/NewFuture/DDNS/pull/342
New Contributors
- @Anankke made their first contribution in https://github.com/NewFuture/DDNS/pull/325
Full Changelog: https://github.com/NewFuture/DDNS/compare/v2.10.3...v2.11.5