Skip to content

Commit

Permalink
Fix bad data
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 30, 2023
1 parent e6c443b commit 563f703
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"

"github.com/sagernet/sing-box/common/geosite"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 563f703

Please sign in to comment.