Improve: add cache for macOS PROCESS-NAME
This commit is contained in:
parent
ee72865f48
commit
0e4b9daaad
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -11,10 +12,14 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/cache"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// store process name for when dealing with multiple PROCESS-NAME rules
|
||||||
|
var processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64))
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
adapter string
|
adapter string
|
||||||
process string
|
process string
|
||||||
@ -25,13 +30,19 @@ func (ps *Process) RuleType() C.RuleType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ps *Process) Match(metadata *C.Metadata) bool {
|
func (ps *Process) Match(metadata *C.Metadata) bool {
|
||||||
name, err := getExecPathFromAddress(metadata.SrcIP, metadata.SrcPort, metadata.NetWork == C.TCP)
|
key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort)
|
||||||
if err != nil {
|
cached, hit := processCache.Get(key)
|
||||||
log.Debugln("[%s] getExecPathFromAddress error: %s", C.Process.String(), err.Error())
|
if !hit {
|
||||||
return false
|
name, err := getExecPathFromAddress(metadata.SrcIP, metadata.SrcPort, metadata.NetWork == C.TCP)
|
||||||
|
if err != nil {
|
||||||
|
log.Debugln("[%s] getExecPathFromAddress error: %s", C.Process.String(), err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cached = name
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToLower(name) == ps.process
|
return strings.EqualFold(cached.(string), ps.process)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Adapter() string {
|
func (p *Process) Adapter() string {
|
||||||
|
Reference in New Issue
Block a user