From 7be25fb1c0eedd2783003ebb329ac21b20404cda Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Sat, 23 Nov 2024 19:11:21 +0800 Subject: [PATCH] Convert ruleset type github to more editable type url --- constant/rule_set.go | 2 +- template/render_geo_resources.go | 59 ++++++++++++-------------------- 2 files changed, 23 insertions(+), 38 deletions(-) diff --git a/constant/rule_set.go b/constant/rule_set.go index 96ea296..a1cf1bc 100644 --- a/constant/rule_set.go +++ b/constant/rule_set.go @@ -2,5 +2,5 @@ package constant const ( RuleSetTypeDefault = "default" - RuleSetTypeGitHub = "github" + RuleSetTypeURL = "url" ) diff --git a/template/render_geo_resources.go b/template/render_geo_resources.go index aa1e746..3c6e84f 100644 --- a/template/render_geo_resources.go +++ b/template/render_geo_resources.go @@ -1,14 +1,15 @@ package template import ( + "net/url" + M "github.com/sagernet/serenity/common/metadata" - "github.com/sagernet/serenity/constant" "github.com/sagernet/serenity/option" C "github.com/sagernet/sing-box/constant" boxOption "github.com/sagernet/sing-box/option" ) -func (t *Template) renderGeoResources(metadata M.Metadata, options *boxOption.Options) { +func (t *Template) renderGeoResources(_ M.Metadata, options *boxOption.Options) { if len(t.CustomRuleSet) == 0 { var ( downloadURL string @@ -63,43 +64,27 @@ func (t *Template) renderGeoResources(metadata M.Metadata, options *boxOption.Op func (t *Template) renderRuleSet(ruleSets []option.RuleSet) []boxOption.RuleSet { var result []boxOption.RuleSet for _, ruleSet := range ruleSets { - if ruleSet.Type == constant.RuleSetTypeGitHub { - var ( - downloadURL string - downloadDetour string - branchSplit string - ) - if t.EnableJSDelivr { - downloadURL = "https://testingcf.jsdelivr.net/gh/" - if t.DirectTag != "" { - downloadDetour = t.DirectTag - } else { - downloadDetour = DefaultDirectTag - } - branchSplit = "@" - } else { - downloadURL = "https://raw.githubusercontent.com/" - branchSplit = "/" - } - - for _, code := range ruleSet.GitHubOptions.RuleSet { - result = append(result, boxOption.RuleSet{ - Type: C.RuleSetTypeRemote, - Tag: ruleSet.GitHubOptions.Prefix + code, - Format: C.RuleSetFormatBinary, - RemoteOptions: boxOption.RemoteRuleSet{ - URL: downloadURL + - ruleSet.GitHubOptions.Repository + - branchSplit + - ruleSet.GitHubOptions.Path + - code + ".srs", - DownloadDetour: downloadDetour, - }, - }) - } - } else { + if ruleSet.Type == "remote" { result = append(result, ruleSet.DefaultOptions) } + + for tagname, filename := range ruleSet.URLOptions.RuleSet { + url, _ := url.JoinPath(ruleSet.URLOptions.Base, filename) + var tag string + if ruleSet.URLOptions.Prefix != "" { + tag = ruleSet.URLOptions.Prefix + "-" + tagname + } else { + tag = tagname + } + result = append(result, boxOption.RuleSet{ + Type: C.RuleSetTypeRemote, + Tag: tag, + Format: C.RuleSetFormatBinary, + RemoteOptions: boxOption.RemoteRuleSet{ + URL: url, + }, + }) + } } return result }