Skip to content

Commit 79bab39

Browse files
committed
Fix auto_redirect fallback rule
1 parent a4d5d59 commit 79bab39

File tree

7 files changed

+105
-65
lines changed

7 files changed

+105
-65
lines changed

docs/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ icon: material/alert-decagram
44

55
#### 1.13.0-beta.7
66

7+
* Add fallback routing rule for `auto_redirect` **1**
78
* Fixes and improvements
89

10+
**1**:
11+
12+
Adds a fallback iproute2 rule checked after system default rules (32766: main, 32767: default),
13+
ensuring traffic is routed to the sing-box table when no route is found in system tables.
14+
15+
The rule index can be customized via `auto_redirect_iproute2_fallback_rule_index` (default: 32768).
16+
917
#### 1.13.0-beta.6
1018

1119
* Update uTLS to v1.8.2 **1**

docs/configuration/inbound/tun.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ icon: material/new-box
66

77
:material-plus: [auto_redirect_reset_mark](#auto_redirect_reset_mark)
88
:material-plus: [auto_redirect_nfqueue](#auto_redirect_nfqueue)
9-
:material-plus: [exclude_mptcp](#exclude_mptcp)
9+
:material-plus: [exclude_mptcp](#exclude_mptcp)
10+
:material-plus: [auto_redirect_iproute2_fallback_rule_index](#auto_redirect_iproute2_fallback_rule_index)
1011

1112
!!! quote "Changes in sing-box 1.12.0"
1213

@@ -71,6 +72,7 @@ icon: material/new-box
7172
"auto_redirect_output_mark": "0x2024",
7273
"auto_redirect_reset_mark": "0x2025",
7374
"auto_redirect_nfqueue": 100,
75+
"auto_redirect_iproute2_fallback_rule_index": 32768,
7476
"exclude_mptcp": false,
7577
"loopback_address": [
7678
"10.7.0.1"
@@ -303,6 +305,17 @@ NFQueue number used by `auto_redirect` pre-matching.
303305

304306
`100` is used by default.
305307

308+
#### auto_redirect_iproute2_fallback_rule_index
309+
310+
!!! question "Since sing-box 1.12.18"
311+
312+
Linux iproute2 fallback rule index generated by `auto_redirect`.
313+
314+
This rule is checked after system default rules (32766: main, 32767: default),
315+
routing traffic to the sing-box table only when no route is found in system tables.
316+
317+
`32768` is used by default.
318+
306319
#### exclude_mptcp
307320

308321
!!! question "Since sing-box 1.13.0"

docs/configuration/inbound/tun.zh.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ icon: material/new-box
66

77
:material-plus: [auto_redirect_reset_mark](#auto_redirect_reset_mark)
88
:material-plus: [auto_redirect_nfqueue](#auto_redirect_nfqueue)
9-
:material-plus: [exclude_mptcp](#exclude_mptcp)
9+
:material-plus: [exclude_mptcp](#exclude_mptcp)
10+
:material-plus: [auto_redirect_iproute2_fallback_rule_index](#auto_redirect_iproute2_fallback_rule_index)
1011

1112
!!! quote "sing-box 1.12.0 中的更改"
1213

@@ -71,6 +72,7 @@ icon: material/new-box
7172
"auto_redirect_output_mark": "0x2024",
7273
"auto_redirect_reset_mark": "0x2025",
7374
"auto_redirect_nfqueue": 100,
75+
"auto_redirect_iproute2_fallback_rule_index": 32768,
7476
"exclude_mptcp": false,
7577
"loopback_address": [
7678
"10.7.0.1"
@@ -302,13 +304,24 @@ tun 接口的 IPv6 前缀。
302304

303305
默认使用 `100`
304306

307+
#### auto_redirect_iproute2_fallback_rule_index
308+
309+
!!! question "自 sing-box 1.12.18 起"
310+
311+
`auto_redirect` 生成的 iproute2 回退规则索引。
312+
313+
此规则在系统默认规则(32766: main,32767: default)之后检查,
314+
仅当系统路由表中未找到路由时才将流量路由到 sing-box 路由表。
315+
316+
默认使用 `32768`
317+
305318
#### exclude_mptcp
306319

307320
!!! question "自 sing-box 1.13.0 起"
308321

309322
!!! quote ""
310323

311-
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
324+
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
312325

313326
由于协议限制,MPTCP 无法被透明代理。
314327

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/sagernet/sing-shadowsocks v0.2.8
4040
github.com/sagernet/sing-shadowsocks2 v0.2.1
4141
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
42-
github.com/sagernet/sing-tun v0.8.0-beta.14
42+
github.com/sagernet/sing-tun v0.8.0-beta.15
4343
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1
4444
github.com/sagernet/smux v1.5.50-sing-box-mod.1
4545
github.com/sagernet/tailscale v1.92.4-sing-box-1.13-mod.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
222222
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
223223
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
224224
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
225-
github.com/sagernet/sing-tun v0.8.0-beta.14 h1:LlIhiynP1aggwDYeHDa+JeJAuptYy+OtQU+hPiHTXbY=
226-
github.com/sagernet/sing-tun v0.8.0-beta.14/go.mod h1:+HAK/y9GZljdT0KYKMYDR8MjjqnqDDQZYp5ZZQoRzS8=
225+
github.com/sagernet/sing-tun v0.8.0-beta.15 h1:R5PMHCXuSUs5g6aor4UhxfaIx+t6yofAYB9YAea7UZM=
226+
github.com/sagernet/sing-tun v0.8.0-beta.15/go.mod h1:+HAK/y9GZljdT0KYKMYDR8MjjqnqDDQZYp5ZZQoRzS8=
227227
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1 h1:aSwUNYUkVyVvdmBSufR8/nRFonwJeKSIROxHcm5br9o=
228228
github.com/sagernet/sing-vmess v0.2.8-0.20250909125414-3aed155119a1/go.mod h1:P11scgTxMxVVQ8dlM27yNm3Cro40mD0+gHbnqrNGDuY=
229229
github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1hzcbp6kSkkyQ478=

option/tun.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,37 @@ import (
1111
)
1212

1313
type TunInboundOptions struct {
14-
InterfaceName string `json:"interface_name,omitempty"`
15-
MTU uint32 `json:"mtu,omitempty"`
16-
Address badoption.Listable[netip.Prefix] `json:"address,omitempty"`
17-
AutoRoute bool `json:"auto_route,omitempty"`
18-
IPRoute2TableIndex int `json:"iproute2_table_index,omitempty"`
19-
IPRoute2RuleIndex int `json:"iproute2_rule_index,omitempty"`
20-
AutoRedirect bool `json:"auto_redirect,omitempty"`
21-
AutoRedirectInputMark FwMark `json:"auto_redirect_input_mark,omitempty"`
22-
AutoRedirectOutputMark FwMark `json:"auto_redirect_output_mark,omitempty"`
23-
AutoRedirectResetMark FwMark `json:"auto_redirect_reset_mark,omitempty"`
24-
AutoRedirectNFQueue uint16 `json:"auto_redirect_nfqueue,omitempty"`
25-
ExcludeMPTCP bool `json:"exclude_mptcp,omitempty"`
26-
LoopbackAddress badoption.Listable[netip.Addr] `json:"loopback_address,omitempty"`
27-
StrictRoute bool `json:"strict_route,omitempty"`
28-
RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
29-
RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`
30-
RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
31-
RouteExcludeAddressSet badoption.Listable[string] `json:"route_exclude_address_set,omitempty"`
32-
IncludeInterface badoption.Listable[string] `json:"include_interface,omitempty"`
33-
ExcludeInterface badoption.Listable[string] `json:"exclude_interface,omitempty"`
34-
IncludeUID badoption.Listable[uint32] `json:"include_uid,omitempty"`
35-
IncludeUIDRange badoption.Listable[string] `json:"include_uid_range,omitempty"`
36-
ExcludeUID badoption.Listable[uint32] `json:"exclude_uid,omitempty"`
37-
ExcludeUIDRange badoption.Listable[string] `json:"exclude_uid_range,omitempty"`
38-
IncludeAndroidUser badoption.Listable[int] `json:"include_android_user,omitempty"`
39-
IncludePackage badoption.Listable[string] `json:"include_package,omitempty"`
40-
ExcludePackage badoption.Listable[string] `json:"exclude_package,omitempty"`
41-
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
42-
Stack string `json:"stack,omitempty"`
43-
Platform *TunPlatformOptions `json:"platform,omitempty"`
14+
InterfaceName string `json:"interface_name,omitempty"`
15+
MTU uint32 `json:"mtu,omitempty"`
16+
Address badoption.Listable[netip.Prefix] `json:"address,omitempty"`
17+
AutoRoute bool `json:"auto_route,omitempty"`
18+
IPRoute2TableIndex int `json:"iproute2_table_index,omitempty"`
19+
IPRoute2RuleIndex int `json:"iproute2_rule_index,omitempty"`
20+
AutoRedirect bool `json:"auto_redirect,omitempty"`
21+
AutoRedirectInputMark FwMark `json:"auto_redirect_input_mark,omitempty"`
22+
AutoRedirectOutputMark FwMark `json:"auto_redirect_output_mark,omitempty"`
23+
AutoRedirectResetMark FwMark `json:"auto_redirect_reset_mark,omitempty"`
24+
AutoRedirectNFQueue uint16 `json:"auto_redirect_nfqueue,omitempty"`
25+
AutoRedirectFallbackRuleIndex int `json:"auto_redirect_iproute2_fallback_rule_index,omitempty"`
26+
ExcludeMPTCP bool `json:"exclude_mptcp,omitempty"`
27+
LoopbackAddress badoption.Listable[netip.Addr] `json:"loopback_address,omitempty"`
28+
StrictRoute bool `json:"strict_route,omitempty"`
29+
RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
30+
RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`
31+
RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
32+
RouteExcludeAddressSet badoption.Listable[string] `json:"route_exclude_address_set,omitempty"`
33+
IncludeInterface badoption.Listable[string] `json:"include_interface,omitempty"`
34+
ExcludeInterface badoption.Listable[string] `json:"exclude_interface,omitempty"`
35+
IncludeUID badoption.Listable[uint32] `json:"include_uid,omitempty"`
36+
IncludeUIDRange badoption.Listable[string] `json:"include_uid_range,omitempty"`
37+
ExcludeUID badoption.Listable[uint32] `json:"exclude_uid,omitempty"`
38+
ExcludeUIDRange badoption.Listable[string] `json:"exclude_uid_range,omitempty"`
39+
IncludeAndroidUser badoption.Listable[int] `json:"include_android_user,omitempty"`
40+
IncludePackage badoption.Listable[string] `json:"include_package,omitempty"`
41+
ExcludePackage badoption.Listable[string] `json:"exclude_package,omitempty"`
42+
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
43+
Stack string `json:"stack,omitempty"`
44+
Platform *TunPlatformOptions `json:"platform,omitempty"`
4445
InboundOptions
4546

4647
// Deprecated: removed

protocol/tun/inbound.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
174174
if ruleIndex == 0 {
175175
ruleIndex = tun.DefaultIPRoute2RuleIndex
176176
}
177+
autoRedirectFallbackRuleIndex := options.AutoRedirectFallbackRuleIndex
178+
if autoRedirectFallbackRuleIndex == 0 {
179+
autoRedirectFallbackRuleIndex = tun.DefaultIPRoute2AutoRedirectFallbackRuleIndex
180+
}
177181
inputMark := uint32(options.AutoRedirectInputMark)
178182
if inputMark == 0 {
179183
inputMark = tun.DefaultAutoRedirectInputMark
@@ -200,35 +204,36 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
200204
logger: logger,
201205
inboundOptions: options.InboundOptions,
202206
tunOptions: tun.Options{
203-
Name: options.InterfaceName,
204-
MTU: tunMTU,
205-
GSO: enableGSO,
206-
Inet4Address: inet4Address,
207-
Inet6Address: inet6Address,
208-
AutoRoute: options.AutoRoute,
209-
IPRoute2TableIndex: tableIndex,
210-
IPRoute2RuleIndex: ruleIndex,
211-
AutoRedirectInputMark: inputMark,
212-
AutoRedirectOutputMark: outputMark,
213-
AutoRedirectResetMark: resetMark,
214-
AutoRedirectNFQueue: nfQueue,
215-
ExcludeMPTCP: options.ExcludeMPTCP,
216-
Inet4LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is4),
217-
Inet6LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is6),
218-
StrictRoute: options.StrictRoute,
219-
IncludeInterface: options.IncludeInterface,
220-
ExcludeInterface: options.ExcludeInterface,
221-
Inet4RouteAddress: inet4RouteAddress,
222-
Inet6RouteAddress: inet6RouteAddress,
223-
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
224-
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
225-
IncludeUID: includeUID,
226-
ExcludeUID: excludeUID,
227-
IncludeAndroidUser: options.IncludeAndroidUser,
228-
IncludePackage: options.IncludePackage,
229-
ExcludePackage: options.ExcludePackage,
230-
InterfaceMonitor: networkManager.InterfaceMonitor(),
231-
EXP_MultiPendingPackets: multiPendingPackets,
207+
Name: options.InterfaceName,
208+
MTU: tunMTU,
209+
GSO: enableGSO,
210+
Inet4Address: inet4Address,
211+
Inet6Address: inet6Address,
212+
AutoRoute: options.AutoRoute,
213+
IPRoute2TableIndex: tableIndex,
214+
IPRoute2RuleIndex: ruleIndex,
215+
IPRoute2AutoRedirectFallbackRuleIndex: autoRedirectFallbackRuleIndex,
216+
AutoRedirectInputMark: inputMark,
217+
AutoRedirectOutputMark: outputMark,
218+
AutoRedirectResetMark: resetMark,
219+
AutoRedirectNFQueue: nfQueue,
220+
ExcludeMPTCP: options.ExcludeMPTCP,
221+
Inet4LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is4),
222+
Inet6LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is6),
223+
StrictRoute: options.StrictRoute,
224+
IncludeInterface: options.IncludeInterface,
225+
ExcludeInterface: options.ExcludeInterface,
226+
Inet4RouteAddress: inet4RouteAddress,
227+
Inet6RouteAddress: inet6RouteAddress,
228+
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
229+
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
230+
IncludeUID: includeUID,
231+
ExcludeUID: excludeUID,
232+
IncludeAndroidUser: options.IncludeAndroidUser,
233+
IncludePackage: options.IncludePackage,
234+
ExcludePackage: options.ExcludePackage,
235+
InterfaceMonitor: networkManager.InterfaceMonitor(),
236+
EXP_MultiPendingPackets: multiPendingPackets,
232237
},
233238
udpTimeout: udpTimeout,
234239
stack: options.Stack,

0 commit comments

Comments
 (0)