Fix include_android_user

This commit is contained in:
世界 2022-08-27 12:16:50 +08:00
parent e01ce3a8a7
commit 3117b24c68
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 26 additions and 15 deletions

View file

@ -5,7 +5,7 @@ import E "github.com/sagernet/sing/common/exceptions"
type PackageManager interface { type PackageManager interface {
Start() error Start() error
Close() error Close() error
IDByPackage(packageName string) ([]uint32, bool) IDByPackage(packageName string) (uint32, bool)
IDBySharedPackage(sharedPackage string) (uint32, bool) IDBySharedPackage(sharedPackage string) (uint32, bool)
PackageByID(id uint32) (string, bool) PackageByID(id uint32) (string, bool)
SharedPackageByID(id uint32) (string, bool) SharedPackageByID(id uint32) (string, bool)

View file

@ -18,7 +18,7 @@ import (
type packageManager struct { type packageManager struct {
callback PackageManagerCallback callback PackageManagerCallback
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
idByPackage map[string][]uint32 idByPackage map[string]uint32
sharedByPackage map[string]uint32 sharedByPackage map[string]uint32
packageById map[uint32]string packageById map[uint32]string
sharedById map[uint32]string sharedById map[uint32]string
@ -78,7 +78,7 @@ func (m *packageManager) Close() error {
return common.Close(common.PtrOrNil(m.watcher)) 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] id, loaded := m.idByPackage[packageName]
return id, loaded return id, loaded
} }
@ -114,7 +114,7 @@ func (m *packageManager) updatePackages() error {
} }
func (m *packageManager) decodePackages(decoder *xml.Decoder) error { func (m *packageManager) decodePackages(decoder *xml.Decoder) error {
idByPackage := make(map[string][]uint32) idByPackage := make(map[string]uint32)
sharedByPackage := make(map[string]uint32) sharedByPackage := make(map[string]uint32)
packageById := make(map[uint32]string) packageById := make(map[uint32]string)
sharedById := 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 == "" { if userID == 0 && name == "" {
continue continue
} }
idByPackage[name] = append(idByPackage[name], uint32(userID)) idByPackage[name] = uint32(userID)
packageById[uint32(userID)] = name packageById[uint32(userID)] = name
case "shared-user": case "shared-user":
var name string var name string

View file

@ -15,23 +15,33 @@ const (
) )
func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler E.Handler) { func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler E.Handler) {
var includeUser []uint32
if len(o.IncludeAndroidUser) > 0 { if len(o.IncludeAndroidUser) > 0 {
o.IncludeAndroidUser = common.Uniq(o.IncludeAndroidUser) o.IncludeAndroidUser = common.Uniq(o.IncludeAndroidUser)
sort.Ints(o.IncludeAndroidUser) sort.Ints(o.IncludeAndroidUser)
var userExcludeRange []ranges.Range[uint32]
for _, androidUser := range o.IncludeAndroidUser { 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 { if len(o.IncludePackage) > 0 {
o.IncludePackage = common.Uniq(o.IncludePackage) o.IncludePackage = common.Uniq(o.IncludePackage)
for _, packageName := range o.IncludePackage { for _, packageName := range o.IncludePackage {
if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded { 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 continue
} }
if ids, loaded := packageManager.IDByPackage(packageName); loaded { if userId, loaded := packageManager.IDByPackage(packageName); loaded {
for _, id := range ids { for _, androidUser := range includeUser {
o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(id)) o.IncludeUID = append(o.IncludeUID, ranges.NewSingle(userId+androidUser*androidUserRange))
} }
continue continue
} }
@ -42,12 +52,13 @@ func (o *Options) BuildAndroidRules(packageManager PackageManager, errorHandler
o.ExcludePackage = common.Uniq(o.ExcludePackage) o.ExcludePackage = common.Uniq(o.ExcludePackage)
for _, packageName := range o.ExcludePackage { for _, packageName := range o.ExcludePackage {
if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded { if sharedId, loaded := packageManager.IDBySharedPackage(packageName); loaded {
o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(sharedId)) for _, androidUser := range includeUser {
continue o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(sharedId+androidUser*androidUserRange))
}
} }
if ids, loaded := packageManager.IDByPackage(packageName); loaded { if userId, loaded := packageManager.IDByPackage(packageName); loaded {
for _, id := range ids { for _, androidUser := range includeUser {
o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(id)) o.ExcludeUID = append(o.ExcludeUID, ranges.NewSingle(userId+androidUser*androidUserRange))
} }
continue continue
} }