2022-04-22 16:27:51 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-05-29 18:12:43 +08:00
|
|
|
"fmt"
|
2022-04-22 16:27:51 +08:00
|
|
|
"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"
|
2022-05-29 18:12:43 +08:00
|
|
|
"runtime"
|
2022-04-22 16:27:51 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Uid struct {
|
|
|
|
*Base
|
2022-11-12 20:43:48 +08:00
|
|
|
uids []utils.Range[uint32]
|
2022-04-22 16:27:51 +08:00
|
|
|
oUid string
|
|
|
|
adapter string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUid(oUid, adapter string) (*Uid, error) {
|
|
|
|
//if len(_uids) > 28 {
|
|
|
|
// return nil, fmt.Errorf("%s, too many uid to use, maximum support 28 uid", errPayload.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")
|
|
|
|
}
|
2022-04-22 16:27:51 +08:00
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
var uidRange []utils.Range[uint32]
|
2022-04-22 16:27:51 +08:00
|
|
|
for _, u := range strings.Split(oUid, "/") {
|
|
|
|
if u == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
subUids := strings.Split(u, "-")
|
|
|
|
subUidsLen := len(subUids)
|
|
|
|
if subUidsLen > 2 {
|
|
|
|
return nil, errPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
uidStart, err := strconv.ParseUint(strings.Trim(subUids[0], "[ ]"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
switch subUidsLen {
|
|
|
|
case 1:
|
2022-11-12 20:43:48 +08:00
|
|
|
uidRange = append(uidRange, *utils.NewRange(uint32(uidStart), uint32(uidStart)))
|
2022-04-22 16:27:51 +08:00
|
|
|
case 2:
|
|
|
|
uidEnd, err := strconv.ParseUint(strings.Trim(subUids[1], "[ ]"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errPayload
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
uidRange = append(uidRange, *utils.NewRange(uint32(uidStart), uint32(uidEnd)))
|
2022-04-22 16:27:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
func (u *Uid) Match(metadata *C.Metadata) (bool, string) {
|
2023-01-14 02:23:30 +08:00
|
|
|
if metadata.Uid != 0 {
|
|
|
|
for _, uid := range u.uids {
|
|
|
|
if uid.Contains(metadata.Uid) {
|
2022-11-12 20:43:48 +08:00
|
|
|
return true, u.adapter
|
|
|
|
}
|
2022-04-22 16:27:51 +08:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 02:23:30 +08:00
|
|
|
log.Warnln("[UID] could not get uid from %s", metadata.String())
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
return false, ""
|
2022-04-22 16:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|