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

62 lines
1.5 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package controller
import (
"errors"
"gopkg.in/yaml.v3"
2023-09-12 12:46:17 -04:00
"net/url"
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-12 06:40:24 -04:00
)
2023-09-12 12:46:17 -04:00
func MixinSubsAndTemplate(subs []string, refresh bool, template string) (
*model.Subscription, error,
) {
2023-09-12 06:40:24 -04:00
// 定义变量
var temp *model.Subscription
var sub *model.Subscription
// 加载模板
template, err := utils.LoadTemplate(template)
if err != nil {
return nil, errors.New("加载模板失败: " + err.Error())
}
// 解析模板
err = yaml.Unmarshal([]byte(template), &temp)
if err != nil {
return nil, errors.New("解析模板失败: " + err.Error())
}
2023-09-12 12:46:17 -04:00
var proxies []model.Proxy
2023-09-12 06:40:24 -04:00
// 加载订阅
2023-09-12 12:46:17 -04:00
for i := range subs {
subs[i], _ = url.QueryUnescape(subs[i])
if _, err := url.ParseRequestURI(subs[i]); err != nil {
return nil, errors.New("订阅地址错误: " + err.Error())
}
data, err := utils.LoadSubscription(
subs[i],
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 {
// 如果无法直接解析尝试Base64解码
base64, err := parser.DecodeBase64(string(data))
if err != nil {
return nil, errors.New("加载订阅失败: " + err.Error())
}
proxyList = utils.ParseProxy(strings.Split(base64, "\n")...)
} else {
proxyList = sub.Proxies
}
proxies = append(proxies, proxyList...)
2023-09-12 06:40:24 -04:00
}
// 添加节点
2023-09-12 12:46:17 -04:00
utils.AddProxy(temp, proxies...)
2023-09-12 06:40:24 -04:00
return temp, nil
}