refactor: finding process and uid should to find with match process or uid rule, reduce memory allocation

This commit is contained in:
gVisor bot 2022-06-14 22:50:57 +08:00
parent d40dea013c
commit 271be43a9d
3 changed files with 33 additions and 24 deletions

View file

@ -2,7 +2,6 @@ package executor
import (
"fmt"
"github.com/Dreamacro/clash/component/process"
"github.com/Dreamacro/clash/listener/inner"
"net/netip"
"os"
@ -127,7 +126,9 @@ func GetGeneral() *config.General {
return general
}
func updateExperimental(c *config.Config) {}
func updateExperimental(c *config.Config) {
runtime.GC()
}
func updateDNS(c *config.DNS, generalIPv6 bool) {
if !c.Enable {
@ -274,7 +275,6 @@ func updateSniffer(sniffer *config.Sniffer) {
func updateGeneral(general *config.General, force bool) {
log.SetLevel(general.LogLevel)
process.EnableFindProcess(general.EnableProcess)
tunnel.SetMode(general.Mode)
dialer.DisableIPv6 = !general.IPv6
if !dialer.DisableIPv6 {

View file

@ -21,6 +21,7 @@ func (ps *Process) Match(metadata *C.Metadata) bool {
if ps.nameOnly {
return strings.EqualFold(metadata.Process, ps.process)
}
return strings.EqualFold(metadata.ProcessPath, ps.process)
}
@ -32,6 +33,10 @@ func (ps *Process) Payload() string {
return ps.process
}
func (ps *Process) ShouldFindProcess() bool {
return true
}
func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
return &Process{
Base: &Base{},

View file

@ -180,27 +180,6 @@ func preHandleMetadata(metadata *C.Metadata) error {
}
}
// pre resolve process name
srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
if err == nil && P.ShouldFindProcess(metadata) {
uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(srcPort))
if err != nil {
if failTotal < 20 {
log.Debugln("[Process] find process %s: %v", metadata.String(), err)
failTotal++
}
} else {
metadata.Process = filepath.Base(path)
metadata.ProcessPath = path
if uid != -1 {
metadata.Uid = &uid
}
if procesCache != metadata.Process {
log.Debugln("[Process] %s from process %s", metadata.String(), path)
}
procesCache = metadata.Process
}
}
return nil
}
@ -386,6 +365,10 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
resolved = true
}
var processUid int32
process := ""
processPath := ""
foundProcess := false
for _, rule := range rules {
if !resolved && shouldResolveIP(rule, metadata) {
ip, err := resolver.ResolveIP(metadata.Host)
@ -398,6 +381,27 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
resolved = true
}
if !foundProcess && rule.ShouldFindProcess() {
srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
if err == nil && P.ShouldFindProcess(metadata) {
uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(srcPort))
if err != nil {
log.Debugln("[Process] find process %s: %v", metadata.String(), err)
} else {
process = filepath.Base(path)
processPath = path
processUid = uid
foundProcess = true
}
}
}
if foundProcess {
metadata.Uid = &processUid
metadata.Process = process
metadata.ProcessPath = processPath
}
if rule.Match(metadata) {
adapter, ok := proxies[rule.Adapter()]
if !ok {