Fix: IP-CIDR invalid payload crash

This commit is contained in:
Dreamacro 2019-03-30 14:11:59 +08:00
parent 531f487629
commit 2036f8cb7a
2 changed files with 20 additions and 8 deletions

View File

@ -339,28 +339,39 @@ func parseRules(cfg *rawConfig) ([]C.Rule, error) {
payload = rule[1] payload = rule[1]
target = rule[2] target = rule[2]
default: default:
return nil, fmt.Errorf("Rules[%d] [- %s] error: format invalid", idx, line) return nil, fmt.Errorf("Rules[%d] [%s] error: format invalid", idx, line)
} }
rule = trimArr(rule) rule = trimArr(rule)
var parsed C.Rule
switch rule[0] { switch rule[0] {
case "DOMAIN": case "DOMAIN":
rules = append(rules, R.NewDomain(payload, target)) parsed = R.NewDomain(payload, target)
case "DOMAIN-SUFFIX": case "DOMAIN-SUFFIX":
rules = append(rules, R.NewDomainSuffix(payload, target)) parsed = R.NewDomainSuffix(payload, target)
case "DOMAIN-KEYWORD": case "DOMAIN-KEYWORD":
rules = append(rules, R.NewDomainKeyword(payload, target)) parsed = R.NewDomainKeyword(payload, target)
case "GEOIP": case "GEOIP":
rules = append(rules, R.NewGEOIP(payload, target)) parsed = R.NewGEOIP(payload, target)
case "IP-CIDR", "IP-CIDR6": case "IP-CIDR", "IP-CIDR6":
rules = append(rules, R.NewIPCIDR(payload, target, false)) if rule := R.NewIPCIDR(payload, target, false); rule != nil {
parsed = rule
}
case "SOURCE-IP-CIDR": case "SOURCE-IP-CIDR":
rules = append(rules, R.NewIPCIDR(payload, target, true)) if rule := R.NewIPCIDR(payload, target, true); rule != nil {
parsed = rule
}
case "MATCH": case "MATCH":
fallthrough fallthrough
case "FINAL": case "FINAL":
rules = append(rules, R.NewMatch(target)) parsed = R.NewMatch(target)
} }
if parsed == nil {
return nil, fmt.Errorf("Rules[%d] [%s] error: payload invalid", idx, line)
}
rules = append(rules, parsed)
} }
return rules, nil return rules, nil

View File

@ -38,6 +38,7 @@ func (i *IPCIDR) Payload() string {
func NewIPCIDR(s string, adapter string, isSourceIP bool) *IPCIDR { func NewIPCIDR(s string, adapter string, isSourceIP bool) *IPCIDR {
_, ipnet, err := net.ParseCIDR(s) _, ipnet, err := net.ParseCIDR(s)
if err != nil { if err != nil {
return nil
} }
return &IPCIDR{ return &IPCIDR{
ipnet: ipnet, ipnet: ipnet,