Refine router config parse

This commit is contained in:
秋のかえで 2021-04-08 13:19:25 +08:00
parent 68201a8898
commit 44317bd657
No known key found for this signature in database
GPG Key ID: C687746B27587C0D
3 changed files with 26 additions and 8 deletions

View File

@ -44,20 +44,36 @@ func ParseDomainRule(domain string) ([]*dm.Domain, error) {
domainRule := new(dm.Domain) domainRule := new(dm.Domain)
switch { switch {
case strings.HasPrefix(domain, "regexp:"): case strings.HasPrefix(domain, "regexp:"):
regexpVal := domain[7:]
if len(regexpVal) == 0 {
return nil, newError("empty regexp type of rule: ", domain)
}
domainRule.Type = dm.MatchingType_Regex domainRule.Type = dm.MatchingType_Regex
domainRule.Value = domain[7:] domainRule.Value = regexpVal
case strings.HasPrefix(domain, "domain:"): case strings.HasPrefix(domain, "domain:"):
domainName := domain[7:]
if len(domainName) == 0 {
return nil, newError("empty domain type of rule: ", domain)
}
domainRule.Type = dm.MatchingType_Subdomain domainRule.Type = dm.MatchingType_Subdomain
domainRule.Value = domain[7:] domainRule.Value = domainName
case strings.HasPrefix(domain, "full:"): case strings.HasPrefix(domain, "full:"):
fullVal := domain[5:]
if len(fullVal) == 0 {
return nil, newError("empty full domain type of rule: ", domain)
}
domainRule.Type = dm.MatchingType_Full domainRule.Type = dm.MatchingType_Full
domainRule.Value = domain[5:] domainRule.Value = fullVal
case strings.HasPrefix(domain, "keyword:"): case strings.HasPrefix(domain, "keyword:"):
keywordVal := domain[8:]
if len(keywordVal) == 0 {
return nil, newError("empty keyword type of rule: ", domain)
}
domainRule.Type = dm.MatchingType_Keyword domainRule.Type = dm.MatchingType_Keyword
domainRule.Value = domain[8:] domainRule.Value = keywordVal
case strings.HasPrefix(domain, "dotless:"): case strings.HasPrefix(domain, "dotless:"):
domainRule.Type = dm.MatchingType_Regex domainRule.Type = dm.MatchingType_Regex

View File

@ -1,5 +1,7 @@
package geosite package geosite
import "strings"
type AttributeList struct { type AttributeList struct {
matcher []AttributeMatcher matcher []AttributeMatcher
} }
@ -25,7 +27,7 @@ type BooleanMatcher string
func (m BooleanMatcher) Match(domain *Domain) bool { func (m BooleanMatcher) Match(domain *Domain) bool {
for _, attr := range domain.Attribute { for _, attr := range domain.Attribute {
if attr.Key == string(m) { if strings.EqualFold(attr.GetKey(), string(m)) {
return true return true
} }
} }

View File

@ -261,21 +261,21 @@ func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
if err != nil { if err != nil {
return nil, newError("invalid router rule").Base(err) return nil, newError("invalid router rule").Base(err)
} }
if rawRule.Type == "field" { if strings.EqualFold(rawRule.Type, "field") {
fieldrule, err := parseFieldRule(msg) fieldrule, err := parseFieldRule(msg)
if err != nil { if err != nil {
return nil, newError("invalid field rule").Base(err) return nil, newError("invalid field rule").Base(err)
} }
return fieldrule, nil return fieldrule, nil
} }
if rawRule.Type == "chinaip" { if strings.EqualFold(rawRule.Type, "chinaip") {
chinaiprule, err := parseChinaIPRule(msg) chinaiprule, err := parseChinaIPRule(msg)
if err != nil { if err != nil {
return nil, newError("invalid chinaip rule").Base(err) return nil, newError("invalid chinaip rule").Base(err)
} }
return chinaiprule, nil return chinaiprule, nil
} }
if rawRule.Type == "chinasites" { if strings.EqualFold(rawRule.Type, "chinasites") {
chinasitesrule, err := parseChinaSitesRule(msg) chinasitesrule, err := parseChinaSitesRule(msg)
if err != nil { if err != nil {
return nil, newError("invalid chinasites rule").Base(err) return nil, newError("invalid chinasites rule").Base(err)