clash/rule/parser.go

55 lines
1.7 KiB
Go
Raw Normal View History

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
)
ruleConfigType := C.RuleConfig(tp)
switch ruleConfigType {
case C.RuleConfigDomain:
2020-06-05 17:43:50 +08:00
parsed = NewDomain(payload, target)
case C.RuleConfigDomainSuffix:
2020-06-05 17:43:50 +08:00
parsed = NewDomainSuffix(payload, target)
case C.RuleConfigDomainKeyword:
2020-06-05 17:43:50 +08:00
parsed = NewDomainKeyword(payload, target)
case C.RuleConfigGeoIP:
2020-06-05 17:43:50 +08:00
noResolve := HasNoResolve(params)
parsed = NewGEOIP(payload, target, noResolve)
case C.RuleConfigIPCIDR, C.RuleConfigIPCIDR6:
2020-06-05 17:43:50 +08:00
noResolve := HasNoResolve(params)
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
case C.RuleConfigSrcIPCIDR:
2020-06-05 17:43:50 +08:00
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
case C.RuleConfigSrcPort:
parsed, parseErr = NewPort(payload, target, PortTypeSrc)
case C.RuleConfigDstPort:
parsed, parseErr = NewPort(payload, target, PortTypeDest)
case C.RuleConfigInboundPort:
parsed, parseErr = NewPort(payload, target, PortTypeInbound)
case C.RuleConfigProcessName:
parsed, parseErr = NewProcess(payload, target, true)
case C.RuleConfigProcessPath:
parsed, parseErr = NewProcess(payload, target, false)
case C.RuleConfigIPSet:
2023-04-22 20:07:47 +08:00
noResolve := HasNoResolve(params)
parsed, parseErr = NewIPSet(payload, target, noResolve)
case C.RuleConfigMatch:
2020-06-05 17:43:50 +08:00
parsed = NewMatch(target)
case C.RuleConfigRuleSet, C.RuleConfigScript:
parseErr = fmt.Errorf("unsupported rule type %s", tp)
2020-06-05 17:43:50 +08:00
default:
parseErr = fmt.Errorf("unsupported rule type %s", tp)
}
return parsed, parseErr
}