Clash.Meta/rules/common/uid.go

65 lines
1.2 KiB
Go
Raw Normal View History

package common
import (
2022-05-29 18:12:43 +08:00
"fmt"
"runtime"
"github.com/Dreamacro/clash/common/utils"
C "github.com/Dreamacro/clash/constant"
2022-04-23 12:11:26 +08:00
"github.com/Dreamacro/clash/log"
)
type Uid struct {
*Base
uids utils.IntRanges[uint32]
oUid string
adapter string
}
func NewUid(oUid, adapter string) (*Uid, error) {
2022-05-29 18:12:43 +08:00
if !(runtime.GOOS == "linux" || runtime.GOOS == "android") {
return nil, fmt.Errorf("uid rule not support this platform")
}
uidRange, err := utils.NewIntRanges[uint32](oUid)
if err != nil {
return nil, fmt.Errorf("%w, %s", errPayload, err.Error())
}
if len(uidRange) == 0 {
return nil, errPayload
}
return &Uid{
Base: &Base{},
adapter: adapter,
oUid: oUid,
uids: uidRange,
}, nil
}
func (u *Uid) RuleType() C.RuleType {
return C.Uid
}
func (u *Uid) Match(metadata *C.Metadata) (bool, string) {
2023-01-14 02:23:30 +08:00
if metadata.Uid != 0 {
if u.uids.Check(metadata.Uid) {
return true, u.adapter
}
}
2023-01-14 02:23:30 +08:00
log.Warnln("[UID] could not get uid from %s", metadata.String())
return false, ""
}
func (u *Uid) Adapter() string {
return u.adapter
}
func (u *Uid) Payload() string {
return u.oUid
}
2023-01-14 02:23:30 +08:00
func (u *Uid) ShouldFindProcess() bool {
return true
}