mihomo/rule/parser.go

63 lines
1.9 KiB
Go
Raw Normal View History

package rule
2020-06-05 17:43:50 +08:00
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
RC "github.com/Dreamacro/clash/rule/common"
"github.com/Dreamacro/clash/rule/logic"
RP "github.com/Dreamacro/clash/rule/provider"
2020-06-05 17:43:50 +08:00
)
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
var (
parseErr error
parsed C.Rule
)
2021-11-17 16:03:47 +08:00
ruleExtra := &C.RuleExtra{
Network: RC.FindNetwork(params),
SourceIPs: RC.FindSourceIPs(params),
2021-11-17 16:03:47 +08:00
}
2020-06-05 17:43:50 +08:00
switch tp {
case "DOMAIN":
parsed = RC.NewDomain(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DOMAIN-SUFFIX":
parsed = RC.NewDomainSuffix(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DOMAIN-KEYWORD":
parsed = RC.NewDomainKeyword(payload, target, ruleExtra)
2021-11-17 16:03:47 +08:00
case "GEOSITE":
parsed, parseErr = RC.NewGEOSITE(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "GEOIP":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve, ruleExtra)
2020-06-05 17:43:50 +08:00
case "IP-CIDR", "IP-CIDR6":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RC.NewIPCIDR(payload, target, ruleExtra, RC.WithIPCIDRNoResolve(noResolve))
2020-06-05 17:43:50 +08:00
case "SRC-IP-CIDR":
parsed, parseErr = RC.NewIPCIDR(payload, target, ruleExtra, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
2020-06-05 17:43:50 +08:00
case "SRC-PORT":
parsed, parseErr = RC.NewPort(payload, target, true, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DST-PORT":
parsed, parseErr = RC.NewPort(payload, target, false, ruleExtra)
2020-07-19 13:17:05 +08:00
case "PROCESS-NAME":
parsed, parseErr = RC.NewProcess(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "MATCH":
parsed = RC.NewMatch(target, ruleExtra)
2021-12-02 22:56:17 +08:00
case "RULE-SET":
parsed, parseErr = RP.NewRuleSet(payload, target, ruleExtra)
case "NETWORK":
parsed, parseErr = RC.NewNetworkType(payload, target)
case "AND":
parsed, parseErr = logic.NewAND(payload, target)
case "OR":
parsed, parseErr = logic.NewOR(payload, target)
case "NOT":
parsed, parseErr = logic.NewNOT(payload, target)
2020-06-05 17:43:50 +08:00
default:
parseErr = fmt.Errorf("unsupported rule type %s", tp)
}
return parsed, parseErr
}