2022-04-22 16:27:51 +08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-05-29 18:12:43 +08:00
|
|
|
"fmt"
|
2023-06-04 11:51:30 +08:00
|
|
|
"runtime"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-04-22 16:27:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Uid struct {
|
|
|
|
*Base
|
2023-06-04 11:51:30 +08:00
|
|
|
uids utils.IntRanges[uint32]
|
2022-04-22 16:27:51 +08:00
|
|
|
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")
|
|
|
|
}
|
2022-04-22 16:27:51 +08:00
|
|
|
|
2024-01-20 11:00:06 +08:00
|
|
|
uidRange, err := utils.NewUnsignedRanges[uint32](oUid)
|
2023-06-04 11:51:30 +08:00
|
|
|
if err != nil {
|
2023-07-02 09:59:18 +08:00
|
|
|
return nil, fmt.Errorf("%w, %w", errPayload, err)
|
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 {
|
2023-06-04 11:51:30 +08:00
|
|
|
if u.uids.Check(metadata.Uid) {
|
|
|
|
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
|
|
|
|
}
|