Convert ruleset type github to more editable type url

This commit is contained in:
Puqns67 2024-11-23 19:11:21 +08:00
parent 9a11d69d63
commit 7be25fb1c0
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
2 changed files with 23 additions and 38 deletions

View File

@ -2,5 +2,5 @@ package constant
const (
RuleSetTypeDefault = "default"
RuleSetTypeGitHub = "github"
RuleSetTypeURL = "url"
)

View File

@ -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 = "/"
if ruleSet.Type == "remote" {
result = append(result, ruleSet.DefaultOptions)
}
for _, code := range ruleSet.GitHubOptions.RuleSet {
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: ruleSet.GitHubOptions.Prefix + code,
Tag: tag,
Format: C.RuleSetFormatBinary,
RemoteOptions: boxOption.RemoteRuleSet{
URL: downloadURL +
ruleSet.GitHubOptions.Repository +
branchSplit +
ruleSet.GitHubOptions.Path +
code + ".srs",
DownloadDetour: downloadDetour,
URL: url,
},
})
}
} else {
result = append(result, ruleSet.DefaultOptions)
}
}
return result
}