本文档详细说明DDNS工具的JSON配置文件格式和参数。JSON配置文件优先级介于命令行参数和环境变量之间。
默认情况下,DDNS会在当前目录查找config.json
文件。您也可以使用-c
参数指定配置文件路径:
config.json
(注意Docker运行目录是 /ddns/
)~/.ddns/config.json
/etc/ddns/config.json
注意:在Docker中使用配置文件时,需要通过卷映射将配置文件挂载到容器的
/ddns/
目录。详情请参考Docker使用文档。
# 使用默认配置文件
# 无配置时会自动生成配置文件
ddns
# 使用指定配置文件
ddns -c /path/to/config.json
# 或者使用Python源码
python run.py -c /path/to/config.json
DDNS配置文件遵循JSON模式(Schema),推荐在配置文件中添加$schema
字段以获得编辑器的自动补全和验证功能:
{
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json"
}
键名 | 类型 | 必需 | 默认值 | 说明 | 备注 |
---|---|---|---|---|---|
id | string | 是 | 无 | API 访问 ID | Cloudflare为邮箱(使用Token时留空) HE.net可留空 华为云为Access Key ID (AK) |
token | string | 是 | 无 | API 授权令牌 | 部分平台叫secret key |
dns | string | 否 | "dnspod" |
DNS服务商 | 可选值: dnspod, alidns, cloudflare, dnscom, dnspod_com, he, huaweidns, callback |
ipv4 | array | 否 | [] |
IPv4域名列表 | 为[] 时,不会获取和更新IPv4地址 |
ipv6 | array | 否 | [] |
IPv6域名列表 | 为[] 时,不会获取和更新IPv6地址 |
index4 | string|int|array | 否 | "default" |
IPv4获取方式 | 详见下方说明 |
index6 | string|int|array | 否 | "default" |
IPv6获取方式 | 详见下方说明 |
ttl | number | 否 | null |
DNS解析TTL时间 | 单位为秒,不设置则采用DNS默认策略 |
proxy | string|array | 否 | 无 | HTTP代理 | 多代理逐个尝试直到成功,DIRECT 为直连 |
debug | boolean | 否 | false |
是否开启调试 | 等同于设置log.level=DEBUG,配置文件中设置此字段无效,仅命令行参数--debug 有效 |
cache | string|bool | 否 | true |
是否缓存记录 | 正常情况打开避免频繁更新,默认位置为临时目录下ddns.cache ,也可以指定具体路径 |
log | object | 否 | null |
日志配置(可选) | 日志配置对象,支持level 、file 、format 、datefmt 参数 |
键名 | 类型 | 必需 | 默认值 | 说明 |
---|---|---|---|---|
level | string | 否 | INFO |
日志级别 |
file | string | 否 | 无 | 日志文件路径 |
format | string | 否 | %(asctime)s %(levelname)s [%(module)s]: %(message)s |
日志格式字符串 |
datefmt | string | 否 | %Y-%m-%dT%H:%M:%S |
日期时间格式 |
index4
和index6
参数用于指定获取IP地址的方式,可以使用以下值:
0
、1
、2
…):表示使用第N个网卡的IP地址"default"
:系统访问外网的默认IP"public"
:使用公网IP(通过API查询)"url:xxx"
:通过指定URL获取IP,例如"url:http://ip.sb"
"regex:xxx"
:使用正则表达式匹配本地网络配置中的IP,例如"regex:192\\.168\\..*"
"regex:10\\.00\\..*"
表示匹配10.00.
开头的IP"cmd:xxx"
:执行指定命令并使用其输出作为IP"shell:xxx"
:使用系统shell运行命令并使用其输出作为IPfalse
表示禁止更新相应IP类型的DNS记录当dns
设置为callback
时,可通过以下方式配置自定义回调:
id
字段:填写回调地址,以HTTP或HTTPS开头token
字段:POST参数,为空则使用GET方式发起回调支持的常量替换:
常量名称 | 常量内容 | 说明 |
---|---|---|
__DOMAIN__ |
DDNS域名 | - |
__RECORDTYPE__ |
DDNS记录类型 | A或AAAA |
__TTL__ |
DDNS TTL | - |
__TIMESTAMP__ |
请求发起时间戳 | 包含小数 |
__IP__ |
获取的IP地址 | - |
{
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
"id": "12345",
"token": "mytokenkey",
"dns": "dnspod",
"ipv4": ["example.com", "www.example.com"],
"ipv6": ["example.com", "ipv6.example.com"],
"ttl": 600
}
{
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
"id": "12345",
"token": "mytokenkey",
"dns": "cloudflare",
"ipv4": ["example.com", "www.example.com"],
"ipv6": ["example.com", "ipv6.example.com"],
"index4": ["public", "regex:192\\.168\\.1\\..*"],
"index6": "public",
"ttl": 300,
"proxy": "127.0.0.1:1080;DIRECT",
"cache": "/var/cache/ddns.cache",
"log": {
"level": "DEBUG",
"file": "/var/log/ddns.log",
"format": "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d]: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
}
{
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
"id": "12345",
"token": "mytokenkey",
"dns": "dnspod",
"ipv4": ["example.com"],
"ipv6": ["example.com"],
"index4": ["public", 0, "regex:192\\.168\\..*"],
"index6": ["public", "url:http://ipv6.icanhazip.com"],
"ttl": 600
}
{
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
"id": "https://api.example.com/ddns/update?domain=__DOMAIN__&type=__RECORDTYPE__&ip=__IP__",
"token": "{\"domain\": \"__DOMAIN__\", \"ip\": \"__IP__\", \"timestamp\": \"__TIMESTAMP__\"}",
"dns": "callback",
"ipv4": ["example.com"],
"ipv6": ["example.com"],
"ttl": 600
}
DDNS工具中的配置优先级顺序为:命令行参数 > JSON配置文件 > 环境变量。
假设有以下配置:
DDNS_TTL=600
"ttl": 300
--ttl 900
最终生效的是命令行参数的值:ttl=900
如果没有提供命令行参数,则使用JSON配置值:ttl=300
null
时,将覆盖环境变量设置,相当于未设置该值debug
)仅在特定配置方式下有效:debug
参数只在命令行中有效,JSON配置中的设置会被忽略debug
参数在配置文件中设置无效,仅支持命令行参数--debug