From b0cc8dca91e4444425ceda5325e94a93ad6ecece Mon Sep 17 00:00:00 2001 From: tr4v3ler <0xtr4v3ler@gmail.com> Date: Sat, 11 Apr 2026 15:07:06 +0800 Subject: [PATCH] Avoid merging category-companies@cn into geolocation-cn. Keep company-wide @cn buckets like google@cn out of the generic mainland direct list so generated geolocation-cn does not classify blocked Google service domains as direct-connect targets. Made-with: Cursor --- main.go | 10 ++++++++++ main_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 main_test.go diff --git a/main.go b/main.go index 65eae47..e598910 100644 --- a/main.go +++ b/main.go @@ -246,6 +246,13 @@ func mergeTags(data map[string][]geosite.Item) { codeList = append(codeList, code) } var cnCodeList []string + // Company-wide @cn buckets may contain domains that are still unsuitable for + // a generic mainland-direct list. For example, category-companies@cn pulls in + // google@cn entries such as ssl.gstatic.com and fonts.googleapis.com, which + // makes geolocation-cn too broad for common direct-routing use. + geolocationCNMergeExclusions := map[string]bool{ + "category-companies@cn": true, + } for _, code := range codeList { codeParts := strings.Split(code, "@") if len(codeParts) != 2 { @@ -260,6 +267,9 @@ func mergeTags(data map[string][]geosite.Item) { if strings.HasSuffix(codeParts[0], "-cn") || strings.HasSuffix(codeParts[0], "-!cn") { continue } + if geolocationCNMergeExclusions[code] { + continue + } cnCodeList = append(cnCodeList, code) } for _, code := range codeList { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..8d67543 --- /dev/null +++ b/main_test.go @@ -0,0 +1,41 @@ +package main + +import ( + "testing" + + "github.com/sagernet/sing-box/common/geosite" +) + +func hasItem(items []geosite.Item, want geosite.Item) bool { + for _, item := range items { + if item == want { + return true + } + } + return false +} + +func TestMergeTagsSkipsCategoryCompaniesAtCN(t *testing.T) { + companyItem := geosite.Item{Type: geosite.RuleTypeDomain, Value: "ssl.gstatic.com"} + devItem := geosite.Item{Type: geosite.RuleTypeDomain, Value: "pkg.go.dev"} + baseItem := geosite.Item{Type: geosite.RuleTypeDomain, Value: "example.cn"} + + data := map[string][]geosite.Item{ + "geolocation-cn": {baseItem}, + "category-companies@cn": {companyItem}, + "category-dev@cn": {devItem}, + } + + mergeTags(data) + + got := data["geolocation-cn"] + if !hasItem(got, baseItem) { + t.Fatalf("base geolocation-cn item missing after merge") + } + if !hasItem(got, devItem) { + t.Fatalf("expected category-dev@cn item to still merge into geolocation-cn") + } + if hasItem(got, companyItem) { + t.Fatalf("did not expect category-companies@cn item to merge into geolocation-cn") + } +}