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