Skip to content

Commit bb60b58

Browse files
committed
Allow customizing TUN DNS mode and hijack interface DNS by default
1 parent 85889fc commit bb60b58

17 files changed

Lines changed: 187 additions & 63 deletions

File tree

docs/configuration/dns/rule.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ Match specified DNS servers' preferred domains.
513513
| `local` | Match hosts entries, neighbor-resolved hosts, and mDNS local domains |
514514
| `mdns` | Match mDNS local domains (`*.local.` and IPv4/IPv6 link-local reverse zones) |
515515
| `tailscale` | Match MagicDNS hosts and DNS route suffixes |
516+
| `resolved` | Match split DNS and search domains from systemd-resolved links |
516517

517518
#### wifi_ssid
518519

docs/configuration/dns/rule.zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ Available values: `wifi`, `cellular`, `ethernet` and `other`.
505505
| `local` | 匹配 hosts 中的条目、邻居解析得到的主机名以及 mDNS 本地域名 |
506506
| `mdns` | 匹配 mDNS 本地域名(`*.local.` 以及 IPv4/IPv6 链路本地反向区域) |
507507
| `tailscale` | 匹配 MagicDNS 主机和 DNS 路由后缀 |
508+
| `resolved` | 匹配 systemd-resolved 链路中的分流域名和搜索域 |
508509

509510
#### wifi_ssid
510511

docs/configuration/dns/server/hosts.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,9 @@ Example:
8989
],
9090
"rules": [
9191
{
92-
"action": "evaluate",
92+
"preferred_by": "hosts",
93+
"action": "route",
9394
"server": "hosts"
94-
},
95-
{
96-
"match_response": true,
97-
"ip_accept_any": true,
98-
"action": "respond"
9995
}
10096
]
10197
}

docs/configuration/dns/server/hosts.zh.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,9 @@ hosts 文件路径列表。
8989
],
9090
"rules": [
9191
{
92-
"action": "evaluate",
92+
"preferred_by": "hosts",
93+
"action": "route",
9394
"server": "hosts"
94-
},
95-
{
96-
"match_response": true,
97-
"ip_accept_any": true,
98-
"action": "respond"
9995
}
10096
]
10197
}

docs/configuration/dns/server/resolved.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,9 @@ If not enabled, `NXDOMAIN` will be returned for requests that do not match searc
6161
],
6262
"rules": [
6363
{
64-
"action": "evaluate",
64+
"preferred_by": "resolved",
65+
"action": "route",
6566
"server": "resolved"
66-
},
67-
{
68-
"match_response": true,
69-
"ip_accept_any": true,
70-
"action": "respond"
7167
}
7268
]
7369
}

docs/configuration/dns/server/resolved.zh.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,9 @@ icon: material/new-box
6060
],
6161
"rules": [
6262
{
63-
"action": "evaluate",
63+
"preferred_by": "resolved",
64+
"action": "route",
6465
"server": "resolved"
65-
},
66-
{
67-
"match_response": true,
68-
"ip_accept_any": true,
69-
"action": "respond"
7066
}
7167
]
7268
}

docs/configuration/dns/server/tailscale.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ Default resolvers are not consulted for single-label queries regardless of `acce
7373
],
7474
"rules": [
7575
{
76-
"action": "evaluate",
76+
"preferred_by": "ts",
77+
"action": "route",
7778
"server": "ts"
78-
},
79-
{
80-
"match_response": true,
81-
"ip_accept_any": true,
82-
"action": "respond"
8379
}
8480
]
8581
}

docs/configuration/dns/server/tailscale.zh.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ icon: material/new-box
7373
],
7474
"rules": [
7575
{
76-
"action": "evaluate",
76+
"preferred_by": "ts",
77+
"action": "route",
7778
"server": "ts"
78-
},
79-
{
80-
"match_response": true,
81-
"ip_accept_any": true,
82-
"action": "respond"
8379
}
8480
]
8581
}

docs/configuration/inbound/tun.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ icon: material/new-box
55
!!! quote "Changes in sing-box 1.14.0"
66

77
:material-plus: [include_mac_address](#include_mac_address)
8-
:material-plus: [exclude_mac_address](#exclude_mac_address)
8+
:material-plus: [exclude_mac_address](#exclude_mac_address)
9+
:material-plus: [dns_mode](#dns_mode)
10+
:material-plus: [dns_address](#dns_address)
911

1012
!!! quote "Changes in sing-box 1.13.3"
1113

@@ -73,6 +75,11 @@ icon: material/new-box
7375
"fdfe:dcba:9876::1/126"
7476
],
7577
"mtu": 9000,
78+
"dns_mode": "hijack",
79+
"dns_address": [
80+
"172.18.0.2",
81+
"fdfe:dcba:9876::2"
82+
],
7683
"auto_route": true,
7784
"iproute2_table_index": 2022,
7885
"iproute2_rule_index": 9000,
@@ -216,6 +223,52 @@ IPv6 prefix for the tun interface.
216223

217224
The maximum transmission unit.
218225

226+
#### dns_mode
227+
228+
!!! question "Since sing-box 1.14.0"
229+
230+
How DNS is handled on the TUN interface.
231+
232+
| Mode | Description |
233+
|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
234+
| `disabled` | Do not configure native DNS and do not hijack DNS traffic. |
235+
| `native` | Set the platform's native interface DNS where possible: per-interface DNS on Windows and Apple platforms, and `systemd-resolved` interface DNS on Linux. |
236+
| `hijack` | Same as `native`, with additional port 53 hijacking described below. Used by default. |
237+
238+
`hijack` adds the following on top of `native`:
239+
240+
*On Linux*: only DNS sent to non-local destinations can be intercepted.
241+
Traffic destined to addresses on the host's own interfaces (such as
242+
`127.0.0.53` or the host's LAN-side IP) is delivered through the kernel
243+
`local` routing table before any user rule applies, and `OUTPUT` NAT cannot
244+
redirect packets going through `lo`.
245+
246+
- Without `auto_redirect`, an `iproute2` rule makes port 53 skip the `main`
247+
table's specific-route lookup, forcing DNS that would otherwise be
248+
delivered through a directly-attached subnet through the TUN. Destination
249+
addresses are not rewritten.
250+
- With `auto_redirect`, an nftables rule DNATs port 53 traffic directly to
251+
[`dns_address`](#dns_address).
252+
253+
*On Windows with [`strict_route`](#strict_route)*: a WFP filter blocks port
254+
53 traffic going through interfaces other than the TUN.
255+
256+
#### dns_address
257+
258+
!!! question "Since sing-box 1.14.0"
259+
260+
List of DNS server addresses used by [`dns_mode`](#dns_mode).
261+
262+
When unset, sing-box derives one address per family by taking the next IP after
263+
the first IPv4/IPv6 entry in [`address`](#address). Connections toward those
264+
derived addresses are additionally hijacked into the sing-box DNS module,
265+
equivalent to a [`hijack-dns`](/configuration/route/rule_action/#hijack-dns)
266+
route action; this preserves the behaviour from before this option was added.
267+
268+
When set, this auto-hijack is not applied; configure an explicit
269+
[`hijack-dns`](/configuration/route/rule_action/#hijack-dns) route rule if the
270+
behaviour is still required.
271+
219272
#### gso
220273

221274
!!! failure "Deprecated in sing-box 1.11.0"

docs/configuration/inbound/tun.zh.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ icon: material/new-box
55
!!! quote "sing-box 1.14.0 中的更改"
66

77
:material-plus: [include_mac_address](#include_mac_address)
8-
:material-plus: [exclude_mac_address](#exclude_mac_address)
8+
:material-plus: [exclude_mac_address](#exclude_mac_address)
9+
:material-plus: [dns_mode](#dns_mode)
10+
:material-plus: [dns_address](#dns_address)
911

1012
!!! quote "sing-box 1.13.3 中的更改"
1113

@@ -73,6 +75,11 @@ icon: material/new-box
7375
"fdfe:dcba:9876::1/126"
7476
],
7577
"mtu": 9000,
78+
"dns_mode": "hijack",
79+
"dns_address": [
80+
"172.18.0.2",
81+
"fdfe:dcba:9876::2"
82+
],
7683
"auto_route": true,
7784
"iproute2_table_index": 2022,
7885
"iproute2_rule_index": 9000,
@@ -222,6 +229,46 @@ tun 接口的 IPv6 前缀。
222229

223230
最大传输单元。
224231

232+
#### dns_mode
233+
234+
!!! question "自 sing-box 1.14.0 起"
235+
236+
TUN 接口上 DNS 的处理方式。
237+
238+
| 模式 | 描述 |
239+
|------------|-------------------------------------------------------------------------------------------------------|
240+
| `disabled` | 不设置原生 DNS,也不劫持 DNS 流量。 |
241+
| `native` | 尽可能设置平台的原生接口 DNS:Windows 与 Apple 上的接口 DNS,Linux 上的 `systemd-resolved` 接口 DNS。 |
242+
| `hijack` |`native` 相同,并额外执行下文所述的 53 端口劫持。默认使用。 |
243+
244+
`hijack``native` 之上额外执行:
245+
246+
*Linux*:只能劫持发往非本机地址的 DNS。发往本机接口地址(如 `127.0.0.53`
247+
或本机 LAN 接口 IP)的流量由内核 `local` 路由表在所有用户规则之前直接交付,
248+
`OUTPUT` 链 NAT 也无法对走 `lo` 的包生效。
249+
250+
- 未启用 `auto_redirect` 时:通过 `iproute2` 规则让 53 端口跳过 `main` 表的
251+
具体路由查找,把本来会经直连子网直接送达的 DNS 改走 TUN —— 不重写目的地址。
252+
- 启用 `auto_redirect` 时:通过 nftables 规则将 53 端口流量直接 DNAT 至
253+
[`dns_address`](#dns_address)
254+
255+
*Windows 启用 [`strict_route`](#strict_route)*:通过 WFP 过滤器阻止经由非
256+
TUN 接口的 53 端口流量。
257+
258+
#### dns_address
259+
260+
!!! question "自 sing-box 1.14.0 起"
261+
262+
[`dns_mode`](#dns_mode) 使用的 DNS 服务器地址列表。
263+
264+
未设置时,sing-box 会按地址族在 [`address`](#address) 的第一个 IPv4/IPv6
265+
条目后面取下一个 IP 作为 DNS 服务器地址,并将流向这些推导地址的连接额外劫持到
266+
sing-box DNS 模块,等价于一条
267+
[`hijack-dns`](/zh/configuration/route/rule_action/#hijack-dns) 路由动作;这与此选项加入之前的行为一致。
268+
269+
设置后,将不再自动劫持;如仍需此行为,请显式配置
270+
[`hijack-dns`](/zh/configuration/route/rule_action/#hijack-dns) 路由规则。
271+
225272
#### gso
226273

227274
!!! failure "已在 sing-box 1.11.0 废弃"

0 commit comments

Comments
 (0)