mirror of
https://github.com/v2fly/domain-list-community.git
synced 2025-12-19 00:50:04 +07:00
Compare commits
3 Commits
2025121613
...
2025121616
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8bd29ce92 | ||
|
|
72eb885658 | ||
|
|
93bfcfd142 |
38
main.go
38
main.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -22,6 +23,14 @@ var (
|
|||||||
exportLists = flag.String("exportlists", "", "Lists to be flattened and exported in plaintext format, separated by ',' comma")
|
exportLists = flag.String("exportlists", "", "Lists to be flattened and exported in plaintext format, separated by ',' comma")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
RuleTypeDomain string = "domain"
|
||||||
|
RuleTypeFullDomain string = "full"
|
||||||
|
RuleTypeKeyword string = "keyword"
|
||||||
|
RuleTypeRegexp string = "regexp"
|
||||||
|
RuleTypeInclude string = "include"
|
||||||
|
)
|
||||||
|
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
Type string
|
Type string
|
||||||
Value string
|
Value string
|
||||||
@@ -64,30 +73,39 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
|||||||
}
|
}
|
||||||
for _, entry := range l.Entry {
|
for _, entry := range l.Entry {
|
||||||
switch entry.Type {
|
switch entry.Type {
|
||||||
case "domain":
|
case RuleTypeDomain:
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
site.Domain = append(site.Domain, &router.Domain{
|
||||||
Type: router.Domain_RootDomain,
|
Type: router.Domain_RootDomain,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Attribute: entry.Attrs,
|
Attribute: entry.Attrs,
|
||||||
})
|
})
|
||||||
case "regexp":
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
site.Domain = append(site.Domain, &router.Domain{
|
||||||
Type: router.Domain_Regex,
|
Type: router.Domain_Regex,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Attribute: entry.Attrs,
|
Attribute: entry.Attrs,
|
||||||
})
|
})
|
||||||
case "keyword":
|
|
||||||
|
case RuleTypeKeyword:
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
site.Domain = append(site.Domain, &router.Domain{
|
||||||
Type: router.Domain_Plain,
|
Type: router.Domain_Plain,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Attribute: entry.Attrs,
|
Attribute: entry.Attrs,
|
||||||
})
|
})
|
||||||
case "full":
|
|
||||||
|
case RuleTypeFullDomain:
|
||||||
site.Domain = append(site.Domain, &router.Domain{
|
site.Domain = append(site.Domain, &router.Domain{
|
||||||
Type: router.Domain_Full,
|
Type: router.Domain_Full,
|
||||||
Value: entry.Value,
|
Value: entry.Value,
|
||||||
Attribute: entry.Attrs,
|
Attribute: entry.Attrs,
|
||||||
})
|
})
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unknown domain type: " + entry.Type)
|
return nil, errors.New("unknown domain type: " + entry.Type)
|
||||||
}
|
}
|
||||||
@@ -118,14 +136,20 @@ func removeComment(line string) string {
|
|||||||
func parseDomain(domain string, entry *Entry) error {
|
func parseDomain(domain string, entry *Entry) error {
|
||||||
kv := strings.Split(domain, ":")
|
kv := strings.Split(domain, ":")
|
||||||
if len(kv) == 1 {
|
if len(kv) == 1 {
|
||||||
entry.Type = "domain"
|
entry.Type = RuleTypeDomain
|
||||||
entry.Value = strings.ToLower(kv[0])
|
entry.Value = strings.ToLower(kv[0])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(kv) == 2 {
|
if len(kv) == 2 {
|
||||||
entry.Type = strings.ToLower(kv[0])
|
entry.Type = strings.ToLower(kv[0])
|
||||||
entry.Value = strings.ToLower(kv[1])
|
|
||||||
|
if strings.EqualFold(entry.Type, RuleTypeRegexp) {
|
||||||
|
entry.Value = kv[1]
|
||||||
|
} else {
|
||||||
|
entry.Value = strings.ToLower(kv[1])
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,7 +279,7 @@ func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
|
|||||||
newEntryList := make([]Entry, 0, len(entryList))
|
newEntryList := make([]Entry, 0, len(entryList))
|
||||||
hasInclude := false
|
hasInclude := false
|
||||||
for _, entry := range entryList {
|
for _, entry := range entryList {
|
||||||
if entry.Type == "include" {
|
if entry.Type == RuleTypeInclude {
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user