sort and fix some reg

This commit is contained in:
xmdhs 2023-12-05 09:59:27 +08:00
parent d8d79fc56d
commit 636d973b6b
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"slices"
"strings"
"github.com/AdguardTeam/urlfilter/filterlist"
@ -54,7 +55,9 @@ func adguard(ctx context.Context, c *http.Client) (*Ruleset, error) {
ruleR := strings.TrimPrefix(rule, "://")
ruleR = strings.ReplaceAll(ruleR, ".", `\.`)
reg := strings.ReplaceAll(ruleR, "*", ".*")
reg = `^(.*\.)?` + reg
if !strings.HasPrefix(hr.RuleText, "*") {
reg = `^(.*\.)?` + reg
}
if strings.HasSuffix(hr.RuleText, "^") {
reg = reg + "$"
}
@ -68,14 +71,20 @@ func adguard(ctx context.Context, c *http.Client) (*Ruleset, error) {
r.Version = 1
r.Rules = []map[string][]any{
{
"domain": lo.Map[string, any](lo.Keys(domain), func(item string, index int) any { return item }),
"domain_suffix": lo.Map[string, any](lo.Keys(domainSuffix), func(item string, index int) any { return item }),
"domain_regex": lo.Map[string, any](lo.Keys(domainRegex), func(item string, index int) any { return item }),
"domain": toAny(domain),
"domain_suffix": toAny(domainSuffix),
"domain_regex": toAny(domainRegex),
},
}
return &r, nil
}
func toAny(m map[string]struct{}) []any {
sl := lo.Keys(m)
slices.Sort(sl)
return lo.Map[string, any](sl, func(item string, index int) any { return item })
}
func getFilter(ctx context.Context, c *http.Client) ([]byte, error) {
reps, err := http.NewRequestWithContext(ctx, "GET", AdGuardSDNSFilter, nil)
if err != nil {