mirror of
https://github.com/v2fly/domain-list-community.git
synced 2026-01-07 07:13:57 +07:00
Refactor: use string attr before toProto
This commit is contained in:
90
main.go
90
main.go
@@ -32,7 +32,7 @@ const (
|
|||||||
type Entry struct {
|
type Entry struct {
|
||||||
Type string
|
Type string
|
||||||
Value string
|
Value string
|
||||||
Attrs []*router.Domain_Attribute
|
Attrs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type List struct {
|
type List struct {
|
||||||
@@ -51,10 +51,7 @@ func (l *ParsedList) toPlainText(listName string) error {
|
|||||||
for _, entry := range l.Entry {
|
for _, entry := range l.Entry {
|
||||||
var attrString string
|
var attrString string
|
||||||
if entry.Attrs != nil {
|
if entry.Attrs != nil {
|
||||||
for _, attr := range entry.Attrs {
|
attrString = ":@"+strings.Join(entry.Attrs, ",@")
|
||||||
attrString += "@" + attr.GetKey() + ","
|
|
||||||
}
|
|
||||||
attrString = strings.TrimRight(":"+attrString, ",")
|
|
||||||
}
|
}
|
||||||
// Entry output format is: type:domain.tld:@attr1,@attr2
|
// Entry output format is: type:domain.tld:@attr1,@attr2
|
||||||
entryBytes = append(entryBytes, []byte(entry.Type+":"+entry.Value+attrString+"\n")...)
|
entryBytes = append(entryBytes, []byte(entry.Type+":"+entry.Value+attrString+"\n")...)
|
||||||
@@ -70,43 +67,33 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
|||||||
CountryCode: l.Name,
|
CountryCode: l.Name,
|
||||||
}
|
}
|
||||||
for _, entry := range l.Entry {
|
for _, entry := range l.Entry {
|
||||||
switch entry.Type {
|
|
||||||
case RuleTypeDomain:
|
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
|
||||||
Type: router.Domain_RootDomain,
|
|
||||||
Value: entry.Value,
|
|
||||||
Attribute: entry.Attrs,
|
|
||||||
})
|
|
||||||
|
|
||||||
case RuleTypeRegexp:
|
pdomain := &router.Domain{Value:entry.Value}
|
||||||
// check regexp validity to avoid runtime error
|
for _, attr := range entry.Attrs {
|
||||||
_, err := regexp.Compile(entry.Value)
|
pdomain.Attribute = append(pdomain.Attribute, &router.Domain_Attribute{
|
||||||
if err != nil {
|
Key: attr,
|
||||||
return nil, fmt.Errorf("invalid regexp in list %s: %s", l.Name, entry.Value)
|
TypedValue: &router.Domain_Attribute_BoolValue{BoolValue: true},
|
||||||
}
|
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
|
||||||
Type: router.Domain_Regex,
|
|
||||||
Value: entry.Value,
|
|
||||||
Attribute: entry.Attrs,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
case RuleTypeKeyword:
|
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
|
||||||
Type: router.Domain_Plain,
|
|
||||||
Value: entry.Value,
|
|
||||||
Attribute: entry.Attrs,
|
|
||||||
})
|
|
||||||
|
|
||||||
case RuleTypeFullDomain:
|
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
|
||||||
Type: router.Domain_Full,
|
|
||||||
Value: entry.Value,
|
|
||||||
Attribute: entry.Attrs,
|
|
||||||
})
|
|
||||||
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unknown domain type: %s", entry.Type)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch entry.Type {
|
||||||
|
case RuleTypeDomain:
|
||||||
|
pdomain.Type = router.Domain_RootDomain
|
||||||
|
case RuleTypeRegexp:
|
||||||
|
// check regexp validity to avoid runtime error
|
||||||
|
_, err := regexp.Compile(entry.Value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid regexp in list %s: %s", l.Name, entry.Value)
|
||||||
|
}
|
||||||
|
pdomain.Type = router.Domain_Regex
|
||||||
|
case RuleTypeKeyword:
|
||||||
|
pdomain.Type = router.Domain_Plain
|
||||||
|
case RuleTypeFullDomain:
|
||||||
|
pdomain.Type = router.Domain_Full
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown domain type: %s", entry.Type)
|
||||||
|
}
|
||||||
|
site.Domain = append(site.Domain, pdomain)
|
||||||
}
|
}
|
||||||
return site, nil
|
return site, nil
|
||||||
}
|
}
|
||||||
@@ -146,15 +133,14 @@ func parseDomain(domain string, entry *Entry) error {
|
|||||||
return fmt.Errorf("invalid format: %s", domain)
|
return fmt.Errorf("invalid format: %s", domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAttribute(attr string) (*router.Domain_Attribute, error) {
|
func parseAttribute(attr string) (string, error) {
|
||||||
var attribute router.Domain_Attribute
|
var attribute string
|
||||||
if len(attr) == 0 || attr[0] != '@' {
|
if len(attr) == 0 || attr[0] != '@' {
|
||||||
return &attribute, fmt.Errorf("invalid attribute: %s", attr)
|
return attribute, fmt.Errorf("invalid attribute: %s", attr)
|
||||||
}
|
}
|
||||||
|
|
||||||
attribute.Key = strings.ToLower(attr[1:]) // Trim attribute prefix `@` character
|
attribute = strings.ToLower(attr[1:]) // Trim attribute prefix `@` character
|
||||||
attribute.TypedValue = &router.Domain_Attribute_BoolValue{BoolValue: true}
|
return attribute, nil
|
||||||
return &attribute, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseEntry(line string) (Entry, error) {
|
func parseEntry(line string) (Entry, error) {
|
||||||
@@ -211,7 +197,7 @@ func Load(path string) (*List, error) {
|
|||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
|
func isMatchAttr(Attrs []string, includeKey string) bool {
|
||||||
isMatch := false
|
isMatch := false
|
||||||
mustMatch := true
|
mustMatch := true
|
||||||
matchName := includeKey
|
matchName := includeKey
|
||||||
@@ -222,14 +208,13 @@ func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, Attr := range Attrs {
|
for _, Attr := range Attrs {
|
||||||
attrName := Attr.Key
|
|
||||||
if mustMatch {
|
if mustMatch {
|
||||||
if matchName == attrName {
|
if matchName == Attr {
|
||||||
isMatch = true
|
isMatch = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if matchName == attrName {
|
if matchName == Attr {
|
||||||
isMatch = false
|
isMatch = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -238,11 +223,10 @@ func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
|
|||||||
return isMatch
|
return isMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
func createIncludeAttrEntrys(list *List, matchAttr *router.Domain_Attribute) []Entry {
|
func createIncludeAttrEntrys(list *List, matchAttr string) []Entry {
|
||||||
newEntryList := make([]Entry, 0, len(list.Entry))
|
newEntryList := make([]Entry, 0, len(list.Entry))
|
||||||
matchName := matchAttr.Key
|
|
||||||
for _, entry := range list.Entry {
|
for _, entry := range list.Entry {
|
||||||
matched := isMatchAttr(entry.Attrs, matchName)
|
matched := isMatchAttr(entry.Attrs, matchAttr)
|
||||||
if matched {
|
if matched {
|
||||||
newEntryList = append(newEntryList, entry)
|
newEntryList = append(newEntryList, entry)
|
||||||
}
|
}
|
||||||
@@ -264,7 +248,7 @@ func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
|
|||||||
refName := strings.ToUpper(entry.Value)
|
refName := strings.ToUpper(entry.Value)
|
||||||
if entry.Attrs != nil {
|
if entry.Attrs != nil {
|
||||||
for _, attr := range entry.Attrs {
|
for _, attr := range entry.Attrs {
|
||||||
InclusionName := strings.ToUpper(refName + "@" + attr.Key)
|
InclusionName := strings.ToUpper(refName + "@" + attr)
|
||||||
if pl.Inclusion[InclusionName] {
|
if pl.Inclusion[InclusionName] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user