Skip to content

Commit d9e105f

Browse files
committed
auto-redirect: Add route address set support for nftables
1 parent 85fe25a commit d9e105f

File tree

8 files changed

+566
-251
lines changed

8 files changed

+566
-251
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ require (
66
github.com/fsnotify/fsnotify v1.7.0
77
github.com/go-ole/go-ole v1.3.0
88
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f
9-
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba
9+
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
1010
github.com/sagernet/nftables v0.3.0-beta.2
1111
github.com/sagernet/sing v0.5.0-alpha.9
1212
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
13+
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
1314
golang.org/x/net v0.26.0
1415
golang.org/x/sys v0.21.0
1516
)
1617

1718
require (
1819
github.com/google/btree v1.1.2 // indirect
19-
github.com/google/go-cmp v0.5.9 // indirect
20+
github.com/google/go-cmp v0.6.0 // indirect
2021
github.com/josharian/native v1.1.0 // indirect
2122
github.com/mdlayher/netlink v1.7.2 // indirect
2223
github.com/mdlayher/socket v0.4.1 // indirect
2324
github.com/vishvananda/netns v0.0.4 // indirect
24-
golang.org/x/sync v0.1.0 // indirect
25+
golang.org/x/sync v0.7.0 // indirect
2526
golang.org/x/time v0.5.0 // indirect
2627
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
55
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
66
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
77
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
8-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
9-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
8+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
9+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1010
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
1111
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
1212
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
@@ -16,8 +16,8 @@ github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8Ku
1616
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1717
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f h1:NkhuupzH5ch7b/Y/6ZHJWrnNLoiNnSJaow6DPb8VW2I=
1818
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f/go.mod h1:KXmw+ouSJNOsuRpg4wgwwCQuunrGz4yoAqQjsLjc6N0=
19-
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba h1:EY5AS7CCtfmARNv2zXUOrsEMPFDGYxaw65JzA2p51Vk=
20-
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
19+
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZNjr6sGeT00J8uU7JF4cNUdb44/Duis=
20+
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
2121
github.com/sagernet/nftables v0.3.0-beta.2 h1:yKqMl4Dpb6nKxAmlE6fXjJRlLO2c1f2wyNFBg4hBr8w=
2222
github.com/sagernet/nftables v0.3.0-beta.2/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
2323
github.com/sagernet/sing v0.5.0-alpha.9 h1:Mmg+LCbaKXBeQD/ttzi0/MQa3NcUyfadIgkGzhQW7o0=
@@ -27,10 +27,12 @@ github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1Y
2727
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
2828
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M=
2929
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
30+
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM=
31+
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
3032
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
3133
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
32-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
33-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
34+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
35+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3436
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3537
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
3638
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

redirect.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ import (
44
"context"
55

66
"github.com/sagernet/sing/common/logger"
7+
8+
"go4.org/netipx"
79
)
810

911
type AutoRedirect interface {
1012
Start() error
1113
Close() error
14+
UpdateRouteAddressSet() error
1215
}
1316

1417
type AutoRedirectOptions struct {
15-
TunOptions *Options
16-
Context context.Context
17-
Handler Handler
18-
Logger logger.Logger
19-
TableName string
20-
DisableNFTables bool
21-
CustomRedirectPort func() int
18+
TunOptions *Options
19+
Context context.Context
20+
Handler Handler
21+
Logger logger.Logger
22+
TableName string
23+
DisableNFTables bool
24+
CustomRedirectPort func() int
25+
RouteAddressSet *[]*netipx.IPSet
26+
RouteExcludeAddressSet *[]*netipx.IPSet
2227
}

redirect_linux.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
E "github.com/sagernet/sing/common/exceptions"
1313
"github.com/sagernet/sing/common/logger"
1414
M "github.com/sagernet/sing/common/metadata"
15+
16+
"go4.org/netipx"
1517
)
1618

1719
type autoRedirect struct {
@@ -30,6 +32,8 @@ type autoRedirect struct {
3032
useNFTables bool
3133
androidSu bool
3234
suPath string
35+
routeAddressSet *[]*netipx.IPSet
36+
routeExcludeAddressSet *[]*netipx.IPSet
3337
}
3438

3539
func NewAutoRedirect(options AutoRedirectOptions) (AutoRedirect, error) {
@@ -41,6 +45,8 @@ func NewAutoRedirect(options AutoRedirectOptions) (AutoRedirect, error) {
4145
tableName: options.TableName,
4246
useNFTables: runtime.GOOS != "android" && !options.DisableNFTables,
4347
customRedirectPortFunc: options.CustomRedirectPort,
48+
routeAddressSet: options.RouteAddressSet,
49+
routeExcludeAddressSet: options.RouteExcludeAddressSet,
4450
}
4551
var err error
4652
if runtime.GOOS == "android" {
@@ -134,6 +140,14 @@ func (r *autoRedirect) Close() error {
134140
)
135141
}
136142

143+
func (r *autoRedirect) UpdateRouteAddressSet() error {
144+
if r.useNFTables {
145+
return r.nftablesUpdateRouteAddressSet()
146+
} else {
147+
return nil
148+
}
149+
}
150+
137151
func (r *autoRedirect) initializeNFTables() error {
138152
nft, err := nftables.New()
139153
if err != nil {

0 commit comments

Comments
 (0)