-
Notifications
You must be signed in to change notification settings - Fork 129
/
tun_rules.go
98 lines (92 loc) · 3.21 KB
/
tun_rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tun
import (
"context"
"os"
"sort"
"strconv"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/ranges"
)
const (
androidUserRange = 100000
userEnd uint32 = 0xFFFFFFFF - 1
)
func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler E.Handler) {
var includeUser []uint32
if len(o.IncludeAndroidUser) > 0 {
o.IncludeAndroidUser = common.Uniq(o.IncludeAndroidUser)
sort.Ints(o.IncludeAndroidUser)
var userExcludeRange []ranges.Range[uint32]
for _, androidUser := range o.IncludeAndroidUser {
includeUser = append(includeUser, uint32(androidUser))
userExcludeRange = append(userExcludeRange, ranges.New[uint32](uint32(androidUser)*androidUserRange, uint32(androidUser+1)*androidUserRange-1))
}
userExcludeRange = ranges.Revert(0, userEnd, userExcludeRange)
o.ExcludeUID = append(o.ExcludeUID, userExcludeRange...)
}
if len(includeUser) == 0 {
userDirs, err := os.ReadDir("/data/user")
if err == nil {
var userId uint64
for _, userDir := range userDirs {
userId, err = strconv.ParseUint(userDir.Name(), 10, 32)
if err != nil {
continue
}
includeUser = append(includeUser, uint32(userId))
}
}
}
if len(includeUser) == 0 {
includeUser = []uint32{0}
}
if len(o.IncludePackage) > 0 {
o.IncludePackage = common.Uniq(o.IncludePackage)
for _, packageName := range o.IncludePackage {
if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded {
for _, androidUser := range includeUser {
o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(sharedId+androidUser*androidUserRange))
}
continue
}
if userId, loaded := packageManager.IDByPackage(packageName); loaded {
for _, androidUser := range includeUser {
o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(userId+androidUser*androidUserRange))
}
continue
}
errorHandler.NewError(context.Background(), E.New("package to include not found: ", packageName))
}
}
if len(o.ExcludePackage) > 0 {
o.ExcludePackage = common.Uniq(o.ExcludePackage)
for _, packageName := range o.ExcludePackage {
if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded {
for _, androidUser := range includeUser {
o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(sharedId+androidUser*androidUserRange))
}
}
if userId, loaded := packageManager.IDByPackage(packageName); loaded {
for _, androidUser := range includeUser {
o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(userId+androidUser*androidUserRange))
}
continue
}
errorHandler.NewError(context.Background(), E.New("package to exclude not found: ", packageName))
}
}
}
func (o *Options) ExcludedRanges() (uidRanges []ranges.Range[uint32]) {
return buildExcludedRanges(o.IncludeUID, o.ExcludeUID)
}
func buildExcludedRanges(includeRanges []ranges.Range[uint32], excludeRanges []ranges.Range[uint32]) (uidRanges []ranges.Range[uint32]) {
uidRanges = includeRanges
if len(uidRanges) > 0 {
uidRanges = ranges.Exclude(uidRanges, excludeRanges)
uidRanges = ranges.Revert(0, userEnd, uidRanges)
} else {
uidRanges = excludeRanges
}
return ranges.Merge(uidRanges)
}