1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 17:00:53 -05:00
sub2clash/api/controller/default.go

289 lines
8.4 KiB
Go
Raw Normal View History

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"
"net/url"
2023-09-13 01:47:22 -04:00
"regexp"
"sort"
"strconv"
2023-09-12 06:40:24 -04:00
"strings"
2023-09-21 05:37:37 -04:00
"sub2clash/logger"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
"sub2clash/parser"
"sub2clash/utils"
2023-09-13 01:47:22 -04:00
"sub2clash/validator"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
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
// 定义变量
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
}
if strings.HasPrefix(template, "http") {
templateBytes, err = utils.LoadSubscription(template, query.Refresh)
2023-09-13 01:47:22 -04:00
if err != nil {
2023-09-21 05:37:37 -04:00
logger.Logger.Debug(
"load template failed", zap.String("template", template), zap.Error(err),
)
2023-09-13 01:47:22 -04:00
return nil, errors.New("加载模板失败: " + err.Error())
}
} else {
unescape, err := url.QueryUnescape(template)
if err != nil {
return nil, errors.New("加载模板失败: " + err.Error())
}
templateBytes, err = utils.LoadTemplate(unescape)
2023-09-13 01:47:22 -04:00
if err != nil {
2023-09-21 05:37:37 -04:00
logger.Logger.Debug(
"load template failed", zap.String("template", template), zap.Error(err),
)
2023-09-13 01:47:22 -04:00
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 {
2023-09-21 05:37:37 -04:00
logger.Logger.Debug("parse template failed", zap.Error(err))
2023-09-12 06:40:24 -04:00
return nil, errors.New("解析模板失败: " + err.Error())
}
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)
subName := ""
if strings.Contains(query.Subs[i], "#") {
subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
}
2023-09-12 06:40:24 -04:00
if err != nil {
2023-09-21 05:37:37 -04:00
logger.Logger.Debug(
"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
)
2023-09-12 06:40:24 -04:00
return nil, errors.New("加载订阅失败: " + err.Error())
}
2023-09-12 12:46:17 -04:00
// 解析订阅
err = yaml.Unmarshal(data, &sub)
newProxies := make([]model.Proxy, 0)
2023-09-12 12:46:17 -04:00
if err != nil {
2023-10-31 03:14:29 -04:00
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless|hysteria)://")
2023-09-13 01:47:22 -04:00
if reg.Match(data) {
p := utils.ParseProxy(strings.Split(string(data), "\n")...)
newProxies = p
2023-09-13 01:47:22 -04:00
} else {
// 如果无法直接解析尝试Base64解码
base64, err := parser.DecodeBase64(string(data))
if err != nil {
2023-09-21 05:37:37 -04:00
logger.Logger.Debug(
"parse subscription failed", zap.String("url", query.Subs[i]),
zap.String("data", string(data)),
zap.Error(err),
)
2023-09-13 01:47:22 -04:00
return nil, errors.New("加载订阅失败: " + err.Error())
}
p := utils.ParseProxy(strings.Split(base64, "\n")...)
newProxies = p
2023-09-12 12:46:17 -04:00
}
} else {
newProxies = sub.Proxies
}
if subName != "" {
for i := range newProxies {
newProxies[i].SubName = subName
}
2023-09-12 12:46:17 -04:00
}
proxyList = append(proxyList, newProxies...)
2023-09-13 01:47:22 -04:00
}
2023-09-22 12:58:57 -04:00
// 添加自定义节点
if len(query.Proxies) != 0 {
proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...)
}
// 给节点添加订阅名称
for i := range proxyList {
if proxyList[i].SubName != "" {
proxyList[i].Name = strings.TrimSpace(proxyList[i].SubName) + " " + strings.TrimSpace(proxyList[i].Name)
}
}
2023-09-22 13:31:04 -04:00
// 去掉配置相同的节点
proxies := make(map[string]*model.Proxy)
2023-09-22 13:11:58 -04:00
newProxies := make([]model.Proxy, 0, len(proxyList))
for i := range proxyList {
key := proxyList[i].Server + ":" + strconv.Itoa(proxyList[i].Port) + ":" + proxyList[i].Type
2023-09-22 13:11:58 -04:00
if _, exist := proxies[key]; !exist {
proxies[key] = &proxyList[i]
newProxies = append(newProxies, proxyList[i])
}
}
2023-09-22 13:11:58 -04:00
proxyList = newProxies
2023-09-22 12:58:57 -04:00
// 删除节点
if strings.TrimSpace(query.Remove) != "" {
newProxyList := make([]model.Proxy, 0, len(proxyList))
for i := range proxyList {
2023-09-22 12:58:57 -04:00
removeReg, err := regexp.Compile(query.Remove)
if err != nil {
logger.Logger.Debug("remove regexp compile failed", zap.Error(err))
return nil, errors.New("remove 参数非法: " + err.Error())
}
// 删除匹配到的节点
if removeReg.MatchString(proxyList[i].Name) {
continue // 如果匹配到要删除的元素,跳过该元素,不添加到新切片中
}
newProxyList = append(newProxyList, proxyList[i]) // 将要保留的元素添加到新切片中
}
proxyList = newProxyList
}
2023-09-22 12:58:57 -04:00
// 重命名
if len(query.ReplaceKeys) != 0 {
// 创建重命名正则表达式
replaceRegs := make([]*regexp.Regexp, 0, len(query.ReplaceKeys))
for _, v := range query.ReplaceKeys {
replaceReg, err := regexp.Compile(v)
if err != nil {
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
return nil, errors.New("replace 参数非法: " + err.Error())
}
replaceRegs = append(replaceRegs, replaceReg)
}
for i := range proxyList {
// 重命名匹配到的节点
for j, v := range replaceRegs {
if err != nil {
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
return nil, errors.New("replaceName 参数非法: " + err.Error())
}
if v.MatchString(proxyList[i].Name) {
proxyList[i].Name = v.ReplaceAllString(
proxyList[i].Name, query.ReplaceTo[j],
)
}
}
}
}
2023-09-22 13:31:04 -04:00
// 重名检测
names := make(map[string]int)
for i := range proxyList {
if _, exist := names[proxyList[i].Name]; exist {
names[proxyList[i].Name] = names[proxyList[i].Name] + 1
2023-09-22 13:31:04 -04:00
proxyList[i].Name = proxyList[i].Name + " " + strconv.Itoa(names[proxyList[i].Name])
} else {
names[proxyList[i].Name] = 0
2023-09-22 13:31:04 -04:00
}
}
2023-09-22 12:58:57 -04:00
// trim
for i := range proxyList {
proxyList[i].Name = strings.TrimSpace(proxyList[i].Name)
}
// 将新增节点都添加到临时变量 t 中,防止策略组排序错乱
var t = &model.Subscription{}
utils.AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
// 排序策略组
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(
temp, v.Name, v.Group, provider,
2023-09-13 01:47:22 -04:00
)
} else {
utils.AppenddRuleProvider(
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
}
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription) {
// 只合并节点、策略组
// 统计所有国家策略组名称
2023-09-14 12:31:57 -04:00
var countryGroupNames []string
for _, proxyGroup := range sub.ProxyGroups {
if proxyGroup.IsCountryGrop {
2023-09-14 12:31:57 -04:00
countryGroupNames = append(
countryGroupNames, proxyGroup.Name,
)
}
}
var proxyNames []string
for _, proxy := range sub.Proxies {
proxyNames = append(proxyNames, proxy.Name)
}
// 将订阅中的节点添加到模板中
temp.Proxies = append(temp.Proxies, sub.Proxies...)
// 将订阅中的策略组添加到模板中
for i := range temp.ProxyGroups {
if temp.ProxyGroups[i].IsCountryGrop {
continue
2023-09-14 20:51:22 -04:00
}
newProxies := make([]string, 0)
countryGroupMap := make(map[string]model.ProxyGroup)
for _, v := range sub.ProxyGroups {
if v.IsCountryGrop {
countryGroupMap[v.Name] = v
}
}
for j := range temp.ProxyGroups[i].Proxies {
reg := regexp.MustCompile("<(.*?)>")
if reg.Match([]byte(temp.ProxyGroups[i].Proxies[j])) {
key := reg.FindStringSubmatch(temp.ProxyGroups[i].Proxies[j])[1]
switch key {
case "all":
newProxies = append(newProxies, proxyNames...)
case "countries":
newProxies = append(newProxies, countryGroupNames...)
default:
if len(key) == 2 {
newProxies = append(
newProxies, countryGroupMap[utils.GetContryName(key)].Proxies...,
)
}
}
} else {
newProxies = append(newProxies, temp.ProxyGroups[i].Proxies[j])
}
2023-09-14 20:51:22 -04:00
}
temp.ProxyGroups[i].Proxies = newProxies
}
temp.ProxyGroups = append(temp.ProxyGroups, sub.ProxyGroups...)
}