diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 1dd21c10..9a3a7bc0 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -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 { diff --git a/rules/common/process.go b/rules/common/process.go index 9e69b25d..69d83632 100644 --- a/rules/common/process.go +++ b/rules/common/process.go @@ -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{}, diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index b8038520..3cf99ead 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -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 {