Skip to content

Commit 58025a0

Browse files
committed
Fix auto_redirect fallback rule
1 parent 99cad72 commit 58025a0

File tree

7 files changed

+102
-56
lines changed

7 files changed

+102
-56
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.12.18
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.12.17
1018

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

docs/configuration/inbound/tun.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
icon: material/new-box
33
---
44

5+
!!! quote "Changes in sing-box 1.12.18"
6+
7+
:material-plus: [auto_redirect_iproute2_fallback_rule_index](#auto_redirect_iproute2_fallback_rule_index)
8+
59
!!! quote "Changes in sing-box 1.12.0"
610

711
:material-plus: [loopback_address](#loopback_address)
@@ -63,6 +67,7 @@ icon: material/new-box
6367
"auto_redirect": true,
6468
"auto_redirect_input_mark": "0x2023",
6569
"auto_redirect_output_mark": "0x2024",
70+
"auto_redirect_iproute2_fallback_rule_index": 32768,
6671
"loopback_address": [
6772
"10.7.0.1"
6873
],
@@ -278,6 +283,17 @@ Connection output mark used by `auto_redirect`.
278283

279284
`0x2024` is used by default.
280285

286+
#### auto_redirect_iproute2_fallback_rule_index
287+
288+
!!! question "Since sing-box 1.12.18"
289+
290+
Linux iproute2 fallback rule index generated by `auto_redirect`.
291+
292+
This rule is checked after system default rules (32766: main, 32767: default),
293+
routing traffic to the sing-box table only when no route is found in system tables.
294+
295+
`32768` is used by default.
296+
281297
#### loopback_address
282298

283299
!!! question "Since sing-box 1.12.0"

docs/configuration/inbound/tun.zh.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
icon: material/new-box
33
---
44

5+
!!! quote "sing-box 1.12.18 中的更改"
6+
7+
:material-plus: [auto_redirect_iproute2_fallback_rule_index](#auto_redirect_iproute2_fallback_rule_index)
8+
59
!!! quote "sing-box 1.12.0 中的更改"
610

711
:material-plus: [loopback_address](#loopback_address)
@@ -63,6 +67,7 @@ icon: material/new-box
6367
"auto_redirect": true,
6468
"auto_redirect_input_mark": "0x2023",
6569
"auto_redirect_output_mark": "0x2024",
70+
"auto_redirect_iproute2_fallback_rule_index": 32768,
6671
"loopback_address": [
6772
"10.7.0.1"
6873
],
@@ -277,6 +282,17 @@ tun 接口的 IPv6 前缀。
277282

278283
默认使用 `0x2024`
279284

285+
#### auto_redirect_iproute2_fallback_rule_index
286+
287+
!!! question "自 sing-box 1.12.18 起"
288+
289+
`auto_redirect` 生成的 iproute2 回退规则索引。
290+
291+
此规则在系统默认规则(32766: main,32767: default)之后检查,
292+
仅当系统路由表中未找到路由时才将流量路由到 sing-box 路由表。
293+
294+
默认使用 `32768`
295+
280296
#### loopback_address
281297

282298
!!! question "自 sing-box 1.12.0 起"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/sagernet/sing-shadowsocks v0.2.8
3434
github.com/sagernet/sing-shadowsocks2 v0.2.1
3535
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
36-
github.com/sagernet/sing-tun v0.7.8
36+
github.com/sagernet/sing-tun v0.7.9
3737
github.com/sagernet/sing-vmess v0.2.7
3838
github.com/sagernet/smux v1.5.50-sing-box-mod.1
3939
github.com/sagernet/tailscale v1.80.3-sing-box-1.12-mod.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
177177
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
178178
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
179179
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
180-
github.com/sagernet/sing-tun v0.7.8 h1:v9GYPL6V1ZiViqdoRYOy1MPE9b2E6SP0YYWCIGV5fGc=
181-
github.com/sagernet/sing-tun v0.7.8/go.mod h1:pUEjh9YHQ2gJT6Lk0TYDklh3WJy7lz+848vleGM3JPM=
180+
github.com/sagernet/sing-tun v0.7.9 h1:kcEFA0Xa+OVcrBGnlIl33+SrW/vuB3jtOw0HQNOSJzc=
181+
github.com/sagernet/sing-tun v0.7.9/go.mod h1:pUEjh9YHQ2gJT6Lk0TYDklh3WJy7lz+848vleGM3JPM=
182182
github.com/sagernet/sing-vmess v0.2.7 h1:2ee+9kO0xW5P4mfe6TYVWf9VtY8k1JhNysBqsiYj0sk=
183183
github.com/sagernet/sing-vmess v0.2.7/go.mod h1:5aYoOtYksAyS0NXDm0qKeTYW1yoE1bJVcv+XLcVoyJs=
184184
github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1hzcbp6kSkkyQ478=

option/tun.go

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,34 @@ 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-
LoopbackAddress badoption.Listable[netip.Addr] `json:"loopback_address,omitempty"`
24-
StrictRoute bool `json:"strict_route,omitempty"`
25-
RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
26-
RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`
27-
RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
28-
RouteExcludeAddressSet badoption.Listable[string] `json:"route_exclude_address_set,omitempty"`
29-
IncludeInterface badoption.Listable[string] `json:"include_interface,omitempty"`
30-
ExcludeInterface badoption.Listable[string] `json:"exclude_interface,omitempty"`
31-
IncludeUID badoption.Listable[uint32] `json:"include_uid,omitempty"`
32-
IncludeUIDRange badoption.Listable[string] `json:"include_uid_range,omitempty"`
33-
ExcludeUID badoption.Listable[uint32] `json:"exclude_uid,omitempty"`
34-
ExcludeUIDRange badoption.Listable[string] `json:"exclude_uid_range,omitempty"`
35-
IncludeAndroidUser badoption.Listable[int] `json:"include_android_user,omitempty"`
36-
IncludePackage badoption.Listable[string] `json:"include_package,omitempty"`
37-
ExcludePackage badoption.Listable[string] `json:"exclude_package,omitempty"`
38-
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
39-
Stack string `json:"stack,omitempty"`
40-
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+
AutoRedirectIPRoute2FallbackRuleIndex int `json:"auto_redirect_iproute2_fallback_rule_index,omitempty"`
24+
LoopbackAddress badoption.Listable[netip.Addr] `json:"loopback_address,omitempty"`
25+
StrictRoute bool `json:"strict_route,omitempty"`
26+
RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
27+
RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`
28+
RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
29+
RouteExcludeAddressSet badoption.Listable[string] `json:"route_exclude_address_set,omitempty"`
30+
IncludeInterface badoption.Listable[string] `json:"include_interface,omitempty"`
31+
ExcludeInterface badoption.Listable[string] `json:"exclude_interface,omitempty"`
32+
IncludeUID badoption.Listable[uint32] `json:"include_uid,omitempty"`
33+
IncludeUIDRange badoption.Listable[string] `json:"include_uid_range,omitempty"`
34+
ExcludeUID badoption.Listable[uint32] `json:"exclude_uid,omitempty"`
35+
ExcludeUIDRange badoption.Listable[string] `json:"exclude_uid_range,omitempty"`
36+
IncludeAndroidUser badoption.Listable[int] `json:"include_android_user,omitempty"`
37+
IncludePackage badoption.Listable[string] `json:"include_package,omitempty"`
38+
ExcludePackage badoption.Listable[string] `json:"exclude_package,omitempty"`
39+
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
40+
Stack string `json:"stack,omitempty"`
41+
Platform *TunPlatformOptions `json:"platform,omitempty"`
4142
InboundOptions
4243

4344
// Deprecated: removed

protocol/tun/inbound.go

Lines changed: 31 additions & 26 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.AutoRedirectIPRoute2FallbackRuleIndex
178+
if autoRedirectFallbackRuleIndex == 0 {
179+
autoRedirectFallbackRuleIndex = tun.DefaultIPRoute2AutoRedirectFallbackRuleIndex
180+
}
177181
inputMark := uint32(options.AutoRedirectInputMark)
178182
if inputMark == 0 {
179183
inputMark = tun.DefaultAutoRedirectInputMark
@@ -192,32 +196,33 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
192196
logger: logger,
193197
inboundOptions: options.InboundOptions,
194198
tunOptions: tun.Options{
195-
Name: options.InterfaceName,
196-
MTU: tunMTU,
197-
GSO: enableGSO,
198-
Inet4Address: inet4Address,
199-
Inet6Address: inet6Address,
200-
AutoRoute: options.AutoRoute,
201-
IPRoute2TableIndex: tableIndex,
202-
IPRoute2RuleIndex: ruleIndex,
203-
AutoRedirectInputMark: inputMark,
204-
AutoRedirectOutputMark: outputMark,
205-
Inet4LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is4),
206-
Inet6LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is6),
207-
StrictRoute: options.StrictRoute,
208-
IncludeInterface: options.IncludeInterface,
209-
ExcludeInterface: options.ExcludeInterface,
210-
Inet4RouteAddress: inet4RouteAddress,
211-
Inet6RouteAddress: inet6RouteAddress,
212-
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
213-
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
214-
IncludeUID: includeUID,
215-
ExcludeUID: excludeUID,
216-
IncludeAndroidUser: options.IncludeAndroidUser,
217-
IncludePackage: options.IncludePackage,
218-
ExcludePackage: options.ExcludePackage,
219-
InterfaceMonitor: networkManager.InterfaceMonitor(),
220-
EXP_MultiPendingPackets: multiPendingPackets,
199+
Name: options.InterfaceName,
200+
MTU: tunMTU,
201+
GSO: enableGSO,
202+
Inet4Address: inet4Address,
203+
Inet6Address: inet6Address,
204+
AutoRoute: options.AutoRoute,
205+
IPRoute2TableIndex: tableIndex,
206+
IPRoute2RuleIndex: ruleIndex,
207+
IPRoute2AutoRedirectFallbackRuleIndex: autoRedirectFallbackRuleIndex,
208+
AutoRedirectInputMark: inputMark,
209+
AutoRedirectOutputMark: outputMark,
210+
Inet4LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is4),
211+
Inet6LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is6),
212+
StrictRoute: options.StrictRoute,
213+
IncludeInterface: options.IncludeInterface,
214+
ExcludeInterface: options.ExcludeInterface,
215+
Inet4RouteAddress: inet4RouteAddress,
216+
Inet6RouteAddress: inet6RouteAddress,
217+
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
218+
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
219+
IncludeUID: includeUID,
220+
ExcludeUID: excludeUID,
221+
IncludeAndroidUser: options.IncludeAndroidUser,
222+
IncludePackage: options.IncludePackage,
223+
ExcludePackage: options.ExcludePackage,
224+
InterfaceMonitor: networkManager.InterfaceMonitor(),
225+
EXP_MultiPendingPackets: multiPendingPackets,
221226
},
222227
udpTimeout: udpTimeout,
223228
stack: options.Stack,

0 commit comments

Comments
 (0)