mirror of
https://github.com/SagerNet/sing-geosite.git
synced 2025-12-17 06:43:13 +08:00
Compare commits
3 Commits
2023121914
...
2024012311
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c72fc7caba | ||
|
|
fc71b5c331 | ||
|
|
563f703dde |
117
main.go
117
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/geosite"
|
"github.com/sagernet/sing-box/common/geosite"
|
||||||
@@ -169,6 +170,114 @@ func parse(vGeositeData []byte) (map[string][]geosite.Item, error) {
|
|||||||
return domainMap, nil
|
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 mergeTags(data map[string][]geosite.Item) {
|
||||||
|
var codeList []string
|
||||||
|
for code := range data {
|
||||||
|
codeList = append(codeList, code)
|
||||||
|
}
|
||||||
|
var cnCodeList []string
|
||||||
|
for _, code := range codeList {
|
||||||
|
codeParts := strings.Split(code, "@")
|
||||||
|
if len(codeParts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if codeParts[1] != "cn" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(codeParts[0], "category-") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(codeParts[0], "-cn") || strings.HasSuffix(codeParts[0], "-!cn") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cnCodeList = append(cnCodeList, code)
|
||||||
|
}
|
||||||
|
newMap := make(map[geosite.Item]bool)
|
||||||
|
for _, item := range data["cn"] {
|
||||||
|
newMap[item] = true
|
||||||
|
}
|
||||||
|
for _, code := range cnCodeList {
|
||||||
|
for _, item := range data[code] {
|
||||||
|
newMap[item] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newList := make([]geosite.Item, 0, len(newMap))
|
||||||
|
for item := range newMap {
|
||||||
|
newList = append(newList, item)
|
||||||
|
}
|
||||||
|
data["cn"] = newList
|
||||||
|
println("merged cn categories: " + strings.Join(cnCodeList, ","))
|
||||||
|
}
|
||||||
|
|
||||||
func generate(release *github.RepositoryRelease, output string, cnOutput string, ruleSetOutput string) error {
|
func generate(release *github.RepositoryRelease, output string, cnOutput string, ruleSetOutput string) error {
|
||||||
vData, err := download(release)
|
vData, err := download(release)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -178,6 +287,8 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
filterTags(domainMap)
|
||||||
|
mergeTags(domainMap)
|
||||||
outputPath, _ := filepath.Abs(output)
|
outputPath, _ := filepath.Abs(output)
|
||||||
os.Stderr.WriteString("write " + outputPath + "\n")
|
os.Stderr.WriteString("write " + outputPath + "\n")
|
||||||
outputFile, err := os.Create(output)
|
outputFile, err := os.Create(output)
|
||||||
@@ -190,9 +301,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cnCodes := []string{
|
cnCodes := []string{
|
||||||
"cn",
|
"geolocation-cn",
|
||||||
"geolocation-!cn",
|
|
||||||
"category-companies@cn",
|
|
||||||
}
|
}
|
||||||
cnDomainMap := make(map[string][]geosite.Item)
|
cnDomainMap := make(map[string][]geosite.Item)
|
||||||
for _, cnCode := range cnCodes {
|
for _, cnCode := range cnCodes {
|
||||||
@@ -227,7 +336,7 @@ func generate(release *github.RepositoryRelease, output string, cnOutput string,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
srsPath, _ := filepath.Abs(filepath.Join(ruleSetOutput, "geosite-"+code+".srs"))
|
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)
|
outputRuleSet, err := os.Create(srsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user