Skip to content

Commit 41f4b71

Browse files
committed
tun: Set address sets to routes
1 parent eb4a184 commit 41f4b71

File tree

10 files changed

+212
-89
lines changed

10 files changed

+212
-89
lines changed

docs/configuration/inbound/tun.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ icon: material/alert-decagram
44

55
!!! quote "Changes in sing-box 1.11.0"
66

7-
:material-delete-alert: [gso](#gso)
7+
:material-delete-alert: [gso](#gso)
8+
:material-alert-decagram: [route_address_set](#stack)
9+
:material-alert-decagram: [route_exclude_address_set](#stack)
810

911
!!! quote "Changes in sing-box 1.10.0"
1012

@@ -248,15 +250,15 @@ use [VPNHotspot](https://github.com/Mygod/VPNHotspot).
248250

249251
!!! question "Since sing-box 1.10.0"
250252

251-
Connection input mark used by `route_address_set` and `route_exclude_address_set`.
253+
Connection input mark used by `route[_exclude]_address_set` with `auto_redirect`.
252254

253255
`0x2023` is used by default.
254256

255257
#### auto_redirect_output_mark
256258

257259
!!! question "Since sing-box 1.10.0"
258260

259-
Connection output mark used by `route_address_set` and `route_exclude_address_set`.
261+
Connection input mark used by `route[_exclude]_address_set` with `auto_redirect`.
260262

261263
`0x2024` is used by default.
262264

@@ -329,29 +331,55 @@ Exclude custom routes when `auto_route` is enabled.
329331

330332
#### route_address_set
331333

332-
!!! question "Since sing-box 1.10.0"
334+
=== "With `auto_redirect` enabled"
333335

334-
!!! quote ""
336+
!!! question "Since sing-box 1.10.0"
335337

336-
Only supported on Linux with nftables and requires `auto_route` and `auto_redirect` enabled.
338+
!!! quote ""
339+
340+
Only supported on Linux with nftables and requires `auto_route` and `auto_redirect` enabled.
341+
342+
Add the destination IP CIDR rules in the specified rule-sets to the firewall.
343+
Unmatched traffic will bypass the sing-box routes.
344+
345+
Conflict with `route.default_mark` and `[dialOptions].routing_mark`.
337346

338-
Add the destination IP CIDR rules in the specified rule-sets to the firewall.
339-
Unmatched traffic will bypass the sing-box routes.
347+
=== "Without `auto_redirect` enabled"
340348

341-
Conflict with `route.default_mark` and `[dialOptions].routing_mark`.
349+
!!! question "Since sing-box 1.11.0"
350+
351+
Add the destination IP CIDR rules in the specified rule-sets to routes, equivalent to adding to `route_address`.
352+
Unmatched traffic will bypass the sing-box routes.
353+
354+
Note that it **doesn't work on the Android graphical client** due to
355+
the Android VpnService not being able to handle a large number of routes (DeadSystemException),
356+
but otherwise it works fine on all command line clients and Apple platforms.
342357

343358
#### route_exclude_address_set
344359

345-
!!! question "Since sing-box 1.10.0"
360+
=== "With `auto_redirect` enabled"
346361

347-
!!! quote ""
362+
!!! question "Since sing-box 1.10.0"
363+
364+
!!! quote ""
348365

349366
Only supported on Linux with nftables and requires `auto_route` and `auto_redirect` enabled.
350367

351-
Add the destination IP CIDR rules in the specified rule-sets to the firewall.
352-
Matched traffic will bypass the sing-box routes.
368+
Add the destination IP CIDR rules in the specified rule-sets to the firewall.
369+
Matched traffic will bypass the sing-box routes.
370+
371+
Conflict with `route.default_mark` and `[dialOptions].routing_mark`.
372+
373+
=== "Without `auto_redirect` enabled"
374+
375+
!!! question "Since sing-box 1.11.0"
376+
377+
Add the destination IP CIDR rules in the specified rule-sets to routes, equivalent to adding to `route_exclude_address`.
378+
Matched traffic will bypass the sing-box routes.
353379

354-
Conflict with `route.default_mark` and `[dialOptions].routing_mark`.
380+
Note that it **doesn't work on the Android graphical client** due to
381+
the Android VpnService not being able to handle a large number of routes (DeadSystemException),
382+
but otherwise it works fine on all command line clients and Apple platforms.
355383

356384
#### endpoint_independent_nat
357385

docs/configuration/inbound/tun.zh.md

+39-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ icon: material/alert-decagram
44

55
!!! quote "sing-box 1.11.0 中的更改"
66

7-
:material-delete-alert: [gso](#gso)
7+
:material-delete-alert: [gso](#gso)
8+
:material-alert-decagram: [route_address_set](#stack)
9+
:material-alert-decagram: [route_exclude_address_set](#stack)
810

911
!!! quote "sing-box 1.10.0 中的更改"
1012

@@ -329,29 +331,53 @@ tun 接口的 IPv6 前缀。
329331

330332
#### route_address_set
331333

332-
!!! question "自 sing-box 1.10.0 起"
334+
=== "`auto_redirect` 已启用"
333335

334-
!!! quote ""
336+
!!! question "自 sing-box 1.10.0 起"
337+
338+
!!! quote ""
339+
340+
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
341+
342+
将指定规则集中的目标 IP CIDR 规则添加到防火墙。
343+
不匹配的流量将绕过 sing-box 路由。
344+
345+
与 `route.default_mark` 和 `[dialOptions].routing_mark` 冲突。
335346

336-
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
347+
=== "`auto_redirect` 未启用"
337348

338-
将指定规则集中的目标 IP CIDR 规则添加到防火墙。
339-
不匹配的流量将绕过 sing-box 路由。
349+
!!! question "自 sing-box 1.11.0 起"
340350

341-
`route.default_mark``[dialOptions].routing_mark` 冲突。
351+
将指定规则集中的目标 IP CIDR 规则添加到路由,相当于添加到 `route_address`。
352+
不匹配的流量将绕过 sing-box 路由。
353+
354+
请注意,由于 Android VpnService 无法处理大量路由(DeadSystemException),
355+
因此它**在 Android 图形客户端上不起作用**,但除此之外,它在所有命令行客户端和 Apple 平台上都可以正常工作。
342356

343357
#### route_exclude_address_set
344358

345-
!!! question "自 sing-box 1.10.0 起"
359+
=== "`auto_redirect` 已启用"
346360

347-
!!! quote ""
361+
!!! question "自 sing-box 1.10.0 起"
362+
363+
!!! quote ""
364+
365+
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
366+
367+
将指定规则集中的目标 IP CIDR 规则添加到防火墙。
368+
匹配的流量将绕过 sing-box 路由。
369+
370+
与 `route.default_mark` 和 `[dialOptions].routing_mark` 冲突。
371+
372+
=== "`auto_redirect` 未启用"
348373

349-
仅支持 Linux,且需要 nftables,`auto_route` 和 `auto_redirect` 已启用。
374+
!!! question "自 sing-box 1.11.0 起"
350375

351-
将指定规则集中的目标 IP CIDR 规则添加到防火墙
352-
匹配的流量将绕过 sing-box 路由。
376+
将指定规则集中的目标 IP CIDR 规则添加到路由,相当于添加到 `route_exclude_address`
377+
匹配的流量将绕过 sing-box 路由。
353378

354-
`route.default_mark``[dialOptions].routing_mark` 冲突。
379+
请注意,由于 Android VpnService 无法处理大量路由(DeadSystemException),
380+
因此它**在 Android 图形客户端上不起作用**,但除此之外,它在所有命令行客户端和 Apple 平台上都可以正常工作。
355381

356382
#### endpoint_independent_nat
357383

experimental/libbox/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func (s *platformInterfaceStub) OpenTun(options *tun.Options, platformOptions op
6666
return nil, os.ErrInvalid
6767
}
6868

69+
func (s *platformInterfaceStub) UpdateRouteOptions(options *tun.Options, platformInterface option.TunPlatformOptions) error {
70+
return os.ErrInvalid
71+
}
72+
6973
func (s *platformInterfaceStub) UsePlatformDefaultInterfaceMonitor() bool {
7074
return true
7175
}

experimental/libbox/platform.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type PlatformInterface interface {
99
UsePlatformAutoDetectInterfaceControl() bool
1010
AutoDetectInterfaceControl(fd int32) error
1111
OpenTun(options TunOptions) (int32, error)
12+
UpdateRouteOptions(options TunOptions) error
1213
WriteLog(message string)
1314
UseProcFS() bool
1415
FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (int32, error)

experimental/libbox/platform/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Interface interface {
1313
UsePlatformAutoDetectInterfaceControl() bool
1414
AutoDetectInterfaceControl(fd int) error
1515
OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error)
16+
UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error
1617
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
1718
Interfaces() ([]adapter.NetworkInterface, error)
1819
SetUnderlyingNetworks(networks []adapter.NetworkInterface) error

experimental/libbox/service.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ func (w *platformInterfaceWrapper) AutoDetectInterfaceControl(fd int) error {
148148

149149
func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) {
150150
if len(options.IncludeUID) > 0 || len(options.ExcludeUID) > 0 {
151-
return nil, E.New("android: unsupported uid options")
151+
return nil, E.New("platform: unsupported uid options")
152152
}
153153
if len(options.IncludeAndroidUser) > 0 {
154-
return nil, E.New("android: unsupported android_user option")
154+
return nil, E.New("platform: unsupported android_user option")
155155
}
156156
routeRanges, err := options.BuildAutoRouteRanges(true)
157157
if err != nil {
@@ -174,6 +174,20 @@ func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions
174174
return tun.New(*options)
175175
}
176176

177+
func (w *platformInterfaceWrapper) UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error {
178+
if len(options.IncludeUID) > 0 || len(options.ExcludeUID) > 0 {
179+
return E.New("android: unsupported uid options")
180+
}
181+
if len(options.IncludeAndroidUser) > 0 {
182+
return E.New("android: unsupported android_user option")
183+
}
184+
routeRanges, err := options.BuildAutoRouteRanges(true)
185+
if err != nil {
186+
return err
187+
}
188+
return w.iif.UpdateRouteOptions(&tunOptions{options, routeRanges, platformOptions})
189+
}
190+
177191
func (w *platformInterfaceWrapper) CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor {
178192
return &platformDefaultInterfaceMonitor{
179193
platformInterfaceWrapper: w,

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/sagernet/sing-shadowsocks v0.2.7
3434
github.com/sagernet/sing-shadowsocks2 v0.2.0
3535
github.com/sagernet/sing-shadowtls v0.2.0-alpha.2
36-
github.com/sagernet/sing-tun v0.6.0-beta.6
36+
github.com/sagernet/sing-tun v0.6.0-beta.7
3737
github.com/sagernet/sing-vmess v0.2.0-beta.1
3838
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7
3939
github.com/sagernet/utls v1.6.7

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.0 h1:wpZNs6wKnR7mh1wV9OHwOyUr21VkS3wK
133133
github.com/sagernet/sing-shadowsocks2 v0.2.0/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
134134
github.com/sagernet/sing-shadowtls v0.2.0-alpha.2 h1:RPrpgAdkP5td0vLfS5ldvYosFjSsZtRPxiyLV6jyKg0=
135135
github.com/sagernet/sing-shadowtls v0.2.0-alpha.2/go.mod h1:0j5XlzKxaWRIEjc1uiSKmVoWb0k+L9QgZVb876+thZA=
136-
github.com/sagernet/sing-tun v0.6.0-beta.6 h1:xaIHoH78MqTSvZqQ4SQto8pC1A+X4qXReDRNaC8DQeI=
137-
github.com/sagernet/sing-tun v0.6.0-beta.6/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
136+
github.com/sagernet/sing-tun v0.6.0-beta.7 h1:FCSX8oGBqb0H57AAvfGeeH/jMGYWCOg6XWkN/oeES+0=
137+
github.com/sagernet/sing-tun v0.6.0-beta.7/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
138138
github.com/sagernet/sing-vmess v0.2.0-beta.1 h1:5sXQ23uwNlZuDvygzi0dFtnG0Csm/SNqTjAHXJkpuj4=
139139
github.com/sagernet/sing-vmess v0.2.0-beta.1/go.mod h1:fLyE1emIcvQ5DV8reFWnufquZ7MkCSYM5ThodsR9NrQ=
140140
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 h1:DImB4lELfQhplLTxeq2z31Fpv8CQqqrUwTbrIRumZqQ=

0 commit comments

Comments
 (0)