diff --git a/main.go b/main.go index b1060bc97..8111365f7 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "sort" "strings" "github.com/sagernet/sing-box/common/geosite" @@ -169,6 +170,75 @@ func parse(vGeositeData []byte) (map[string][]geosite.Item, error) { return domainMap, nil } +type filteredCodePair struct { + code string + badCode string +} + +func filterTags(data map[string][]geosite.Item) { + var codeList []string + for code := range data { + codeList = append(codeList, code) + } + var badCodeList []filteredCodePair + var filteredCodeMap []string + var mergedCodeMap []string + for _, code := range codeList { + codeParts := strings.Split(code, "@") + if len(codeParts) != 2 { + continue + } + leftParts := strings.Split(codeParts[0], "-") + var lastName string + if len(leftParts) > 1 { + lastName = leftParts[len(leftParts)-1] + } + if lastName == "" { + lastName = codeParts[0] + } + if lastName == codeParts[1] { + delete(data, code) + filteredCodeMap = append(filteredCodeMap, code) + continue + } + if "!"+lastName == codeParts[1] { + badCodeList = append(badCodeList, filteredCodePair{ + code: codeParts[0], + badCode: code, + }) + } else if lastName == "!"+codeParts[1] { + badCodeList = append(badCodeList, filteredCodePair{ + code: codeParts[0], + badCode: code, + }) + } + } + for _, it := range badCodeList { + badList := data[it.badCode] + if badList == nil { + panic("bad list not found: " + it.badCode) + } + delete(data, it.badCode) + newMap := make(map[geosite.Item]bool) + for _, item := range data[it.code] { + newMap[item] = true + } + for _, item := range badList { + delete(newMap, item) + } + newList := make([]geosite.Item, 0, len(newMap)) + for item := range newMap { + newList = append(newList, item) + } + data[it.code] = newList + mergedCodeMap = append(mergedCodeMap, it.badCode) + } + sort.Strings(filteredCodeMap) + sort.Strings(mergedCodeMap) + os.Stderr.WriteString("filtered " + strings.Join(filteredCodeMap, ",") + "\n") + os.Stderr.WriteString("merged " + strings.Join(mergedCodeMap, ",") + "\n") +} + func generate(release *github.RepositoryRelease, output string, cnOutput string, ruleSetOutput string) error { vData, err := download(release) if err != nil { @@ -178,6 +248,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string, if err != nil { return err } + filterTags(domainMap) outputPath, _ := filepath.Abs(output) os.Stderr.WriteString("write " + outputPath + "\n") outputFile, err := os.Create(output) @@ -227,7 +298,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string, }, } srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".srs")) - os.Stderr.WriteString("write " + srsPath + "\n") + //os.Stderr.WriteString("write " + srsPath + "\n") outputRuleSet, err := os.Create(srsPath) if err != nil { return err