From 2a3dabd58cae80a831ea98644ad0548800e2c9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 30 Dec 2023 23:43:49 +0800 Subject: [PATCH] Add apple-update rule for apple clients --- option/template.go | 18 +++++++++++++++++- template/render_dns.go | 10 ++-------- template/render_geo_resources.go | 12 +++++++++++- template/render_route.go | 4 ++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/option/template.go b/option/template.go index 247cbeb..f8c6b70 100644 --- a/option/template.go +++ b/option/template.go @@ -1,9 +1,11 @@ package option import ( + M "github.com/sagernet/serenity/common/metadata" "github.com/sagernet/serenity/common/semver" "github.com/sagernet/sing-box/option" - dns "github.com/sagernet/sing-dns" + "github.com/sagernet/sing-dns" + "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/json/badjson" ) @@ -67,6 +69,20 @@ func (t Template) DisableIPv6() bool { return t.DomainStrategy == option.DomainStrategy(dns.DomainStrategyUseIPv4) } +func (t Template) ChinaGeositeList(metadata M.Metadata) []string { + chinaCodes := []string{"cn", "category-companies@cn"} + if metadata.Platform.IsApple() { + chinaCodes = append(chinaCodes, "apple-update") + } + return chinaCodes +} + +func (t Template) ChinaGeositeRuleSetList(metadata M.Metadata) []string { + return common.Map(t.ChinaGeositeList(metadata), func(it string) string { + return "geosite-" + it + }) +} + type ExtraGroup struct { Tag string `json:"tag,omitempty"` Type string `json:"type,omitempty"` diff --git a/template/render_dns.go b/template/render_dns.go index ce93a19..3b9e56f 100644 --- a/template/render_dns.go +++ b/template/render_dns.go @@ -128,10 +128,7 @@ func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error { Type: C.RuleTypeDefault, DefaultOptions: option.DefaultDNSRule{ - Geosite: []string{ - "cn", - "category-companies@cn", - }, + Geosite: t.ChinaGeositeList(metadata), }, }, }, @@ -154,10 +151,7 @@ func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error { Type: C.RuleTypeDefault, DefaultOptions: option.DefaultDNSRule{ - RuleSet: []string{ - "geosite-cn", - "geosite-category-companies@cn", - }, + RuleSet: t.ChinaGeositeRuleSetList(metadata), DomainSuffix: []string{"download.jetbrains.com"}, }, }, diff --git a/template/render_geo_resources.go b/template/render_geo_resources.go index afbfaa2..b40c2aa 100644 --- a/template/render_geo_resources.go +++ b/template/render_geo_resources.go @@ -56,7 +56,6 @@ func (t *Template) renderGeoResources(metadata M.Metadata, options *option.Optio downloadURL = "https://raw.githubusercontent.com/" branchSplit = "/" } - options.Route.RuleSet = []option.RuleSet{ { Type: C.RuleSetTypeRemote, @@ -95,5 +94,16 @@ func (t *Template) renderGeoResources(metadata M.Metadata, options *option.Optio }, }, } + if metadata.Platform.IsApple() { + options.Route.RuleSet = append(options.Route.RuleSet, option.RuleSet{ + Type: C.RuleSetTypeRemote, + Tag: "geosite-apple-update", + Format: C.RuleSetFormatBinary, + RemoteOptions: option.RemoteRuleSet{ + URL: downloadURL + "SagerNet/sing-geosite" + branchSplit + "rule-set/geosite-apple-update.srs", + DownloadDetour: downloadDetour, + }, + }) + } } } diff --git a/template/render_route.go b/template/render_route.go index 7cbf3d7..8193995 100644 --- a/template/render_route.go +++ b/template/render_route.go @@ -136,7 +136,7 @@ func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) err Type: C.RuleTypeDefault, DefaultOptions: option.DefaultRule{ GeoIP: []string{"cn"}, - Geosite: []string{"cn", "category-companies@cn"}, + Geosite: t.ChinaGeositeList(metadata), Domain: []string{"download.jetbrains.com"}, }, }, @@ -160,7 +160,7 @@ func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) err { Type: C.RuleTypeDefault, DefaultOptions: option.DefaultRule{ - RuleSet: []string{"geoip-cn", "geosite-cn", "geosite-category-companies@cn"}, + RuleSet: append([]string{"geoip-cn"}, t.ChinaGeositeRuleSetList(metadata)...), Domain: []string{"download.jetbrains.com"}, }, },