Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): index4/6允许使用列表提供多个规则 #188

Merged
merged 3 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ python run.py -c /path/to/config.json

#### 配置参数表

| key | type | required | default | description | tips |
| :----: | :---------: | :------: | :---------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
| id | string | √ | 无 | api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空 |
| token | string | √ | 无 | api 授权 token | 部分平台叫 secret key , **反馈粘贴时删除** |
| dns | string | No | `"dnspod"` | dns 服务商 | 阿里`alidns`,<br>dns.com 为`dnscom`,<br>DNSPOD 国际版`dnspod_com`,<br>HE.net 为`he`,华为 DNS 为`huaweidns`,<br>自定义回调为`callback` |
| ipv4 | array | No | `[]` | ipv4 域名列表 | 为`[]`时,不会获取和更新 IPv4 地址 |
| ipv6 | array | No | `[]` | ipv6 域名列表 | 为`[]`时,不会获取和更新 IPv6 地址 |
| index4 | string\|int | No | `"default"` | ipv4 获取方式 | 可设置`网卡`,`内网`,`公网`,`正则`等方式 |
| index6 | string\|int | No | `"default"` | ipv6 获取方式 | 可设置`网卡`,`内网`,`公网`,`正则`等方式 |
| ttl | number | No | `null` | DNS 解析 TTL 时间 | 不设置采用 DNS 默认策略 |
| proxy | string | No | 无 | http 代理`;`分割 | 多代理逐个尝试直到成功,`DIRECT`为直连 |
| debug | bool | No | `false` | 是否开启调试 | 运行异常时,打开调试输出,方便诊断错误 |
| cache | bool | No | `true` | 是否缓存记录 | 正常情况打开避免频繁更新 |
| key | type | required | default | description | tips |
| :----: | :----------------: | :------: | :---------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
| id | string | √ | 无 | api 访问 ID | Cloudflare 为邮箱(使用 Token 时留空)<br>HE.net 可留空 |
| token | string | √ | 无 | api 授权 token | 部分平台叫 secret key , **反馈粘贴时删除** |
| dns | string | No | `"dnspod"` | dns 服务商 | 阿里`alidns`,<br>dns.com 为`dnscom`,<br>DNSPOD 国际版`dnspod_com`,<br>HE.net 为`he`,华为 DNS 为`huaweidns`,<br>自定义回调为`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 | bool | No | `true` | 是否缓存记录 | 正常情况打开避免频繁更新 |

#### index4 和 index6 参数说明

Expand All @@ -137,6 +137,8 @@ python run.py -c /path/to/config.json
- 字符串`"cmd:xxxx"`: 执行命令`xxxx`的 stdout 输出结果作为目标 IP
- 字符串`"shell:xxx"`: 使用系统 shell 运行`xxx`,并把结果 stdout 作为目标 IP
- `false`: 强制禁止更新 ipv4 或 ipv6 的 DNS 解析
- 列表:依次执行列表中的index规则,并将最先获得的结果作为目标 IP
- 例如`["public", "172.*"]`将先查询公网API,未获取到IP后再从本地寻找172开头的IP

#### 自定义回调配置说明

Expand Down
12 changes: 9 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ def get_config(key=None, default=None, path="config.json"):
return get_config.config


def get_ip(ip_type):
def get_ip(ip_type, index="default"):
"""
get IP address
"""
index = get_config('index' + ip_type, "default")
if index is False: # disabled
return False
elif type(index) == list: # 如果获取到的规则是列表,则依次判断列表中每一个规则,直到找到一个可以正确获取到的IP
value = None
for i in index:
value = get_ip(ip_type, i)
if value:
break
elif str(index).isdigit(): # 数字 local eth
value = getattr(ip, "local_v" + ip_type)(index)
elif index.startswith('cmd:'): # cmd
Expand Down Expand Up @@ -129,7 +134,8 @@ def update_ip(ip_type, cache, dns, proxy_list):
domains = get_config(ipname)
if not domains:
return None
address = get_ip(ip_type)
index_rule = get_config('index' + ip_type, "default") # 从配置中获取index配置
address = get_ip(ip_type, index_rule)
if not address:
error('Fail to get %s address!' ,ipname)
return False
Expand Down
26 changes: 24 additions & 2 deletions schema/v2.8.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,19 @@
"type": [
"string",
"integer",
"boolean"
"boolean",
"array"
],
"items": {
"type": [
"string",
"integer"
],
"minimum": 0
},
"uniqueItems": true,
"minItems": 1,
"minimum": 0,
"title": "IPv4 address Setting",
"description": "本机 IPv4 获取方式设置",
"default": "default",
Expand All @@ -110,8 +121,19 @@
"type": [
"string",
"integer",
"boolean"
"boolean",
"array"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array的话限制一下item的类型 string/integer

{
"items": {
    "type": ["string","integer"],
    "minimum": 0
  },
  "uniqueItems": true,
  "minItems": 1,
  "minimum": 0
}

],
"items": {
"type": [
"string",
"integer"
],
"minimum": 0
},
"uniqueItems": true,
"minItems": 1,
"minimum": 0,
"title": "IPv6 address Setting",
"description": "本机 IPv6 获取方式设置",
"default": "default",
Expand Down