Chore: move find connection process to tunnel (#2016)

This commit is contained in:
Kr328
2022-03-12 19:07:53 +08:00
committed by GitHub
parent 9683c297a7
commit b866f06414
16 changed files with 84 additions and 64 deletions

View File

@ -34,6 +34,10 @@ func (d *Domain) ShouldResolveIP() bool {
return false
}
func (d *Domain) ShouldFindProcess() bool {
return false
}
func NewDomain(domain string, adapter string) *Domain {
return &Domain{
domain: strings.ToLower(domain),

View File

@ -35,6 +35,10 @@ func (dk *DomainKeyword) ShouldResolveIP() bool {
return false
}
func (dk *DomainKeyword) ShouldFindProcess() bool {
return false
}
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
return &DomainKeyword{
keyword: strings.ToLower(keyword),

View File

@ -35,6 +35,10 @@ func (ds *DomainSuffix) ShouldResolveIP() bool {
return false
}
func (ds *DomainSuffix) ShouldFindProcess() bool {
return false
}
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
return &DomainSuffix{
suffix: strings.ToLower(suffix),

View File

@ -28,6 +28,10 @@ func (f *Match) ShouldResolveIP() bool {
return false
}
func (f *Match) ShouldFindProcess() bool {
return false
}
func NewMatch(adapter string) *Match {
return &Match{
adapter: adapter,

View File

@ -42,6 +42,10 @@ func (g *GEOIP) ShouldResolveIP() bool {
return !g.noResolveIP
}
func (g *GEOIP) ShouldFindProcess() bool {
return false
}
func NewGEOIP(country string, adapter string, noResolveIP bool) *GEOIP {
geoip := &GEOIP{
country: country,

View File

@ -54,6 +54,10 @@ func (i *IPCIDR) ShouldResolveIP() bool {
return !i.noResolveIP
}
func (i *IPCIDR) ShouldFindProcess() bool {
return false
}
func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error) {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {

View File

@ -32,7 +32,9 @@ func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
case "DST-PORT":
parsed, parseErr = NewPort(payload, target, false)
case "PROCESS-NAME":
parsed, parseErr = NewProcess(payload, target)
parsed, parseErr = NewProcess(payload, target, true)
case "PROCESS-PATH":
parsed, parseErr = NewProcess(payload, target, false)
case "MATCH":
parsed = NewMatch(target)
default:

View File

@ -38,6 +38,10 @@ func (p *Port) ShouldResolveIP() bool {
return false
}
func (p *Port) ShouldFindProcess() bool {
return false
}
func NewPort(port string, adapter string, isSource bool) (*Port, error) {
_, err := strconv.ParseUint(port, 10, 16)
if err != nil {

View File

@ -1,21 +1,16 @@
package rules
import (
"fmt"
"strconv"
"path/filepath"
"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
adapter string
process string
nameOnly bool
}
func (ps *Process) RuleType() C.RuleType {
@ -23,26 +18,11 @@ func (ps *Process) RuleType() C.RuleType {
}
func (ps *Process) Match(metadata *C.Metadata) bool {
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
if ps.nameOnly {
return strings.EqualFold(filepath.Base(metadata.ProcessPath), ps.process)
}
return strings.EqualFold(cached.(string), ps.process)
return strings.EqualFold(metadata.ProcessPath, ps.process)
}
func (ps *Process) Adapter() string {
@ -57,9 +37,14 @@ func (ps *Process) ShouldResolveIP() bool {
return false
}
func NewProcess(process string, adapter string) (*Process, error) {
func (ps *Process) ShouldFindProcess() bool {
return true
}
func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
return &Process{
adapter: adapter,
process: process,
adapter: adapter,
process: process,
nameOnly: nameOnly,
}, nil
}