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
|
|
|
|
)
|
|
|
|
|
2023-08-16 11:06:30 +08:00
|
|
|
ruleConfigType := C.RuleConfig(tp)
|
|
|
|
|
|
|
|
switch ruleConfigType {
|
|
|
|
case C.RuleConfigDomain:
|
2020-06-05 17:43:50 +08:00
|
|
|
parsed = NewDomain(payload, target)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigDomainSuffix:
|
2020-06-05 17:43:50 +08:00
|
|
|
parsed = NewDomainSuffix(payload, target)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigDomainKeyword:
|
2020-06-05 17:43:50 +08:00
|
|
|
parsed = NewDomainKeyword(payload, target)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigGeoIP:
|
2020-06-05 17:43:50 +08:00
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed = NewGEOIP(payload, target, noResolve)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigIPCIDR, C.RuleConfigIPCIDR6:
|
2020-06-05 17:43:50 +08:00
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigSrcIPCIDR:
|
2020-06-05 17:43:50 +08:00
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigSrcPort:
|
2023-08-03 22:30:08 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeSrc)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigDstPort:
|
2023-08-03 22:30:08 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeDest)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigInboundPort:
|
2023-08-03 22:30:08 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, PortTypeInbound)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigProcessName:
|
2022-03-12 19:07:53 +08:00
|
|
|
parsed, parseErr = NewProcess(payload, target, true)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigProcessPath:
|
2022-03-12 19:07:53 +08:00
|
|
|
parsed, parseErr = NewProcess(payload, target, false)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigIPSet:
|
2023-04-22 20:07:47 +08:00
|
|
|
noResolve := HasNoResolve(params)
|
|
|
|
parsed, parseErr = NewIPSet(payload, target, noResolve)
|
2023-08-16 11:06:30 +08:00
|
|
|
case C.RuleConfigMatch:
|
2020-06-05 17:43:50 +08:00
|
|
|
parsed = NewMatch(target)
|
2023-08-16 11:06:30 +08:00
|
|
|
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
|
|
|
|
}
|