1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 11:24:41 -05:00
sub2clash/api/controller/default.go

101 lines
2.5 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package controller
import (
2023-09-13 01:47:22 -04:00
"crypto/md5"
"encoding/hex"
2023-09-12 06:40:24 -04:00
"errors"
"gopkg.in/yaml.v3"
2023-09-13 01:47:22 -04:00
"regexp"
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-13 01:47:22 -04:00
func BuildSub(query validator.SubQuery, template string) (
2023-09-12 12:46:17 -04:00
*model.Subscription, error,
) {
2023-09-12 06:40:24 -04:00
// 定义变量
2023-09-13 01:47:22 -04:00
var externalTemplate = query.Template != ""
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-13 01:47:22 -04:00
if !externalTemplate {
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-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
// 解析订阅
var proxyList []model.Proxy
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) {
proxyList = utils.ParseProxy(strings.Split(string(data), "\n")...)
} else {
// 如果无法直接解析尝试Base64解码
base64, err := parser.DecodeBase64(string(data))
if err != nil {
return nil, errors.New("加载订阅失败: " + err.Error())
}
proxyList = utils.ParseProxy(strings.Split(base64, "\n")...)
2023-09-12 12:46:17 -04:00
}
} else {
proxyList = sub.Proxies
}
2023-09-13 01:47:22 -04:00
utils.AddProxy(temp, query.AutoTest, query.Lazy, proxyList...)
}
// 处理自定义代理
utils.AddProxy(temp, query.AutoTest, query.Lazy, utils.ParseProxy(query.Proxies...)...)
// 处理自定义规则
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 {
hash := md5.Sum([]byte(v.Url))
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, name, v.Group, provider,
)
} else {
utils.AppenddRuleProvider(
temp, name, v.Group, provider,
)
}
2023-09-12 06:40:24 -04:00
}
return temp, nil
}