Skip to content

Commit ca84a23

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

File tree

8 files changed

+495
-245
lines changed

8 files changed

+495
-245
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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=

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)