Add process_name/package_name/user/user_id rule item

This commit is contained in:
世界
2022-07-23 19:01:41 +08:00
parent 4abf669d09
commit 5f6f33c464
23 changed files with 1191 additions and 55 deletions

44
route/rule_user_id.go Normal file
View File

@@ -0,0 +1,44 @@
package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
F "github.com/sagernet/sing/common/format"
)
var _ RuleItem = (*UserIdItem)(nil)
type UserIdItem struct {
userIds []int32
userIdMap map[int32]bool
}
func NewUserIDItem(userIdList []int32) *UserIdItem {
rule := &UserIdItem{
userIds: userIdList,
userIdMap: make(map[int32]bool),
}
for _, userId := range userIdList {
rule.userIdMap[userId] = true
}
return rule
}
func (r *UserIdItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.UserId == -1 {
return false
}
return r.userIdMap[metadata.ProcessInfo.UserId]
}
func (r *UserIdItem) String() string {
var description string
pLen := len(r.userIds)
if pLen == 1 {
description = "user_id=" + F.ToString(r.userIds[0])
} else {
description = "user_id=[" + strings.Join(F.MapToString(r.userIds), " ") + "]"
}
return description
}