2023-09-12 06:40:24 -04:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2023-09-17 03:52:37 -04:00
|
|
|
|
"crypto/sha256"
|
2023-09-13 01:47:22 -04:00
|
|
|
|
"encoding/hex"
|
2023-09-12 06:40:24 -04:00
|
|
|
|
"errors"
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-09-14 12:13:45 -04:00
|
|
|
|
"net/url"
|
2023-09-13 01:47:22 -04:00
|
|
|
|
"regexp"
|
2023-09-20 21:08:02 -04:00
|
|
|
|
"sort"
|
2023-09-12 06:40:24 -04:00
|
|
|
|
"strings"
|
2023-09-12 12:46:17 -04:00
|
|
|
|
"sub2clash/model"
|
|
|
|
|
"sub2clash/parser"
|
|
|
|
|
"sub2clash/utils"
|
2023-09-13 01:47:22 -04:00
|
|
|
|
"sub2clash/validator"
|
2023-09-12 06:40:24 -04:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-17 03:52:37 -04:00
|
|
|
|
func BuildSub(clashType model.ClashType, query validator.SubValidator, template string) (
|
2023-09-12 12:46:17 -04:00
|
|
|
|
*model.Subscription, error,
|
|
|
|
|
) {
|
2023-09-12 06:40:24 -04:00
|
|
|
|
// 定义变量
|
2023-09-17 01:21:01 -04:00
|
|
|
|
var temp = &model.Subscription{}
|
|
|
|
|
var sub = &model.Subscription{}
|
2023-09-13 01:47:22 -04:00
|
|
|
|
var err error
|
|
|
|
|
var templateBytes []byte
|
2023-09-12 06:40:24 -04:00
|
|
|
|
// 加载模板
|
2023-09-14 20:26:48 -04:00
|
|
|
|
if query.Template != "" {
|
|
|
|
|
template = query.Template
|
|
|
|
|
}
|
2023-09-14 12:13:45 -04:00
|
|
|
|
_, err = url.ParseRequestURI(template)
|
|
|
|
|
if err != nil {
|
2023-09-13 01:47:22 -04:00
|
|
|
|
templateBytes, err = utils.LoadTemplate(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("加载模板失败: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
templateBytes, err = utils.LoadSubscription(template, query.Refresh)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("加载模板失败: " + err.Error())
|
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
// 解析模板
|
2023-09-13 01:47:22 -04:00
|
|
|
|
err = yaml.Unmarshal(templateBytes, &temp)
|
2023-09-12 06:40:24 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("解析模板失败: " + err.Error())
|
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
var proxyList []model.Proxy
|
2023-09-12 06:40:24 -04:00
|
|
|
|
// 加载订阅
|
2023-09-13 01:47:22 -04:00
|
|
|
|
for i := range query.Subs {
|
|
|
|
|
data, err := utils.LoadSubscription(query.Subs[i], query.Refresh)
|
2023-09-12 06:40:24 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("加载订阅失败: " + err.Error())
|
|
|
|
|
}
|
2023-09-12 12:46:17 -04:00
|
|
|
|
// 解析订阅
|
2023-09-20 21:08:02 -04:00
|
|
|
|
|
2023-09-12 12:46:17 -04:00
|
|
|
|
err = yaml.Unmarshal(data, &sub)
|
|
|
|
|
if err != nil {
|
2023-09-13 01:47:22 -04:00
|
|
|
|
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|http|https)://")
|
|
|
|
|
if reg.Match(data) {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
p := utils.ParseProxy(strings.Split(string(data), "\n")...)
|
|
|
|
|
proxyList = append(proxyList, p...)
|
2023-09-13 01:47:22 -04:00
|
|
|
|
} else {
|
|
|
|
|
// 如果无法直接解析,尝试Base64解码
|
|
|
|
|
base64, err := parser.DecodeBase64(string(data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("加载订阅失败: " + err.Error())
|
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
p := utils.ParseProxy(strings.Split(base64, "\n")...)
|
|
|
|
|
proxyList = append(proxyList, p...)
|
2023-09-12 12:46:17 -04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
proxyList = append(proxyList, sub.Proxies...)
|
2023-09-12 12:46:17 -04:00
|
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
// 将新增节点都添加到临时变量 t 中,防止策略组排序错乱
|
|
|
|
|
var t = &model.Subscription{}
|
|
|
|
|
utils.AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
|
2023-09-13 01:47:22 -04:00
|
|
|
|
// 处理自定义代理
|
2023-09-14 12:13:45 -04:00
|
|
|
|
utils.AddProxy(
|
2023-09-20 21:08:02 -04:00
|
|
|
|
t, query.AutoTest, query.Lazy, clashType,
|
2023-09-14 12:13:45 -04:00
|
|
|
|
utils.ParseProxy(query.Proxies...)...,
|
|
|
|
|
)
|
2023-09-20 21:08:02 -04:00
|
|
|
|
// 排序策略组
|
|
|
|
|
switch query.Sort {
|
|
|
|
|
case "sizeasc":
|
|
|
|
|
sort.Sort(model.ProxyGroupsSortBySize(t.ProxyGroups))
|
|
|
|
|
case "sizedesc":
|
|
|
|
|
sort.Sort(sort.Reverse(model.ProxyGroupsSortBySize(t.ProxyGroups)))
|
|
|
|
|
case "nameasc":
|
|
|
|
|
sort.Sort(model.ProxyGroupsSortByName(t.ProxyGroups))
|
|
|
|
|
case "namedesc":
|
|
|
|
|
sort.Sort(sort.Reverse(model.ProxyGroupsSortByName(t.ProxyGroups)))
|
|
|
|
|
default:
|
|
|
|
|
sort.Sort(model.ProxyGroupsSortByName(t.ProxyGroups))
|
|
|
|
|
}
|
|
|
|
|
// 合并新节点和模板
|
|
|
|
|
MergeSubAndTemplate(temp, t)
|
2023-09-13 01:47:22 -04:00
|
|
|
|
// 处理自定义规则
|
|
|
|
|
for _, v := range query.Rules {
|
|
|
|
|
if v.Prepend {
|
|
|
|
|
utils.PrependRules(temp, v.Rule)
|
|
|
|
|
} else {
|
|
|
|
|
utils.AppendRules(temp, v.Rule)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 处理自定义 ruleProvider
|
|
|
|
|
for _, v := range query.RuleProviders {
|
2023-09-17 03:52:37 -04:00
|
|
|
|
hash := sha256.Sum224([]byte(v.Url))
|
2023-09-13 01:47:22 -04:00
|
|
|
|
name := hex.EncodeToString(hash[:])
|
|
|
|
|
provider := model.RuleProvider{
|
|
|
|
|
Type: "http",
|
|
|
|
|
Behavior: v.Behavior,
|
|
|
|
|
Url: v.Url,
|
|
|
|
|
Path: "./" + name + ".yaml",
|
|
|
|
|
Interval: 3600,
|
|
|
|
|
}
|
|
|
|
|
if v.Prepend {
|
|
|
|
|
utils.PrependRuleProvider(
|
2023-09-14 12:13:45 -04:00
|
|
|
|
temp, v.Name, v.Group, provider,
|
2023-09-13 01:47:22 -04:00
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
utils.AppenddRuleProvider(
|
2023-09-14 12:13:45 -04:00
|
|
|
|
temp, v.Name, v.Group, provider,
|
2023-09-13 01:47:22 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
return temp, nil
|
|
|
|
|
}
|
2023-09-14 12:13:45 -04:00
|
|
|
|
|
|
|
|
|
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription) {
|
|
|
|
|
// 只合并节点、策略组
|
|
|
|
|
// 统计所有国家策略组名称
|
2023-09-14 12:31:57 -04:00
|
|
|
|
var countryGroupNames []string
|
2023-09-14 12:13:45 -04:00
|
|
|
|
for _, proxyGroup := range sub.ProxyGroups {
|
|
|
|
|
if proxyGroup.IsCountryGrop {
|
2023-09-14 12:31:57 -04:00
|
|
|
|
countryGroupNames = append(
|
|
|
|
|
countryGroupNames, proxyGroup.Name,
|
2023-09-14 12:13:45 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 将订阅中的节点添加到模板中
|
|
|
|
|
temp.Proxies = append(temp.Proxies, sub.Proxies...)
|
|
|
|
|
// 将订阅中的策略组添加到模板中
|
2023-09-14 20:51:22 -04:00
|
|
|
|
skipGroups := []string{"全球直连", "广告拦截", "手动切换"}
|
2023-09-14 12:13:45 -04:00
|
|
|
|
for i := range temp.ProxyGroups {
|
2023-09-14 20:51:22 -04:00
|
|
|
|
skip := false
|
|
|
|
|
for _, v := range skipGroups {
|
|
|
|
|
if strings.Contains(temp.ProxyGroups[i].Name, v) {
|
|
|
|
|
if v == "手动切换" {
|
|
|
|
|
proxies := make([]string, 0, len(sub.Proxies))
|
|
|
|
|
for _, p := range sub.Proxies {
|
|
|
|
|
proxies = append(proxies, p.Name)
|
|
|
|
|
}
|
|
|
|
|
temp.ProxyGroups[i].Proxies = proxies
|
|
|
|
|
}
|
|
|
|
|
skip = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !skip {
|
|
|
|
|
temp.ProxyGroups[i].Proxies = append(temp.ProxyGroups[i].Proxies, countryGroupNames...)
|
|
|
|
|
}
|
2023-09-14 12:13:45 -04:00
|
|
|
|
}
|
|
|
|
|
temp.ProxyGroups = append(temp.ProxyGroups, sub.ProxyGroups...)
|
|
|
|
|
}
|