uid.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package common
  2. import (
  3. "fmt"
  4. "runtime"
  5. "github.com/metacubex/mihomo/common/utils"
  6. C "github.com/metacubex/mihomo/constant"
  7. "github.com/metacubex/mihomo/log"
  8. )
  9. type Uid struct {
  10. *Base
  11. uids utils.IntRanges[uint32]
  12. oUid string
  13. adapter string
  14. }
  15. func NewUid(oUid, adapter string) (*Uid, error) {
  16. if !(runtime.GOOS == "linux" || runtime.GOOS == "android") {
  17. return nil, fmt.Errorf("uid rule not support this platform")
  18. }
  19. uidRange, err := utils.NewUnsignedRanges[uint32](oUid)
  20. if err != nil {
  21. return nil, fmt.Errorf("%w, %w", errPayload, err)
  22. }
  23. if len(uidRange) == 0 {
  24. return nil, errPayload
  25. }
  26. return &Uid{
  27. Base: &Base{},
  28. adapter: adapter,
  29. oUid: oUid,
  30. uids: uidRange,
  31. }, nil
  32. }
  33. func (u *Uid) RuleType() C.RuleType {
  34. return C.Uid
  35. }
  36. func (u *Uid) Match(metadata *C.Metadata) (bool, string) {
  37. if metadata.Uid != 0 {
  38. if u.uids.Check(metadata.Uid) {
  39. return true, u.adapter
  40. }
  41. }
  42. log.Warnln("[UID] could not get uid from %s", metadata.String())
  43. return false, ""
  44. }
  45. func (u *Uid) Adapter() string {
  46. return u.adapter
  47. }
  48. func (u *Uid) Payload() string {
  49. return u.oUid
  50. }
  51. func (u *Uid) ShouldFindProcess() bool {
  52. return true
  53. }