mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-07-04 20:02:34 +08:00
⚡️ Improve
This commit is contained in:
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func SubmodHandler(c *gin.Context) {
|
||||
// 从请求中获取参数
|
||||
|
||||
query, err := validator.ParseQuery(c)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, err.Error())
|
||||
@ -22,7 +22,7 @@ func SubmodHandler(c *gin.Context) {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
// 输出
|
||||
|
||||
if query.NodeListMode {
|
||||
nodelist := model.NodeList{}
|
||||
nodelist.Proxies = sub.Proxies
|
||||
|
@ -22,12 +22,12 @@ import (
|
||||
func BuildSub(clashType model.ClashType, query validator.SubValidator, template string) (
|
||||
*model.Subscription, error,
|
||||
) {
|
||||
// 定义变量
|
||||
|
||||
var temp = &model.Subscription{}
|
||||
var sub = &model.Subscription{}
|
||||
var err error
|
||||
var templateBytes []byte
|
||||
// 加载模板
|
||||
|
||||
if query.Template != "" {
|
||||
template = query.Template
|
||||
}
|
||||
@ -52,14 +52,14 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
return nil, errors.New("加载模板失败: " + err.Error())
|
||||
}
|
||||
}
|
||||
// 解析模板
|
||||
|
||||
err = yaml.Unmarshal(templateBytes, &temp)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("parse template failed", zap.Error(err))
|
||||
return nil, errors.New("解析模板失败: " + err.Error())
|
||||
}
|
||||
var proxyList []model.Proxy
|
||||
// 加载订阅
|
||||
|
||||
for i := range query.Subs {
|
||||
data, err := common.LoadSubscription(query.Subs[i], query.Refresh, query.UserAgent)
|
||||
subName := ""
|
||||
@ -72,7 +72,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
)
|
||||
return nil, errors.New("加载订阅失败: " + err.Error())
|
||||
}
|
||||
// 解析订阅
|
||||
|
||||
err = yaml.Unmarshal(data, &sub)
|
||||
var newProxies []model.Proxy
|
||||
if err != nil {
|
||||
@ -81,7 +81,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
p := common.ParseProxy(strings.Split(string(data), "\n")...)
|
||||
newProxies = p
|
||||
} else {
|
||||
// 如果无法直接解析,尝试Base64解码
|
||||
|
||||
base64, err := parser.DecodeBase64(string(data))
|
||||
if err != nil {
|
||||
logger.Logger.Debug(
|
||||
@ -104,17 +104,17 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
}
|
||||
proxyList = append(proxyList, newProxies...)
|
||||
}
|
||||
// 添加自定义节点
|
||||
|
||||
if len(query.Proxies) != 0 {
|
||||
proxyList = append(proxyList, common.ParseProxy(query.Proxies...)...)
|
||||
}
|
||||
// 给节点添加订阅名称
|
||||
|
||||
for i := range proxyList {
|
||||
if proxyList[i].SubName != "" {
|
||||
proxyList[i].Name = strings.TrimSpace(proxyList[i].SubName) + " " + strings.TrimSpace(proxyList[i].Name)
|
||||
}
|
||||
}
|
||||
// 去掉配置相同的节点
|
||||
|
||||
proxies := make(map[string]*model.Proxy)
|
||||
newProxies := make([]model.Proxy, 0, len(proxyList))
|
||||
for i := range proxyList {
|
||||
@ -125,7 +125,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
}
|
||||
}
|
||||
proxyList = newProxies
|
||||
// 删除节点
|
||||
|
||||
if strings.TrimSpace(query.Remove) != "" {
|
||||
newProxyList := make([]model.Proxy, 0, len(proxyList))
|
||||
for i := range proxyList {
|
||||
@ -134,17 +134,17 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
logger.Logger.Debug("remove regexp compile failed", zap.Error(err))
|
||||
return nil, errors.New("remove 参数非法: " + err.Error())
|
||||
}
|
||||
// 删除匹配到的节点
|
||||
|
||||
if removeReg.MatchString(proxyList[i].Name) {
|
||||
continue // 如果匹配到要删除的元素,跳过该元素,不添加到新切片中
|
||||
continue
|
||||
}
|
||||
newProxyList = append(newProxyList, proxyList[i]) // 将要保留的元素添加到新切片中
|
||||
newProxyList = append(newProxyList, proxyList[i])
|
||||
}
|
||||
proxyList = newProxyList
|
||||
}
|
||||
// 重命名
|
||||
|
||||
if len(query.ReplaceKeys) != 0 {
|
||||
// 创建重命名正则表达式
|
||||
|
||||
replaceRegs := make([]*regexp.Regexp, 0, len(query.ReplaceKeys))
|
||||
for _, v := range query.ReplaceKeys {
|
||||
replaceReg, err := regexp.Compile(v)
|
||||
@ -155,7 +155,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
replaceRegs = append(replaceRegs, replaceReg)
|
||||
}
|
||||
for i := range proxyList {
|
||||
// 重命名匹配到的节点
|
||||
|
||||
for j, v := range replaceRegs {
|
||||
if v.MatchString(proxyList[i].Name) {
|
||||
proxyList[i].Name = v.ReplaceAllString(
|
||||
@ -165,7 +165,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
}
|
||||
}
|
||||
}
|
||||
// 重名检测
|
||||
|
||||
names := make(map[string]int)
|
||||
for i := range proxyList {
|
||||
if _, exist := names[proxyList[i].Name]; exist {
|
||||
@ -175,14 +175,14 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
names[proxyList[i].Name] = 0
|
||||
}
|
||||
}
|
||||
// trim
|
||||
|
||||
for i := range proxyList {
|
||||
proxyList[i].Name = strings.TrimSpace(proxyList[i].Name)
|
||||
}
|
||||
// 将新增节点都添加到临时变量 t 中,防止策略组排序错乱
|
||||
|
||||
var t = &model.Subscription{}
|
||||
common.AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
|
||||
// 排序策略组
|
||||
|
||||
switch query.Sort {
|
||||
case "sizeasc":
|
||||
sort.Sort(model.ProxyGroupsSortBySize(t.ProxyGroups))
|
||||
@ -195,9 +195,9 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
default:
|
||||
sort.Sort(model.ProxyGroupsSortByName(t.ProxyGroups))
|
||||
}
|
||||
// 合并新节点和模板
|
||||
|
||||
MergeSubAndTemplate(temp, t, query.IgnoreCountryGrooup)
|
||||
// 处理自定义规则
|
||||
|
||||
for _, v := range query.Rules {
|
||||
if v.Prepend {
|
||||
common.PrependRules(temp, v.Rule)
|
||||
@ -205,7 +205,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
common.AppendRules(temp, v.Rule)
|
||||
}
|
||||
}
|
||||
// 处理自定义 ruleProvider
|
||||
|
||||
for _, v := range query.RuleProviders {
|
||||
hash := sha256.Sum224([]byte(v.Url))
|
||||
name := hex.EncodeToString(hash[:])
|
||||
@ -230,8 +230,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
}
|
||||
|
||||
func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg bool) {
|
||||
// 只合并节点、策略组
|
||||
// 统计所有国家策略组名称
|
||||
|
||||
var countryGroupNames []string
|
||||
for _, proxyGroup := range sub.ProxyGroups {
|
||||
if proxyGroup.IsCountryGrop {
|
||||
@ -244,9 +243,9 @@ func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription, igcg
|
||||
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
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func SubHandler(c *gin.Context) {
|
||||
// 从请求中获取参数
|
||||
|
||||
query, err := validator.ParseQuery(c)
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, err.Error())
|
||||
@ -23,7 +23,7 @@ func SubHandler(c *gin.Context) {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
// 输出
|
||||
|
||||
if query.NodeListMode {
|
||||
nodelist := model.NodeList{}
|
||||
nodelist.Proxies = sub.Proxies
|
||||
|
@ -88,7 +88,7 @@ func UpdateLinkHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetRawConfHandler(c *gin.Context) {
|
||||
// 获取动态路由参数
|
||||
|
||||
hash := c.Param("hash")
|
||||
password := c.Query("password")
|
||||
|
||||
@ -97,27 +97,24 @@ func GetRawConfHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 查询数据库中的短链接
|
||||
shortLink, err := database.FindShortLinkByHash(hash)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验密码
|
||||
if shortLink.Password != "" && shortLink.Password != password {
|
||||
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 更新最后访问时间
|
||||
shortLink.LastRequestTime = time.Now().Unix()
|
||||
err = database.SaveShortLink(shortLink)
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
||||
return
|
||||
}
|
||||
// 请求短链接指向的URL
|
||||
|
||||
response, err := http.Get("http://localhost:" + strconv.Itoa(config.Default.Port) + "/" + shortLink.Url)
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
|
||||
@ -125,19 +122,17 @@ func GetRawConfHandler(c *gin.Context) {
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
// 读取响应内容
|
||||
all, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
respondWithError(c, http.StatusInternalServerError, "读取错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 返回响应内容
|
||||
c.String(http.StatusOK, string(all))
|
||||
}
|
||||
|
||||
func GetRawConfUriHandler(c *gin.Context) {
|
||||
// 获取动态路由参数
|
||||
|
||||
hash := c.Query("hash")
|
||||
password := c.Query("password")
|
||||
|
||||
@ -146,14 +141,12 @@ func GetRawConfUriHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 查询数据库中的短链接
|
||||
shortLink, err := database.FindShortLinkByHash(hash)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验密码
|
||||
if shortLink.Password != "" && shortLink.Password != password {
|
||||
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
||||
return
|
||||
|
Reference in New Issue
Block a user