Skip to content

Commit da19b8c

Browse files
authored
更新配置文件模式至v4.0 重构日志配置,(NewFuture#465)
Break changes * 移除调试模式, * 添加日志级别和输出文件
1 parent 3baf48a commit da19b8c

7 files changed

Lines changed: 158 additions & 59 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
config.*.json
22

3-
.vscode
43
*.temp
54

65
# Byte-compiled / optimized / DLL files

.vscode/extensions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"recommendations": [
3+
"ms-python.flake8",
4+
"ms-python.autopep8",
5+
"ms-python.python",
6+
"github.vscode-github-actions",
7+
]
8+
}

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# [DDNS](https://github.com/NewFuture/DDNS) [<img src=".build/ddns.svg" width="32px" height="32px"/>](https://ddns.newfuture.cc)
22

3-
43
> 自动更新 DNS 解析 到本机 IP 地址,支持 ipv4 和 ipv6 以 本地(内网)IP 和 公网 IP。
54
> 代理模式,支持自动创建域名记录。
65
7-
86
[![PyPI](https://img.shields.io/pypi/v/ddns.svg?label=DDNS&style=social)](https://pypi.org/project/ddns/)
97
[![Build Status](https://github.com/NewFuture/DDNS/actions/workflows/build.yml/badge.svg?event=push)](https://github.com/NewFuture/DDNS/actions/workflows/build.yml)
108
[![Publish Status](https://github.com/NewFuture/DDNS/actions/workflows/publish.yml/badge.svg)](https://github.com/NewFuture/DDNS/releases/latest)
@@ -18,7 +16,7 @@
1816
- [x] 多系统兼容 ![cross platform](https://img.shields.io/badge/platform-windows_%7C%20linux_%7C%20osx-success.svg?style=social)
1917
- [x] python3 支持 ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ddns.svg?style=social)(2.x支持python2和python3)
2018
- [x] PIP 安装 ![PyPI - Wheel](https://img.shields.io/pypi/wheel/ddns.svg?style=social)
21-
- [x] Docker 支持(@NN708)
19+
- [x] Docker 支持(@NN708)
2220
- 域名支持:
2321
- [x] 多个域名支持
2422
- [x] 多级域名解析
@@ -52,17 +50,25 @@
5250
根据需要选择一种方式: `二进制`版,`pip`版,`源码`运行,或者`Docker`
5351

5452
- #### pip 安装(需要 pip 或 easy_install)
53+
5554
1. 安装 ddns: `pip install ddns``easy_install ddns`
5655
2. 运行: `ddns`
56+
5757
- #### 二进制版(单文件,无需 python)
58+
5859
- Windows [ddns.exe](https://github.com/NewFuture/DDNS/releases/latest)
5960
- Linux (仅 Ubuntu 测试) [ddns](https://github.com/NewFuture/DDNS/releases/latest)
6061
- Mac OSX [ddns-osx](https://github.com/NewFuture/DDNS/releases/latest)
62+
6163
- #### 源码运行(无任何依赖, 需 python 环境)
64+
6265
1. clone 或者[下载此仓库](https://github.com/NewFuture/DDNS/archive/master.zip)并解压
6366
2. 运行./run.py (widnows 双击`run.bat`或者运行`python run.py`)
67+
6468
- #### Docker(需要安装 Docker)
69+
6570
- 使用环境变量:
71+
6672
```
6773
docker run -d \
6874
-e DDNS_DNS=dnspod \
@@ -73,7 +79,9 @@
7379
--network host \
7480
newfuture/ddns
7581
```
82+
7683
- 使用配置文件(docker 工作目录`/ddds/`,默认配置位置`/ddns/config.json`):
84+
7785
```
7886
docker run -d \
7987
-v /local/config/path/:/ddns/ \
@@ -102,7 +110,7 @@
102110
103111
1. 命令行参数 `ddns --key=value` (`ddns -h` 查看详情),优先级最高
104112
2. JSON配置文件(值为null认为是有效值,会覆盖环境变量的设置,如果没有对应的key则会尝试试用环境变量)
105-
3. 环境变量DDNS_前缀加上key 全大写或者全小写 (`${ddns_key}` 或 `${DDNS_KEY}`)
113+
3. 环境变量DDNS_前缀加上key 全大写或者全小写,点转下划线 (`${ddns_id}` 或 `${DDNS_ID}`,`${DDNS_LOG_LEVEL}`)
106114
107115
<details open>
108116
@@ -132,8 +140,9 @@ python run.py -c /path/to/config.json
132140
| index6 | string\|int\|array | No | `"default"` | ipv6 获取方式 | 可设置`网卡`,`内网`,`公网`,`正则`等方式 |
133141
| ttl | number | No | `null` | DNS 解析 TTL 时间 | 不设置采用 DNS 默认策略 |
134142
| proxy | string | No || http 代理`;`分割 | 多代理逐个尝试直到成功,`DIRECT`为直连 |
135-
| debug | bool | No | `false` | 是否开启调试 | 运行异常时,打开调试输出,方便诊断错误 |
143+
| ~~debug~~ | bool | No | `false` | 是否开启调试 | v4 中弃用,请改用log.level=DEBUG |
136144
| cache | string\|bool | No | `true` | 是否缓存记录 | 正常情况打开避免频繁更新,默认位置为临时目录下`ddns.cache`,<br>也可以指定一个具体文件实现自定义文件缓存位置 |
145+
| log | {"level":string,"file":string} | No | `null` | 日志配置(可选) | 日志配置,日志级别和路径(默认命令行),<br>例如: `{ "level": "DEBUG", "file": "/path/to/logfile.log" }` |
137146

138147
#### index4 和 index6 参数说明
139148

@@ -167,7 +176,7 @@ python run.py -c /path/to/config.json
167176

168177
```json
169178
{
170-
"$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
179+
"$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
171180
"id": "12345",
172181
"token": "mytokenkey",
173182
"dns": "dnspod 或 dnspod_com 或 alidns 或 dnscom 或 cloudflare 或 he 或 huaweidns 或 callback",
@@ -177,7 +186,10 @@ python run.py -c /path/to/config.json
177186
"index6": "public",
178187
"ttl": 600,
179188
"proxy": "127.0.0.1:1080;DIRECT",
180-
"debug": false
189+
"log": {
190+
"level": "DEBUG",
191+
"file": "dns.log"
192+
},
181193
}
182194
```
183195

@@ -200,12 +212,14 @@ python run.py -c /path/to/config.json
200212
- 使用init.d和crontab:
201213
`sudo ./task.sh`
202214
- 使用systemd:
215+
203216
```bash
204217
安装:
205218
sudo ./systemd.sh install
206219
卸载:
207220
sudo ./systemd.sh uninstall
208221
```
222+
209223
该脚本安装的文件符合 [Filesystem Hierarchy Standard (FHS)](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard):
210224
可执行文件所在目录为 `/usr/share/DDNS`
211225
配置文件所在目录为 `/etc/DDNS`

run.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
# nuitka-project-else:
1212
# nuitka-project: --product-version=0.0.0
1313

14-
from __future__ import print_function
15-
from time import ctime, asctime
1614
from os import path, environ, name as os_name
1715
from tempfile import gettempdir
18-
from logging import DEBUG, basicConfig, info, warning, error, debug
16+
from logging import basicConfig, info, warning, error, debug
1917
from subprocess import check_output
2018

2119
import sys
@@ -82,8 +80,7 @@ def change_dns_record(dns, proxy_list, **kw):
8280
else:
8381
dns.Config.PROXY = proxy
8482
record_type, domain = kw['record_type'], kw['domain']
85-
print('\n%s %s(%s) ==> %s [via %s]' %
86-
(asctime(), domain, record_type, kw['ip'], proxy))
83+
info("%s(%s) ==> %s [via %s]", domain, record_type, kw['ip'], proxy)
8784
try:
8885
return dns.update_record(domain, kw['ip'], record_type=record_type)
8986
except Exception as e:
@@ -108,7 +105,7 @@ def update_ip(ip_type, cache, dns, proxy_list):
108105
error('Fail to get %s address!', ipname)
109106
return False
110107
elif cache and (address == cache[ipname]):
111-
print('.', end=" ") # 缓存命中
108+
info('%s address not changed, using cache.', ipname)
112109
return True
113110
record_type = (ip_type == '4') and 'A' or 'AAAA'
114111
update_fail = False # https://github.com/NewFuture/DDNS/issues/16
@@ -132,16 +129,17 @@ def main():
132129
dns.Config.ID = get_config('id')
133130
dns.Config.TOKEN = get_config('token')
134131
dns.Config.TTL = get_config('ttl')
135-
if get_config('debug'):
136-
ip.DEBUG = get_config('debug')
137-
basicConfig(
138-
level=DEBUG,
139-
format='%(asctime)s <%(module)s.%(funcName)s> %(lineno)d@%(pathname)s \n[%(levelname)s] %(message)s')
140-
print("DDNS[", __version__, "] run:", os_name, sys.platform)
141-
if get_config("config"):
142-
print("Configuration was loaded from <==",
143-
path.abspath(get_config("config")))
144-
print("=" * 25, ctime(), "=" * 25, sep=' ')
132+
133+
basicConfig(
134+
level=get_config('log.level'),
135+
format='%(asctime)s [%(levelname)s] %(message)s',
136+
datefmt='%m-%d %H:%M:%S',
137+
filename=get_config('log.file'),
138+
)
139+
140+
info("DDNS[ %s ] run: %s %s", __version__, os_name, sys.platform)
141+
if get_config("config"):
142+
info("loaded Config from: %s", path.abspath(get_config('config')))
145143

146144
proxy = get_config('proxy') or 'DIRECT'
147145
proxy_list = proxy if isinstance(

schema/v2.8.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,14 @@
176176
"debug": {
177177
"$id": "/properties/debug",
178178
"type": "boolean",
179-
"title": "Enable Debug Mode",
180-
"description": "是否启用调试模式显示更多信息",
179+
"title": "Enable Debug Mode (deprecated, use logger instead)",
180+
"description": "是否启用调试模式显示更多信息(已废弃,请使用 logger 字段)",
181181
"default": false,
182182
"examples": [
183183
false,
184184
true
185-
]
185+
],
186+
"deprecated": true
186187
},
187188
"cache": {
188189
"$id": "/properties/cache",

schema.json renamed to schema/v4.0.json

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema",
3-
"$id": "https://ddns.newfuture.cc/schema.json",
4-
"description": "[Deprecated] 已弃用,请使用 https://ddns.newfuture.cc/schema/v2.json",
3+
"$id": "https://ddns.newfuture.cc/schema/v4.0.json",
4+
"description": "DNS 配置文件 https://github.com/NewFuture/DDNS",
55
"type": "object",
66
"properties": {
77
"$schema": {
88
"type": "string",
9-
"title": "please use https://ddns.newfuture.cc/schema/v2.json",
10-
"description": "请更换为 https://ddns.newfuture.cc/schema/v2.json",
9+
"title": "please use https://ddns.newfuture.cc/schema/v2.8.json",
10+
"description": "请更换为 https://ddns.newfuture.cc/schema/v2.8.json",
11+
"default": "https://ddns.newfuture.cc/schema/v2.8.json",
1112
"enum": [
12-
"https://ddns.newfuture.cc/schema/v2.json",
13-
"http://ddns.newfuture.cc/schema/v2.json"
13+
"https://ddns.newfuture.cc/schema/v2.8.json",
14+
"http://ddns.newfuture.cc/schema/v2.8.json",
15+
"./schema/v2.8.json"
1416
]
1517
},
1618
"id": {
1719
"$id": "/properties/id",
18-
"type": "string",
20+
"type": [
21+
"string",
22+
"null"
23+
],
1924
"title": "ID or Email",
2025
"description": "DNS服务API认证的ID或者邮箱"
2126
},
@@ -29,7 +34,7 @@
2934
"$id": "/properties/dns",
3035
"type": "string",
3136
"title": "DNS Provider",
32-
"description": "dns服务商:阿里为alidns,DNS.COM为dnscom,DNSPOD国际版为(dnspod_com),cloudflare,HE.net为he",
37+
"description": "dns服务商:阿里为alidns,DNS.COM为dnscom,DNSPOD国际版为(dnspod_com),cloudflare,HE.net为he,华为DNS为huaweidns,自定义回调为callback",
3338
"default": "dnspod",
3439
"examples": [
3540
"dnspod",
@@ -42,7 +47,9 @@
4247
"cloudflare",
4348
"dnspod_com",
4449
"dnscom",
45-
"he"
50+
"he",
51+
"huaweidns",
52+
"callback"
4653
]
4754
},
4855
"ipv4": {
@@ -55,7 +62,7 @@
5562
"$id": "/properties/ipv4/items",
5663
"title": "ipv4 domain for DDNS",
5764
"type": "string",
58-
"pattern": "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,18}$",
65+
"pattern": "^(?:\\*\\.)?(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,18}$",
5966
"examples": [
6067
"newfuture.cc",
6168
"ipv4.example.newfuture.cc"
@@ -72,7 +79,7 @@
7279
"$id": "/properties/ipv6/items",
7380
"title": "The ipv6 domain for DDNS",
7481
"type": "string",
75-
"pattern": "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$",
82+
"pattern": "^(?:\\*\\.)?(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,18}$",
7683
"examples": [
7784
"newfuture.cc",
7885
"ipv6.example.newfuture.cc"
@@ -84,8 +91,19 @@
8491
"type": [
8592
"string",
8693
"integer",
87-
"boolean"
94+
"boolean",
95+
"array"
8896
],
97+
"items": {
98+
"type": [
99+
"string",
100+
"integer"
101+
],
102+
"minimum": 0
103+
},
104+
"uniqueItems": true,
105+
"minItems": 1,
106+
"minimum": 0,
89107
"title": "IPv4 address Setting",
90108
"description": "本机 IPv4 获取方式设置",
91109
"default": "default",
@@ -103,8 +121,19 @@
103121
"type": [
104122
"string",
105123
"integer",
106-
"boolean"
124+
"boolean",
125+
"array"
107126
],
127+
"items": {
128+
"type": [
129+
"string",
130+
"integer"
131+
],
132+
"minimum": 0
133+
},
134+
"uniqueItems": true,
135+
"minItems": 1,
136+
"minimum": 0,
108137
"title": "IPv6 address Setting",
109138
"description": "本机 IPv6 获取方式设置",
110139
"default": "default",
@@ -144,16 +173,42 @@
144173
"127.0.0.1:1080;DIRECT"
145174
]
146175
},
147-
"debug": {
148-
"$id": "/properties/debug",
149-
"type": "boolean",
150-
"title": "Enable Debug Mode",
151-
"description": "是否启用调试模式显示更多信息",
152-
"default": false,
176+
"cache": {
177+
"$id": "/properties/cache",
178+
"type": [
179+
"string",
180+
"boolean"
181+
],
182+
"title": "Enable Cache",
183+
"description": "是否启用缓存记录以避免频繁更新",
184+
"default": true,
153185
"examples": [
186+
true,
154187
false,
155-
true
188+
"/path/to/cache/ddns.cache"
156189
]
190+
},
191+
"log": {
192+
"$id": "/properties/log",
193+
"type": "object",
194+
"title": "Log Config",
195+
"description": "日志配置,支持自定义日志级别和输出位置。可通过命令行 --log.level, --log.file 或环境变量 DDNS_LOG_LEVEL, DDNS_LOG_FILE 设置。",
196+
"properties": {
197+
"level": {
198+
"type": "string",
199+
"title": "Log Level",
200+
"description": "日志级别,如 DEBUG、INFO、WARNING、ERROR、CRITICAL",
201+
"default": "INFO",
202+
"enum": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
203+
},
204+
"file": {
205+
"type": ["string", "null"],
206+
"title": "Log Output File",
207+
"description": "日志输出文件路径,留空或为null时输出到控制台"
208+
}
209+
},
210+
"required": [],
211+
"additionalProperties": false
157212
}
158213
},
159214
"required": [

0 commit comments

Comments
 (0)