2021-05-03 22:15:11 -04:00
|
|
|
package geodata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
2021-05-03 22:15:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type AttributeList struct {
|
|
|
|
matcher []AttributeMatcher
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:13:58 -04:00
|
|
|
func (al *AttributeList) Match(domain *routercommon.Domain) bool {
|
2021-05-03 22:15:11 -04:00
|
|
|
for _, matcher := range al.matcher {
|
|
|
|
if !matcher.Match(domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (al *AttributeList) IsEmpty() bool {
|
|
|
|
return len(al.matcher) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAttrs(attrs []string) *AttributeList {
|
|
|
|
al := new(AttributeList)
|
|
|
|
for _, attr := range attrs {
|
|
|
|
trimmedAttr := strings.ToLower(strings.TrimSpace(attr))
|
|
|
|
if len(trimmedAttr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
al.matcher = append(al.matcher, BooleanMatcher(trimmedAttr))
|
|
|
|
}
|
|
|
|
return al
|
|
|
|
}
|
|
|
|
|
|
|
|
type AttributeMatcher interface {
|
2021-09-07 04:13:58 -04:00
|
|
|
Match(*routercommon.Domain) bool
|
2021-05-03 22:15:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type BooleanMatcher string
|
|
|
|
|
2021-09-07 04:13:58 -04:00
|
|
|
func (m BooleanMatcher) Match(domain *routercommon.Domain) bool {
|
2021-05-03 22:15:11 -04:00
|
|
|
for _, attr := range domain.Attribute {
|
|
|
|
if strings.EqualFold(attr.GetKey(), string(m)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|