feat: cache uid

This commit is contained in:
adlyq 2022-04-23 12:11:26 +08:00
parent eb6f7e3158
commit b8d5321615
2 changed files with 20 additions and 6 deletions

View File

@ -83,6 +83,7 @@ type Metadata struct {
AddrType int `json:"-"`
Host string `json:"host"`
DNSMode DNSMode `json:"dnsMode"`
Uid int32 `json:"uid"`
Process string `json:"process"`
ProcessPath string `json:"processPath"`
}
@ -100,7 +101,11 @@ func (m *Metadata) SourceDetail() string {
return fmt.Sprintf("[%s]", ClashName)
}
if m.Process != "" {
if m.Process != "" && m.Uid != 0 {
return fmt.Sprintf("%s(%s:%d)", m.SourceAddress(), m.Process, m.Uid)
} else if m.Uid != 0 {
return fmt.Sprintf("%s(%d)", m.SourceAddress(), m.Uid)
} else if m.Process != "" {
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
} else {
return fmt.Sprintf("%s", m.SourceAddress())

View File

@ -4,6 +4,7 @@ import (
"github.com/Dreamacro/clash/common/utils"
"github.com/Dreamacro/clash/component/process"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"strconv"
"strings"
)
@ -70,11 +71,19 @@ func (u *Uid) Match(metadata *C.Metadata) bool {
if err != nil {
return false
}
if uid, err := process.FindUid(metadata.NetWork.String(), metadata.SrcIP, srcPort); err == nil {
for _, _uid := range u.uids {
if _uid.Contains(uid) {
return true
}
var uid int32
if metadata.Uid != 0 {
uid = metadata.Uid
} else if uid, err := process.FindUid(metadata.NetWork.String(), metadata.SrcIP, srcPort); err == nil {
metadata.Uid = uid
} else {
log.Warnln("[UID] could not get uid from %s", metadata.String())
return false
}
for _, _uid := range u.uids {
if _uid.Contains(uid) {
return true
}
}
return false