Compare commits

...

3 Commits

Author SHA1 Message Date
Loyalsoldier
d8bd29ce92 Fix: incorrect lowercase for regexp rules (#3066) 2025-12-17 00:17:43 +08:00
Loyalsoldier
72eb885658 Refine: extract rule type (#3065) 2025-12-17 00:10:33 +08:00
风扇滑翔翼
93bfcfd142 Feat: check regexp before build (#3064)
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2025-12-16 23:40:25 +08:00

38
main.go
View File

@@ -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 {