rule_item_user_id.go 939 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package rule
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. F "github.com/sagernet/sing/common/format"
  6. )
  7. var _ RuleItem = (*UserIdItem)(nil)
  8. type UserIdItem struct {
  9. userIds []int32
  10. userIdMap map[int32]bool
  11. }
  12. func NewUserIDItem(userIdList []int32) *UserIdItem {
  13. rule := &UserIdItem{
  14. userIds: userIdList,
  15. userIdMap: make(map[int32]bool),
  16. }
  17. for _, userId := range userIdList {
  18. rule.userIdMap[userId] = true
  19. }
  20. return rule
  21. }
  22. func (r *UserIdItem) Match(metadata *adapter.InboundContext) bool {
  23. if metadata.ProcessInfo == nil || metadata.ProcessInfo.UserId == -1 {
  24. return false
  25. }
  26. return r.userIdMap[metadata.ProcessInfo.UserId]
  27. }
  28. func (r *UserIdItem) String() string {
  29. var description string
  30. pLen := len(r.userIds)
  31. if pLen == 1 {
  32. description = "user_id=" + F.ToString(r.userIds[0])
  33. } else {
  34. description = "user_id=[" + strings.Join(F.MapToString(r.userIds), " ") + "]"
  35. }
  36. return description
  37. }