添加没有正则的版本

This commit is contained in:
xmdhs 2023-12-09 22:23:30 +08:00
parent 636d973b6b
commit c734230952
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
4 changed files with 22 additions and 8 deletions

View File

@ -7,7 +7,11 @@ set -e -o pipefail
wget https://github.com/SagerNet/sing-box/releases/download/v1.8.0-alpha.11/sing-box-1.8.0-alpha.11-linux-amd64.tar.gz wget https://github.com/SagerNet/sing-box/releases/download/v1.8.0-alpha.11/sing-box-1.8.0-alpha.11-linux-amd64.tar.gz
tar -zxvf sing-box-1.8.0-alpha.11-linux-amd64.tar.gz tar -zxvf sing-box-1.8.0-alpha.11-linux-amd64.tar.gz
sing-box-1.8.0-alpha.11-linux-amd64/sing-box rule-set compile output/AdGuardSDNSFilter.json
for file in output/*.json; do
sing-box-1.8.0-alpha.11-linux-amd64/sing-box rule-set compile "$file"
done
cd output cd output
git init git init

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"maps"
"net/http" "net/http"
"slices" "slices"
"strings" "strings"
@ -16,10 +17,10 @@ import (
const AdGuardSDNSFilter = "https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt" const AdGuardSDNSFilter = "https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt"
func adguard(ctx context.Context, c *http.Client) (*Ruleset, error) { func adguard(ctx context.Context, c *http.Client) (hasReg *Ruleset, noReg *Ruleset, err error) {
b, err := getFilter(ctx, c) b, err := getFilter(ctx, c)
if err != nil { if err != nil {
return nil, fmt.Errorf("adguard: %w", err) return nil, nil, fmt.Errorf("adguard: %w", err)
} }
domain := map[string]struct{}{} domain := map[string]struct{}{}
domainRegex := map[string]struct{}{} domainRegex := map[string]struct{}{}
@ -67,16 +68,17 @@ func adguard(ctx context.Context, c *http.Client) (*Ruleset, error) {
domainSuffix["."+k] = struct{}{} domainSuffix["."+k] = struct{}{}
} }
r := Ruleset{} rules := []map[string][]any{
r.Version = 1
r.Rules = []map[string][]any{
{ {
"domain": toAny(domain), "domain": toAny(domain),
"domain_suffix": toAny(domainSuffix), "domain_suffix": toAny(domainSuffix),
"domain_regex": toAny(domainRegex), "domain_regex": toAny(domainRegex),
}, },
} }
return &r, nil noRegRules := maps.Clone(rules[0])
delete(noRegRules, "domain_regex")
return NewRuleSet(rules), NewRuleSet([]map[string][]any{noRegRules}), nil
} }
func toAny(m map[string]struct{}) []any { func toAny(m map[string]struct{}) []any {

View File

@ -8,12 +8,13 @@ import (
) )
func main() { func main() {
adg, err := adguard(context.Background(), &http.Client{}) adg, adgNoReg, err := adguard(context.Background(), &http.Client{})
if err != nil { if err != nil {
panic(err) panic(err)
} }
os.MkdirAll("output", 0777) os.MkdirAll("output", 0777)
write("output/AdGuardSDNSFilter.json", adg) write("output/AdGuardSDNSFilter.json", adg)
write("output/AdGuardSDNSFilter-NoRegex.json", adgNoReg)
} }
func write(name string, ruleSet *Ruleset) { func write(name string, ruleSet *Ruleset) {

View File

@ -4,3 +4,10 @@ type Ruleset struct {
Rules []map[string][]any `json:"rules"` Rules []map[string][]any `json:"rules"`
Version int `json:"version"` Version int `json:"version"`
} }
func NewRuleSet(rules []map[string][]any) *Ruleset {
return &Ruleset{
Rules: rules,
Version: 1,
}
}