2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2020-12-17 22:17:27 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
|
|
|
"github.com/Dreamacro/clash/component/process"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64))
|
|
|
|
|
|
|
|
type Process struct {
|
2021-11-17 16:03:47 +08:00
|
|
|
adapter string
|
|
|
|
process string
|
|
|
|
ruleExtra *C.RuleExtra
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) RuleType() C.RuleType {
|
|
|
|
return C.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) Match(metadata *C.Metadata) bool {
|
2021-11-17 16:03:47 +08:00
|
|
|
if metadata.Process != "" {
|
|
|
|
return strings.EqualFold(metadata.Process, ps.process)
|
|
|
|
}
|
2021-12-04 19:59:41 +08:00
|
|
|
|
2022-02-05 00:51:06 +08:00
|
|
|
if C.AutoIptables == "Enable" {
|
2021-11-17 16:03:47 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:17:27 +08:00
|
|
|
key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort)
|
2022-02-04 17:36:34 +08:00
|
|
|
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
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2022-02-04 17:36:34 +08:00
|
|
|
metadata.Process = cached.(string)
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
return strings.EqualFold(metadata.Process, ps.process)
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2021-03-24 01:00:21 +08:00
|
|
|
func (ps *Process) Adapter() string {
|
|
|
|
return ps.adapter
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2021-03-24 01:00:21 +08:00
|
|
|
func (ps *Process) Payload() string {
|
|
|
|
return ps.process
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2021-03-24 01:00:21 +08:00
|
|
|
func (ps *Process) ShouldResolveIP() bool {
|
2020-12-17 22:17:27 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
func (ps *Process) RuleExtra() *C.RuleExtra {
|
|
|
|
return ps.ruleExtra
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcess(process string, adapter string, ruleExtra *C.RuleExtra) (*Process, error) {
|
2020-12-17 22:17:27 +08:00
|
|
|
return &Process{
|
2021-11-17 16:03:47 +08:00
|
|
|
adapter: adapter,
|
|
|
|
process: process,
|
|
|
|
ruleExtra: ruleExtra,
|
2020-12-17 22:17:27 +08:00
|
|
|
}, nil
|
|
|
|
}
|