From 72635ce0fc08933f4f32b25c82c16b8418f795ea Mon Sep 17 00:00:00 2001 From: nitezs Date: Fri, 15 Sep 2023 00:13:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=9B=BD=E5=AE=B6?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E7=BB=84=E6=8E=92=E5=BA=8F=E7=AD=96=E7=95=A5?= =?UTF-8?q?=20feat:=20=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=A8=A1=E6=9D=BF=20mod:=20Rule-Provider=20=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 22 +++++++------- api/controller/default.go | 38 +++++++++++++++++++---- model/proxy_group.go | 58 ++++++++++++++++++++++++++++++++++++ model/sub.go | 11 ------- templates/template_meta.yaml | 1 - utils/proxy.go | 46 +++++++++++++++------------- validator/sub.go | 11 ++++++- 7 files changed, 138 insertions(+), 49 deletions(-) create mode 100644 model/proxy_group.go diff --git a/README.md b/README.md index 873dbdb..898acb3 100644 --- a/README.md +++ b/README.md @@ -41,16 +41,17 @@ 获取 Clash/Clash.Meta 配置链接 -| Query 参数 | 类型 | 是否必须 | 说明 | -|--------------|--------|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| sub | string | sub/proxy 至少有一项存在 | 订阅链接(可以输入多个,用 `,` 分隔) | -| proxy | string | sub/proxy 至少有一项存在 | 节点分享链接(可以输入多个,用 `,` 分隔) | -| refresh | bool | 否(默认 `false`) | 强制刷新配置(默认缓存 5 分钟) | -| template | string | 否 | 外部模板 | -| ruleProvider | string | 否 | 格式 `[Behavior,Url,Group,Prepend],[Behavior,Url,Group,Prepend],...`,其中 `Group` 是该规则集所走的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前) | -| rule | string | 否 | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前) | -| autoTest | bool | 否(默认 `false`) | 指定国家策略组是否自动测速 | -| lazy | bool | 否(默认 `false`) | 自动测速是否启用 lazy | +| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 | +|------------------|--------|-------------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| sub | string | sub/proxy 至少有一项存在 | - | 订阅链接(可以输入多个,用 `,` 分隔) | +| proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) | +| refresh | bool | 否 | `false` | 强制刷新配置(默认缓存 5 分钟) | +| template | string | 否 | - | 外部模板链接或内部模板名称 | +| ruleProvider | string | 否 | - | 格式 `[Behavior,Url,Group,Prepend,Name],[Behavior,Url,Group,Prepend,Name],...`,其中 `Group` 是该规则集所走的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前) | +| rule | string | 否 | - | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前) | +| autoTest | bool | 否 | `false` | 指定国家策略组是否自动测速 | +| lazy | bool | 否 | `false` | 自动测速是否启用 lazy | +| countryGroupSort | string | 否 | `nameasc` | 国家策略组排序策略,可选值 `nameasc`、`namedesc`、`sizeasc`、`sizedesc` | ## 默认模板 @@ -63,3 +64,4 @@ ## TODO +- [ ] 可视化面板 \ No newline at end of file diff --git a/api/controller/default.go b/api/controller/default.go index 55760a6..07e2785 100644 --- a/api/controller/default.go +++ b/api/controller/default.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "errors" "gopkg.in/yaml.v3" + "net/url" "regexp" "strings" "sub2clash/model" @@ -17,13 +18,13 @@ func BuildSub(query validator.SubQuery, template string) ( *model.Subscription, error, ) { // 定义变量 - var externalTemplate = query.Template != "" var temp *model.Subscription var sub *model.Subscription var err error var templateBytes []byte // 加载模板 - if !externalTemplate { + _, err = url.ParseRequestURI(template) + if err != nil { templateBytes, err = utils.LoadTemplate(template) if err != nil { return nil, errors.New("加载模板失败: " + err.Error()) @@ -63,10 +64,14 @@ func BuildSub(query validator.SubQuery, template string) ( } else { proxyList = sub.Proxies } - utils.AddProxy(temp, query.AutoTest, query.Lazy, proxyList...) + utils.AddProxy(sub, query.AutoTest, query.Lazy, query.Sort, proxyList...) } // 处理自定义代理 - utils.AddProxy(temp, query.AutoTest, query.Lazy, utils.ParseProxy(query.Proxies...)...) + utils.AddProxy( + sub, query.AutoTest, query.Lazy, query.Sort, + utils.ParseProxy(query.Proxies...)..., + ) + MergeSubAndTemplate(temp, sub) // 处理自定义规则 for _, v := range query.Rules { if v.Prepend { @@ -88,13 +93,34 @@ func BuildSub(query validator.SubQuery, template string) ( } if v.Prepend { utils.PrependRuleProvider( - temp, name, v.Group, provider, + temp, v.Name, v.Group, provider, ) } else { utils.AppenddRuleProvider( - temp, name, v.Group, provider, + temp, v.Name, v.Group, provider, ) } } return temp, nil } + +func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription) { + // 只合并节点、策略组 + // 统计所有国家策略组名称 + var newCountryGroupNames []string + for _, proxyGroup := range sub.ProxyGroups { + if proxyGroup.IsCountryGrop { + newCountryGroupNames = append( + newCountryGroupNames, proxyGroup.Name, + ) + } + } + // 将订阅中的节点添加到模板中 + temp.Proxies = append(temp.Proxies, sub.Proxies...) + // 将订阅中的策略组添加到模板中 + for i := range temp.ProxyGroups { + temp.ProxyGroups[i].Proxies = append(temp.ProxyGroups[i].Proxies, newCountryGroupNames...) + } + temp.ProxyGroups = append(temp.ProxyGroups, sub.ProxyGroups...) + temp.Rules = append(temp.Rules, sub.Rules...) +} diff --git a/model/proxy_group.go b/model/proxy_group.go new file mode 100644 index 0000000..26179c6 --- /dev/null +++ b/model/proxy_group.go @@ -0,0 +1,58 @@ +package model + +import ( + "golang.org/x/text/collate" + "golang.org/x/text/language" +) + +type ProxyGroup struct { + Name string `yaml:"name,omitempty"` + Type string `yaml:"type,omitempty"` + Proxies []string `yaml:"proxies,omitempty"` + IsCountryGrop bool `yaml:"-"` + Url string `yaml:"url,omitempty"` + Interval int `yaml:"interval,omitempty"` + Tolerance int `yaml:"tolerance,omitempty"` + Lazy bool `yaml:"lazy"` + Size int `yaml:"-"` +} + +type ProxyGroupsSortByName []ProxyGroup +type ProxyGroupsSortBySize []ProxyGroup + +func (p ProxyGroupsSortByName) Len() int { + return len(p) +} +func (p ProxyGroupsSortBySize) Len() int { + return len(p) +} + +func (p ProxyGroupsSortByName) Less(i, j int) bool { + // 定义一组备选语言:首选英语,其次中文 + tags := []language.Tag{ + language.English, + language.Chinese, + } + matcher := language.NewMatcher(tags) + + // 假设我们的请求语言是 "zh"(中文),则使用匹配器找到最佳匹配的语言 + bestMatch, _, _ := matcher.Match(language.Make("zh")) + // 使用最佳匹配的语言进行排序 + c := collate.New(bestMatch) + + return c.CompareString(p[i].Name, p[j].Name) < 0 +} + +func (p ProxyGroupsSortBySize) Less(i, j int) bool { + if p[i].Size == p[j].Size { + return p[i].Name < p[j].Name + } + return p[i].Size < p[j].Size +} + +func (p ProxyGroupsSortByName) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} +func (p ProxyGroupsSortBySize) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} diff --git a/model/sub.go b/model/sub.go index 4d8d3d4..9043450 100644 --- a/model/sub.go +++ b/model/sub.go @@ -13,17 +13,6 @@ type Subscription struct { RuleProviders map[string]RuleProvider `yaml:"rule-providers,omitempty,omitempty"` } -type ProxyGroup struct { - Name string `yaml:"name,omitempty"` - Type string `yaml:"type,omitempty"` - Proxies []string `yaml:"proxies,omitempty"` - IsCountryGrop bool `yaml:"-"` - Url string `yaml:"url,omitempty"` - Interval int `yaml:"interval,omitempty"` - Tolerance int `yaml:"tolerance,omitempty"` - Lazy bool `yaml:"lazy"` -} - type RuleProvider struct { Type string `yaml:"type,omitempty"` Behavior string `yaml:"behavior,omitempty"` diff --git a/templates/template_meta.yaml b/templates/template_meta.yaml index 1cf3788..7287b80 100644 --- a/templates/template_meta.yaml +++ b/templates/template_meta.yaml @@ -57,6 +57,5 @@ rules: - GEOIP,CN,全球直连 - GEOSITE,category-games,游戏平台 - GEOSITE,geolocation-!cn,节点选择 - - GEOIP,ad,广告拦截 - GEOSITE,category-ads-all,广告拦截 - MATCH,漏网之鱼 \ No newline at end of file diff --git a/utils/proxy.go b/utils/proxy.go index 9283e6e..130f771 100644 --- a/utils/proxy.go +++ b/utils/proxy.go @@ -1,6 +1,7 @@ package utils import ( + "sort" "strings" "sub2clash/model" "sub2clash/parser" @@ -27,35 +28,30 @@ func GetContryName(proxy model.Proxy) string { return "其他地区" } -var skipGroups = map[string]bool{ - "手动切换": true, - "全球直连": true, - "广告拦截": true, - "应用净化": true, -} - -func AddProxy(sub *model.Subscription, autotest bool, lazy bool, proxies ...model.Proxy) { +func AddProxy( + sub *model.Subscription, autotest bool, lazy bool, sortStrategy string, + proxies ...model.Proxy, +) { newCountryGroupNames := make([]string, 0) - + // 添加节点 for _, proxy := range proxies { sub.Proxies = append(sub.Proxies, proxy) - haveProxyGroup := false countryName := GetContryName(proxy) - for i := range sub.ProxyGroups { group := &sub.ProxyGroups[i] if group.Name == countryName { group.Proxies = append(group.Proxies, proxy.Name) + group.Size++ haveProxyGroup = true } if group.Name == "手动切换" { group.Proxies = append(group.Proxies, proxy.Name) + group.Size++ } } - if !haveProxyGroup { var newGroup model.ProxyGroup if !autotest { @@ -64,6 +60,7 @@ func AddProxy(sub *model.Subscription, autotest bool, lazy bool, proxies ...mode Type: "select", Proxies: []string{proxy.Name}, IsCountryGrop: true, + Size: 1, } } else { newGroup = model.ProxyGroup{ @@ -75,24 +72,33 @@ func AddProxy(sub *model.Subscription, autotest bool, lazy bool, proxies ...mode Interval: 300, Tolerance: 50, Lazy: lazy, + Size: 1, } } sub.ProxyGroups = append(sub.ProxyGroups, newGroup) newCountryGroupNames = append(newCountryGroupNames, countryName) } } - + // 统计国家策略组数量 + countryGroupCount := 0 for i := range sub.ProxyGroups { if sub.ProxyGroups[i].IsCountryGrop { - continue - } - if !skipGroups[sub.ProxyGroups[i].Name] { - combined := make([]string, len(newCountryGroupNames)+len(sub.ProxyGroups[i].Proxies)) - copy(combined, newCountryGroupNames) - copy(combined[len(newCountryGroupNames):], sub.ProxyGroups[i].Proxies) - sub.ProxyGroups[i].Proxies = combined + countryGroupCount++ } } + // 对国家策略组进行排序 + switch sortStrategy { + case "sizeasc": + sort.Sort(model.ProxyGroupsSortBySize(sub.ProxyGroups[:countryGroupCount])) + case "sizedesc": + sort.Sort(sort.Reverse(model.ProxyGroupsSortBySize(sub.ProxyGroups[:countryGroupCount]))) + case "nameasc": + sort.Sort(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount])) + case "namedesc": + sort.Sort(sort.Reverse(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount]))) + default: + sort.Sort(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount])) + } } func ParseProxy(proxies ...string) []model.Proxy { diff --git a/validator/sub.go b/validator/sub.go index c903dd5..24405fd 100644 --- a/validator/sub.go +++ b/validator/sub.go @@ -1,6 +1,8 @@ package validator import ( + "crypto/md5" + "encoding/hex" "errors" "github.com/gin-gonic/gin" "net/url" @@ -21,6 +23,7 @@ type SubQuery struct { Rules []RuleStruct `form:"-" binding:""` AutoTest bool `form:"autoTest,default=false" binding:""` Lazy bool `form:"lazy,default=false" binding:""` + Sort string `form:"sort" binding:""` } type RuleProviderStruct struct { @@ -28,6 +31,7 @@ type RuleProviderStruct struct { Url string Group string Prepend bool + Name string } type RuleStruct struct { @@ -87,7 +91,7 @@ func ParseQuery(c *gin.Context) (SubQuery, error) { for i := range ruleProviders { length := len(ruleProviders) parts := strings.Split(ruleProviders[length-i-1][1], ",") - if len(parts) != 4 { + if len(parts) < 4 { return SubQuery{}, errors.New("参数错误: ruleProvider 格式错误") } u := parts[1] @@ -100,12 +104,17 @@ func ParseQuery(c *gin.Context) (SubQuery, error) { if err != nil { return SubQuery{}, errors.New("参数错误: " + err.Error()) } + if len(parts) == 4 { + hash := md5.Sum([]byte(u)) + parts = append(parts, hex.EncodeToString(hash[:])) + } query.RuleProviders = append( query.RuleProviders, RuleProviderStruct{ Behavior: parts[0], Url: u, Group: parts[2], Prepend: parts[3] == "true", + Name: parts[4], }, ) }