2020-06-05 17:43:50 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
|
|
|
parsed = NewDomain(payload, target)
|
|
|
|
case "DOMAIN-SUFFIX":
|
|
|
|
parsed = NewDomainSuffix(payload, target)
|
|
|
|
case "DOMAIN-KEYWORD":
|
|
|
|
parsed = NewDomainKeyword(payload, target)
|
|
|
|
case "GEOIP":
|
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed = NewGEOIP(payload, target, noResolve)
|
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
|
|
|
|
case "SRC-IP-CIDR":
|
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
|
|
|
|
case "SRC-PORT":
|
2023-08-03 22:30:08 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeSrc)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DST-PORT":
|
2023-08-03 22:30:08 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeDest)
|
|
|
|
case "INBOUND-PORT":
|
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeInbound)
|
2020-07-19 13:17:05 +08:00
|
|
|
case "PROCESS-NAME":
|
2022-03-12 19:07:53 +08:00
|
|
|
parsed, parseErr = NewProcess(payload, target, true)
|
|
|
|
case "PROCESS-PATH":
|
|
|
|
parsed, parseErr = NewProcess(payload, target, false)
|
2023-04-22 20:07:47 +08:00
|
|
|
case "IPSET":
|
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed, parseErr = NewIPSet(payload, target, noResolve)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "MATCH":
|
|
|
|
parsed = NewMatch(target)
|
|
|
|
default:
|
|
|
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, parseErr
|
|
|
|
}
|