2020-12-17 14:17:27 +00:00
|
|
|
package rules
|
|
|
|
|
|
|
|
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 {
|
|
|
|
adapter string
|
|
|
|
process string
|
2021-07-01 14:49:29 +00:00
|
|
|
network C.NetWork
|
2020-12-17 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) RuleType() C.RuleType {
|
|
|
|
return C.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) Match(metadata *C.Metadata) bool {
|
2021-07-01 14:49:29 +00:00
|
|
|
|
|
|
|
if metadata.Process != "" {
|
|
|
|
//log.Debugln("Use cache process: %s", metadata.Process)
|
|
|
|
return strings.EqualFold(metadata.Process, ps.process)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore match in proxy type "tproxy"
|
|
|
|
if metadata.Type == C.TPROXY {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:49:29 +00:00
|
|
|
metadata.Process = cached.(string)
|
|
|
|
|
|
|
|
return strings.EqualFold(metadata.Process, ps.process)
|
2020-12-17 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:00:21 +00:00
|
|
|
func (ps *Process) Adapter() string {
|
|
|
|
return ps.adapter
|
2020-12-17 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:00:21 +00:00
|
|
|
func (ps *Process) Payload() string {
|
|
|
|
return ps.process
|
2020-12-17 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:00:21 +00:00
|
|
|
func (ps *Process) ShouldResolveIP() bool {
|
2020-12-17 14:17:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:49:29 +00:00
|
|
|
func (ps *Process) NetWork() C.NetWork {
|
|
|
|
return ps.network
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcess(process string, adapter string, network C.NetWork) (*Process, error) {
|
2020-12-17 14:17:27 +00:00
|
|
|
return &Process{
|
|
|
|
adapter: adapter,
|
|
|
|
process: process,
|
2021-07-01 14:49:29 +00:00
|
|
|
network: network,
|
2020-12-17 14:17:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|