From b8d5321615676103e8d2b7da59e776ce66e6739f Mon Sep 17 00:00:00 2001 From: adlyq <2833154405@qq.com> Date: Sat, 23 Apr 2022 12:11:26 +0800 Subject: [PATCH] feat: cache uid --- constant/metadata.go | 7 ++++++- rule/common/uid.go | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/constant/metadata.go b/constant/metadata.go index 5a91fe96..03b2dccd 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -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()) diff --git a/rule/common/uid.go b/rule/common/uid.go index 80c7d73a..342cd25e 100644 --- a/rule/common/uid.go +++ b/rule/common/uid.go @@ -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