Feature: process condition for rules

This commit is contained in:
gVisor bot 2022-01-28 22:52:35 +08:00
parent 18e0f75e9e
commit b448d10ff9
5 changed files with 46 additions and 7 deletions

View File

@ -86,6 +86,7 @@ tun:
- Support rule `SCRIPT`.
- Support `multiport` condition for rule `SRC-PORT` and `DST-PORT`.
- Support `network` condition for all rules.
- Support `process` condition for all rules.
- Support source IPCIDR condition for all rules, just append to the end.
The `GEOSITE` databases via https://github.com/Loyalsoldier/v2ray-rules-dat.
@ -106,6 +107,9 @@ rules:
- DOMAIN-SUFFIX,bilibili.com,DIRECT,tcp
- DOMAIN-SUFFIX,bilibili.com,REJECT,udp
# process(add 'P:' prefix) condition for all rules
- DOMAIN-SUFFIX,qq.com,REJECT,P:Google Chrome Helper
# multiport condition for rules SRC-PORT and DST-PORT
- DST-PORT,123/136/137-139,DIRECT,udp

View File

@ -2,6 +2,7 @@ package constant
import (
"net"
"strings"
"github.com/Dreamacro/clash/component/geodata/router"
)
@ -9,8 +10,9 @@ import (
var TunBroadcastAddr = net.IPv4(198, 18, 255, 255)
type RuleExtra struct {
Network NetWork
SourceIPs []*net.IPNet
Network NetWork
SourceIPs []*net.IPNet
ProcessNames []string
}
func (re *RuleExtra) NotMatchNetwork(network NetWork) bool {
@ -30,6 +32,19 @@ func (re *RuleExtra) NotMatchSourceIP(srcIP net.IP) bool {
return true
}
func (re *RuleExtra) NotMatchProcessName(processName string) bool {
if re.ProcessNames == nil {
return false
}
for _, pn := range re.ProcessNames {
if strings.EqualFold(pn, processName) {
return false
}
}
return true
}
type RuleGeoSite interface {
GetDomainMatcher() *router.DomainMatcher
}

View File

@ -3,6 +3,7 @@ package rules
import (
"errors"
"net"
"strings"
C "github.com/Dreamacro/clash/constant"
)
@ -24,9 +25,9 @@ func HasNoResolve(params []string) bool {
func findNetwork(params []string) C.NetWork {
for _, p := range params {
if p == "tcp" {
if strings.EqualFold(p, "tcp") {
return C.TCP
} else if p == "udp" {
} else if strings.EqualFold(p, "udp") {
return C.UDP
}
}
@ -51,3 +52,17 @@ func findSourceIPs(params []string) []*net.IPNet {
}
return nil
}
func findProcessName(params []string) []string {
var processNames []string
for _, p := range params {
if strings.HasPrefix(p, "P:") {
processNames = append(processNames, strings.TrimPrefix(p, "P:"))
}
}
if len(processNames) > 0 {
return processNames
}
return nil
}

View File

@ -13,8 +13,9 @@ func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
)
ruleExtra := &C.RuleExtra{
Network: findNetwork(params),
SourceIPs: findSourceIPs(params),
Network: findNetwork(params),
SourceIPs: findSourceIPs(params),
ProcessNames: findProcessName(params),
}
switch tp {

View File

@ -360,6 +360,10 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
if extra.NotMatchSourceIP(metadata.SrcIP) {
continue
}
if extra.NotMatchProcessName(metadata.Process) {
continue
}
}
return adapter, rule, nil