github.com/metacubex/mihomo@v1.18.5/rules/common/uid.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/metacubex/mihomo/common/utils"
     8  	C "github.com/metacubex/mihomo/constant"
     9  	"github.com/metacubex/mihomo/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  }