Skip to content

Commit a8cd005

Browse files
committed
Add local ret direct address support for tun
1 parent 8413d12 commit a8cd005

File tree

6 files changed

+67
-32
lines changed

6 files changed

+67
-32
lines changed

docs/configuration/inbound/tun.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
---
2-
icon: material/alert-decagram
2+
icon: material/new-box
33
---
44

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

711
:material-delete-alert: [gso](#gso)
@@ -56,17 +60,19 @@ icon: material/alert-decagram
5660
"auto_route": true,
5761
"iproute2_table_index": 2022,
5862
"iproute2_rule_index": 9000,
59-
"auto_redirect": false,
63+
"auto_redirect": true,
6064
"auto_redirect_input_mark": "0x2023",
6165
"auto_redirect_output_mark": "0x2024",
66+
"local_redirect_address": [
67+
"10.0.7.1"
68+
],
6269
"strict_route": true,
6370
"route_address": [
6471
"0.0.0.0/1",
6572
"128.0.0.0/1",
6673
"::/1",
6774
"8000::/1"
6875
],
69-
7076
"route_exclude_address": [
7177
"192.168.0.0/16",
7278
"fc00::/7"
@@ -117,7 +123,6 @@ icon: material/alert-decagram
117123
"match_domain": []
118124
}
119125
},
120-
121126
// Deprecated
122127
"gso": false,
123128
"inet4_address": [
@@ -140,8 +145,8 @@ icon: material/alert-decagram
140145
"inet6_route_exclude_address": [
141146
"fc00::/7"
142147
],
143-
144-
... // Listen Fields
148+
...
149+
// Listen Fields
145150
}
146151
```
147152

@@ -273,6 +278,16 @@ Connection output mark used by `auto_redirect`.
273278

274279
`0x2024` is used by default.
275280

281+
#### local_redirect_address
282+
283+
!!! question "Since sing-box 1.12.0"
284+
285+
Local redirect addresses are loopback addresses to make TCP connections to the specified address connect to the original address.
286+
287+
Setting option value to `10.0.7.1` achieves the same behavior as SideStore/StosVPN.
288+
289+
When `auto_redirect` is enabled, the same behavior can be achieved for LAN devices (not just local) as a gateway.
290+
276291
#### strict_route
277292

278293
Enforce strict routing rules when `auto_route` is enabled:

docs/configuration/inbound/tun.zh.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
---
2-
icon: material/alert-decagram
2+
icon: material/new-box
33
---
44

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

711
:material-delete-alert: [gso](#gso)
@@ -59,6 +63,9 @@ icon: material/alert-decagram
5963
"auto_redirect": false,
6064
"auto_redirect_input_mark": "0x2023",
6165
"auto_redirect_output_mark": "0x2024",
66+
"local_redirect_address": [
67+
"10.0.7.1"
68+
],
6269
"strict_route": true,
6370
"route_address": [
6471
"0.0.0.0/1",
@@ -270,6 +277,16 @@ tun 接口的 IPv6 前缀。
270277

271278
默认使用 `0x2024`
272279

280+
#### local_redirect_address
281+
282+
!!! question "自 sing-box 1.12.0 起"
283+
284+
本地重定向地址是用于使指向指定地址的 TCP 连接连接到原始地址的环回地址。
285+
286+
将选项值设置为 `10.0.7.1` 可实现与 SideStore/StosVPN 相同的行为。
287+
288+
当启用 `auto_redirect` 时,可以作为网关为局域网设备(而不仅仅是本地)实现相同的行为。
289+
273290
#### strict_route
274291

275292
当启用 `auto_route` 时,强制执行严格的路由规则:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/sagernet/sing-shadowsocks v0.2.8
3535
github.com/sagernet/sing-shadowsocks2 v0.2.1
3636
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
37-
github.com/sagernet/sing-tun v0.6.6-0.20250428031943-0686f8c4f210
37+
github.com/sagernet/sing-tun v0.6.6-0.20250610080513-c30e60e95c09
3838
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88
3939
github.com/sagernet/smux v1.5.34-mod.2
4040
github.com/sagernet/tailscale v1.80.3-mod.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
180180
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
181181
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
182182
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
183-
github.com/sagernet/sing-tun v0.6.6-0.20250428031943-0686f8c4f210 h1:6H4BZaTqKI3YcDMyTV3E576LuJM4S4wY99xoq2T1ECw=
184-
github.com/sagernet/sing-tun v0.6.6-0.20250428031943-0686f8c4f210/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
183+
github.com/sagernet/sing-tun v0.6.6-0.20250610080513-c30e60e95c09 h1:8gS+1fPyq4tYw15MLsGWnYMlWq39O6hDIHWlrdwaCas=
184+
github.com/sagernet/sing-tun v0.6.6-0.20250610080513-c30e60e95c09/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
185185
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88 h1:0pVm8sPOel+BoiCddW3pV3cKDKEaSioVTYDdTSKjyFI=
186186
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88/go.mod h1:IL8Rr+EGwuqijszZkNrEFTQDKhilEpkqFqOlvdpS6/w=
187187
github.com/sagernet/smux v1.5.34-mod.2 h1:gkmBjIjlJ2zQKpLigOkFur5kBKdV6bNRoFu2WkltRQ4=

option/tun.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type TunInboundOptions struct {
2020
AutoRedirect bool `json:"auto_redirect,omitempty"`
2121
AutoRedirectInputMark FwMark `json:"auto_redirect_input_mark,omitempty"`
2222
AutoRedirectOutputMark FwMark `json:"auto_redirect_output_mark,omitempty"`
23+
LocalRedirectAddress badoption.Listable[netip.Addr] `json:"local_redirect_address,omitempty"`
2324
StrictRoute bool `json:"strict_route,omitempty"`
2425
RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
2526
RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`

protocol/tun/inbound.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,30 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
181181
logger: logger,
182182
inboundOptions: options.InboundOptions,
183183
tunOptions: tun.Options{
184-
Name: options.InterfaceName,
185-
MTU: tunMTU,
186-
Inet4Address: inet4Address,
187-
Inet6Address: inet6Address,
188-
AutoRoute: options.AutoRoute,
189-
IPRoute2TableIndex: tableIndex,
190-
IPRoute2RuleIndex: ruleIndex,
191-
AutoRedirectInputMark: inputMark,
192-
AutoRedirectOutputMark: outputMark,
193-
StrictRoute: options.StrictRoute,
194-
IncludeInterface: options.IncludeInterface,
195-
ExcludeInterface: options.ExcludeInterface,
196-
Inet4RouteAddress: inet4RouteAddress,
197-
Inet6RouteAddress: inet6RouteAddress,
198-
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
199-
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
200-
IncludeUID: includeUID,
201-
ExcludeUID: excludeUID,
202-
IncludeAndroidUser: options.IncludeAndroidUser,
203-
IncludePackage: options.IncludePackage,
204-
ExcludePackage: options.ExcludePackage,
205-
InterfaceMonitor: networkManager.InterfaceMonitor(),
184+
Name: options.InterfaceName,
185+
MTU: tunMTU,
186+
Inet4Address: inet4Address,
187+
Inet6Address: inet6Address,
188+
AutoRoute: options.AutoRoute,
189+
IPRoute2TableIndex: tableIndex,
190+
IPRoute2RuleIndex: ruleIndex,
191+
AutoRedirectInputMark: inputMark,
192+
AutoRedirectOutputMark: outputMark,
193+
Inet4LocalRedirectAddress: common.Filter(options.LocalRedirectAddress, netip.Addr.Is4),
194+
Inet6LocalRedirectAddress: common.Filter(options.LocalRedirectAddress, netip.Addr.Is6),
195+
StrictRoute: options.StrictRoute,
196+
IncludeInterface: options.IncludeInterface,
197+
ExcludeInterface: options.ExcludeInterface,
198+
Inet4RouteAddress: inet4RouteAddress,
199+
Inet6RouteAddress: inet6RouteAddress,
200+
Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
201+
Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
202+
IncludeUID: includeUID,
203+
ExcludeUID: excludeUID,
204+
IncludeAndroidUser: options.IncludeAndroidUser,
205+
IncludePackage: options.IncludePackage,
206+
ExcludePackage: options.ExcludePackage,
207+
InterfaceMonitor: networkManager.InterfaceMonitor(),
206208
},
207209
udpTimeout: udpTimeout,
208210
stack: options.Stack,

0 commit comments

Comments
 (0)