弃用过期函数,修复Process Name获取问题
This commit is contained in:
gVisor bot 2022-03-26 16:17:44 +08:00
parent 3fd2aa1832
commit 31708f1e74
2 changed files with 5 additions and 37 deletions

View file

@ -174,7 +174,7 @@ func newSearcher(isV4, isTCP bool) *searcher {
func getTransportTable(fn uintptr, family int, class int) ([]byte, error) { func getTransportTable(fn uintptr, family int, class int) ([]byte, error) {
for size, buf := uint32(8), make([]byte, 8); ; { for size, buf := uint32(8), make([]byte, 8); ; {
ptr := unsafe.Pointer(&buf[0]) ptr := unsafe.Pointer(&buf[0])
err, _, _ := syscall.SyscallN(fn, 6, uintptr(ptr), uintptr(unsafe.Pointer(&size)), 0, uintptr(family), uintptr(class), 0) err, _, _ := syscall.SyscallN(fn, uintptr(ptr), uintptr(unsafe.Pointer(&size)), 0, uintptr(family), uintptr(class), 0)
switch err { switch err {
case 0: case 0:
@ -205,14 +205,12 @@ func getExecPathFromPID(pid uint32) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
defer func(handle windows.Handle) { defer windows.CloseHandle(h)
_ = windows.CloseHandle(handle)
}(h)
buf := make([]uint16, syscall.MAX_LONG_PATH) buf := make([]uint16, syscall.MAX_LONG_PATH)
size := uint32(len(buf)) size := uint32(len(buf))
r1, _, err := syscall.SyscallN( r1, _, err := syscall.SyscallN(
queryProcName, 4, queryProcName,
uintptr(h), uintptr(h),
uintptr(1), uintptr(1),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),

View file

@ -1,18 +1,11 @@
package common package common
import ( import (
"fmt"
"strconv"
"strings" "strings"
"github.com/Dreamacro/clash/common/cache"
"github.com/Dreamacro/clash/component/process"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
) )
var processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64))
type Process struct { type Process struct {
*Base *Base
adapter string adapter string
@ -25,34 +18,11 @@ func (ps *Process) RuleType() C.RuleType {
} }
func (ps *Process) Match(metadata *C.Metadata) bool { func (ps *Process) Match(metadata *C.Metadata) bool {
if metadata.Process != "" { if ps.nameOnly {
return strings.EqualFold(metadata.Process, ps.process) return strings.EqualFold(metadata.Process, ps.process)
} }
key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort) return strings.EqualFold(metadata.ProcessPath, ps.process)
if strings.TrimSpace(metadata.Process) == "" {
cached, hit := processCache.Get(key)
if !hit {
srcPort, err := strconv.Atoi(metadata.SrcPort)
if err != nil {
processCache.Set(key, "")
return false
}
name, err := process.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, srcPort)
if err != nil {
log.Debugln("[Rule] find process name %s error: %s", C.Process.String(), err.Error())
}
processCache.Set(key, name)
cached = name
}
metadata.Process = cached.(string)
}
return strings.EqualFold(metadata.Process, ps.process)
} }
func (ps *Process) Adapter() string { func (ps *Process) Adapter() string {