DDNS

DDNS JSON配置文件参考

本文档详细说明DDNS工具的JSON配置文件格式和参数。JSON配置文件优先级介于命令行参数和环境变量之间。

基本用法

默认情况下,DDNS会在当前目录查找config.json文件。您也可以使用-c参数指定配置文件路径:

注意:在Docker中使用配置文件时,需要通过卷映射将配置文件挂载到容器的/ddns/目录。详情请参考Docker使用文档

# 使用默认配置文件
# 无配置时会自动生成配置文件
ddns

# 使用指定配置文件
ddns -c /path/to/config.json

# 或者使用Python源码
python run.py -c /path/to/config.json

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 日志配置(可选) 日志配置对象,支持levelfileformatdatefmt参数

log对象参数说明

键名 类型 必需 默认值 说明
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参数说明

index4index6参数用于指定获取IP地址的方式,可以使用以下值:

自定义回调配置

dns设置为callback时,可通过以下方式配置自定义回调:

支持的常量替换:

常量名称 常量内容 说明
__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"
  }
}

使用多种IP获取方式的配置

{
  "$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配置文件 > 环境变量

配置覆盖示例

假设有以下配置:

  1. 环境变量DDNS_TTL=600
  2. JSON配置文件"ttl": 300
  3. 命令行参数--ttl 900

最终生效的是命令行参数的值:ttl=900

如果没有提供命令行参数,则使用JSON配置值:ttl=300

特殊情况

注意事项

  1. 配置文件使用UTF-8编码,不包含BOM标记
  2. JSON中所有键名区分大小写
  3. 在配置文件中,对于需要使用反斜杠的字符串(如正则表达式),需要进行双重转义
  4. debug参数在配置文件中设置无效,仅支持命令行参数--debug
  5. 首次运行时会在当前目录自动生成一个模板配置文件
  6. 推荐使用支持JSONSchema的编辑器(如VSCode)编辑配置文件,可获得自动补全和验证功能