refactor: finding process and uid should to find with match process or uid rule, reduce memory allocation
This commit is contained in:
parent
277e71b26a
commit
be298cfa16
3 changed files with 33 additions and 24 deletions
|
@ -2,7 +2,6 @@ package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/process"
|
|
||||||
"github.com/Dreamacro/clash/listener/inner"
|
"github.com/Dreamacro/clash/listener/inner"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
@ -127,7 +126,9 @@ func GetGeneral() *config.General {
|
||||||
return general
|
return general
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateExperimental(c *config.Config) {}
|
func updateExperimental(c *config.Config) {
|
||||||
|
runtime.GC()
|
||||||
|
}
|
||||||
|
|
||||||
func updateDNS(c *config.DNS, generalIPv6 bool) {
|
func updateDNS(c *config.DNS, generalIPv6 bool) {
|
||||||
if !c.Enable {
|
if !c.Enable {
|
||||||
|
@ -274,7 +275,6 @@ func updateSniffer(sniffer *config.Sniffer) {
|
||||||
|
|
||||||
func updateGeneral(general *config.General, force bool) {
|
func updateGeneral(general *config.General, force bool) {
|
||||||
log.SetLevel(general.LogLevel)
|
log.SetLevel(general.LogLevel)
|
||||||
process.EnableFindProcess(general.EnableProcess)
|
|
||||||
tunnel.SetMode(general.Mode)
|
tunnel.SetMode(general.Mode)
|
||||||
dialer.DisableIPv6 = !general.IPv6
|
dialer.DisableIPv6 = !general.IPv6
|
||||||
if !dialer.DisableIPv6 {
|
if !dialer.DisableIPv6 {
|
||||||
|
|
|
@ -21,6 +21,7 @@ func (ps *Process) Match(metadata *C.Metadata) bool {
|
||||||
if ps.nameOnly {
|
if ps.nameOnly {
|
||||||
return strings.EqualFold(metadata.Process, ps.process)
|
return strings.EqualFold(metadata.Process, ps.process)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.EqualFold(metadata.ProcessPath, ps.process)
|
return strings.EqualFold(metadata.ProcessPath, ps.process)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +33,10 @@ func (ps *Process) Payload() string {
|
||||||
return ps.process
|
return ps.process
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ps *Process) ShouldFindProcess() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
|
func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
|
||||||
return &Process{
|
return &Process{
|
||||||
Base: &Base{},
|
Base: &Base{},
|
||||||
|
|
|
@ -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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,6 +365,10 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
resolved = true
|
resolved = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var processUid int32
|
||||||
|
process := ""
|
||||||
|
processPath := ""
|
||||||
|
foundProcess := false
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if !resolved && shouldResolveIP(rule, metadata) {
|
if !resolved && shouldResolveIP(rule, metadata) {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(metadata.Host)
|
||||||
|
@ -398,6 +381,27 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
resolved = true
|
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) {
|
if rule.Match(metadata) {
|
||||||
adapter, ok := proxies[rule.Adapter()]
|
adapter, ok := proxies[rule.Adapter()]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in a new issue