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 |
36
main.go
36
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -22,6 +23,14 @@ var (
|
||||
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 string
|
||||
Value string
|
||||
@@ -64,30 +73,39 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
||||
}
|
||||
for _, entry := range l.Entry {
|
||||
switch entry.Type {
|
||||
case "domain":
|
||||
case RuleTypeDomain:
|
||||
site.Domain = append(site.Domain, &router.Domain{
|
||||
Type: router.Domain_RootDomain,
|
||||
Value: entry.Value,
|
||||
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{
|
||||
Type: router.Domain_Regex,
|
||||
Value: entry.Value,
|
||||
Attribute: entry.Attrs,
|
||||
})
|
||||
case "keyword":
|
||||
|
||||
case RuleTypeKeyword:
|
||||
site.Domain = append(site.Domain, &router.Domain{
|
||||
Type: router.Domain_Plain,
|
||||
Value: entry.Value,
|
||||
Attribute: entry.Attrs,
|
||||
})
|
||||
case "full":
|
||||
|
||||
case RuleTypeFullDomain:
|
||||
site.Domain = append(site.Domain, &router.Domain{
|
||||
Type: router.Domain_Full,
|
||||
Value: entry.Value,
|
||||
Attribute: entry.Attrs,
|
||||
})
|
||||
|
||||
default:
|
||||
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 {
|
||||
kv := strings.Split(domain, ":")
|
||||
if len(kv) == 1 {
|
||||
entry.Type = "domain"
|
||||
entry.Type = RuleTypeDomain
|
||||
entry.Value = strings.ToLower(kv[0])
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(kv) == 2 {
|
||||
entry.Type = strings.ToLower(kv[0])
|
||||
|
||||
if strings.EqualFold(entry.Type, RuleTypeRegexp) {
|
||||
entry.Value = kv[1]
|
||||
} else {
|
||||
entry.Value = strings.ToLower(kv[1])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -255,7 +279,7 @@ func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
|
||||
newEntryList := make([]Entry, 0, len(entryList))
|
||||
hasInclude := false
|
||||
for _, entry := range entryList {
|
||||
if entry.Type == "include" {
|
||||
if entry.Type == RuleTypeInclude {
|
||||
refName := strings.ToUpper(entry.Value)
|
||||
if entry.Attrs != nil {
|
||||
for _, attr := range entry.Attrs {
|
||||
|
||||
Reference in New Issue
Block a user