2020-12-17 22:17:27 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-05 18:06:20 +08:00
|
|
|
"net"
|
2020-12-17 22:17:27 +08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-03-05 18:06:20 +08:00
|
|
|
var (
|
|
|
|
processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64))
|
|
|
|
|
|
|
|
localIPs = getLocalIPs()
|
|
|
|
)
|
2020-12-17 22:17:27 +08:00
|
|
|
|
|
|
|
type Process struct {
|
2021-08-31 21:46:04 +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-07-01 22:49:29 +08:00
|
|
|
if metadata.Process != "" {
|
|
|
|
return strings.EqualFold(metadata.Process, ps.process)
|
|
|
|
}
|
|
|
|
|
2022-03-05 18:06:20 +08:00
|
|
|
// ignore source IP not on local machine
|
|
|
|
if !isLocalIP(metadata.SrcIP) {
|
2021-07-01 22:49:29 +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)
|
|
|
|
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 22:49:29 +08:00
|
|
|
metadata.Process = cached.(string)
|
|
|
|
|
|
|
|
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-08-31 21:46:04 +08:00
|
|
|
func (ps *Process) RuleExtra() *C.RuleExtra {
|
|
|
|
return ps.ruleExtra
|
2021-07-01 22:49:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:46:04 +08:00
|
|
|
func NewProcess(process string, adapter string, ruleExtra *C.RuleExtra) (*Process, error) {
|
2020-12-17 22:17:27 +08:00
|
|
|
return &Process{
|
2021-08-31 21:46:04 +08:00
|
|
|
adapter: adapter,
|
|
|
|
process: process,
|
|
|
|
ruleExtra: ruleExtra,
|
2020-12-17 22:17:27 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2022-03-05 18:06:20 +08:00
|
|
|
|
|
|
|
func getLocalIPs() []net.IP {
|
|
|
|
ips := []net.IP{net.IPv4(198, 18, 0, 1), net.IPv4zero, net.IPv6zero}
|
|
|
|
|
|
|
|
netInterfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(netInterfaces); i++ {
|
|
|
|
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
|
|
|
|
adds, _ := netInterfaces[i].Addrs()
|
|
|
|
|
|
|
|
for _, address := range adds {
|
|
|
|
if ipNet, ok := address.(*net.IPNet); ok {
|
|
|
|
ips = append(ips, ipNet.IP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLocalIP(srcIP net.IP) bool {
|
|
|
|
for _, ip := range localIPs {
|
|
|
|
if ip.Equal(srcIP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|