From c73423095234e71a298375c78e717b4c5eb39d79 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Sat, 9 Dec 2023 22:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B2=A1=E6=9C=89=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/release-rule-set.sh | 6 +++++- adguardsdnsfilter.go | 14 ++++++++------ main.go | 3 ++- ruleset.go | 7 +++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/release-rule-set.sh b/.github/release-rule-set.sh index 3291066..de264a5 100644 --- a/.github/release-rule-set.sh +++ b/.github/release-rule-set.sh @@ -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 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 git init diff --git a/adguardsdnsfilter.go b/adguardsdnsfilter.go index b906991..4ee72c1 100644 --- a/adguardsdnsfilter.go +++ b/adguardsdnsfilter.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "maps" "net/http" "slices" "strings" @@ -16,10 +17,10 @@ import ( 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) if err != nil { - return nil, fmt.Errorf("adguard: %w", err) + return nil, nil, fmt.Errorf("adguard: %w", err) } domain := map[string]struct{}{} domainRegex := map[string]struct{}{} @@ -67,16 +68,17 @@ func adguard(ctx context.Context, c *http.Client) (*Ruleset, error) { domainSuffix["."+k] = struct{}{} } - r := Ruleset{} - r.Version = 1 - r.Rules = []map[string][]any{ + rules := []map[string][]any{ { "domain": toAny(domain), "domain_suffix": toAny(domainSuffix), "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 { diff --git a/main.go b/main.go index b058f40..f813ca0 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,13 @@ import ( ) func main() { - adg, err := adguard(context.Background(), &http.Client{}) + adg, adgNoReg, err := adguard(context.Background(), &http.Client{}) if err != nil { panic(err) } os.MkdirAll("output", 0777) write("output/AdGuardSDNSFilter.json", adg) + write("output/AdGuardSDNSFilter-NoRegex.json", adgNoReg) } func write(name string, ruleSet *Ruleset) { diff --git a/ruleset.go b/ruleset.go index fb55b32..349c9a1 100644 --- a/ruleset.go +++ b/ruleset.go @@ -4,3 +4,10 @@ type Ruleset struct { Rules []map[string][]any `json:"rules"` Version int `json:"version"` } + +func NewRuleSet(rules []map[string][]any) *Ruleset { + return &Ruleset{ + Rules: rules, + Version: 1, + } +}