From 3117b24c688eaaa7a94f3ade7b756fa1b883448a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 27 Aug 2022 12:16:50 +0800 Subject: [PATCH] Fix include_android_user --- packages.go | 2 +- packages_android.go | 8 ++++---- tun_rules.go | 31 +++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/packages.go b/packages.go index 816948b..58ffbda 100644 --- a/packages.go +++ b/packages.go @@ -5,7 +5,7 @@ import E "github.com/sagernet/sing/common/exceptions" type PackageManager interface { Start() error Close() error - IDByPackage(packageName string) ([]uint32, bool) + IDByPackage(packageName string) (uint32, bool) IDBySharedPackage(sharedPackage string) (uint32, bool) PackageByID(id uint32) (string, bool) SharedPackageByID(id uint32) (string, bool) diff --git a/packages_android.go b/packages_android.go index ddf5480..dfa4f83 100644 --- a/packages_android.go +++ b/packages_android.go @@ -18,7 +18,7 @@ import ( type packageManager struct { callback PackageManagerCallback watcher *fsnotify.Watcher - idByPackage map[string][]uint32 + idByPackage map[string]uint32 sharedByPackage map[string]uint32 packageById map[uint32]string sharedById map[uint32]string @@ -78,7 +78,7 @@ func (m *packageManager) Close() error { return common.Close(common.PtrOrNil(m.watcher)) } -func (m *packageManager) IDByPackage(packageName string) ([]uint32, bool) { +func (m *packageManager) IDByPackage(packageName string) (uint32, bool) { id, loaded := m.idByPackage[packageName] return id, loaded } @@ -114,7 +114,7 @@ func (m *packageManager) updatePackages() error { } func (m *packageManager) decodePackages(decoder *xml.Decoder) error { - idByPackage := make(map[string][]uint32) + idByPackage := make(map[string]uint32) sharedByPackage := make(map[string]uint32) packageById := make(map[uint32]string) sharedById := make(map[uint32]string) @@ -149,7 +149,7 @@ func (m *packageManager) decodePackages(decoder *xml.Decoder) error { if userID == 0 && name == "" { continue } - idByPackage[name] = append(idByPackage[name], uint32(userID)) + idByPackage[name] = uint32(userID) packageById[uint32(userID)] = name case "shared-user": var name string diff --git a/tun_rules.go b/tun_rules.go index 19bb402..5e0c256 100644 --- a/tun_rules.go +++ b/tun_rules.go @@ -15,23 +15,33 @@ const ( ) 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 { - o.IncludeUID = append(o.IncludeUID, ranges.New[uint32](uint32(androidUser)*androidUserRange, uint32(androidUser+1)*androidUserRange-1)) + 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 { + 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 { - o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(sharedId)) + for _, androidUser := range includeUser { + o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(sharedId+androidUser*androidUserRange)) + } continue } - if ids, loaded := packageManager.IDByPackage(packageName); loaded { - for _, id := range ids { - o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(id)) + if userId, loaded := packageManager.IDByPackage(packageName); loaded { + for _, androidUser := range includeUser { + o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(userId+androidUser*androidUserRange)) } continue } @@ -42,12 +52,13 @@ func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler o.ExcludePackage = common.Uniq(o.ExcludePackage) for _, packageName := range o.ExcludePackage { if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded { - o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(sharedId)) - continue + for _, androidUser := range includeUser { + o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(sharedId+androidUser*androidUserRange)) + } } - if ids, loaded := packageManager.IDByPackage(packageName); loaded { - for _, id := range ids { - o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(id)) + if userId, loaded := packageManager.IDByPackage(packageName); loaded { + for _, androidUser := range includeUser { + o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(userId+androidUser*androidUserRange)) } continue }