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

feat: 增加将订阅名称添加到节点名中的功能

feat: 增加地区模板变量
This commit is contained in:
Nite07 2023-09-25 23:58:13 +08:00
parent 6a5cfd35c9
commit 6f075ea44e
5 changed files with 48 additions and 13 deletions

View File

@ -4,7 +4,7 @@
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 | | Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|--------------|--------|-------------------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |--------------|--------|-------------------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sub | string | sub/proxy 至少有一项存在 | - | 订阅链接(可以输入多个,用 `,` 分隔) | | sub | string | sub/proxy 至少有一项存在 | - | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个,用 `,` 分隔) |
| proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) | | proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) |
| refresh | bool | 否 | `false` | 强制刷新配置(默认缓存 5 分钟) | | refresh | bool | 否 | `false` | 强制刷新配置(默认缓存 5 分钟) |
| template | string | 否 | - | 外部模板链接或内部模板名称 | | template | string | 否 | - | 外部模板链接或内部模板名称 |

View File

@ -48,6 +48,7 @@
- `<all>` 为添加所有节点 - `<all>` 为添加所有节点
- `<countries>` 为添加所有国家策略组 - `<countries>` 为添加所有国家策略组
- `<地区二位字母代码>` 为添加指定地区所有节点,例如 `<hk>` 将添加所有香港节点
#### 默认模板 #### 默认模板

View File

@ -58,6 +58,10 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
// 加载订阅 // 加载订阅
for i := range query.Subs { for i := range query.Subs {
data, err := utils.LoadSubscription(query.Subs[i], query.Refresh) 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:]
}
if err != nil { if err != nil {
logger.Logger.Debug( logger.Logger.Debug(
"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err), "load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
@ -66,11 +70,12 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
} }
// 解析订阅 // 解析订阅
err = yaml.Unmarshal(data, &sub) err = yaml.Unmarshal(data, &sub)
newProxies := make([]model.Proxy, 0)
if err != nil { if err != nil {
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless)://") reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless)://")
if reg.Match(data) { if reg.Match(data) {
p := utils.ParseProxy(strings.Split(string(data), "\n")...) p := utils.ParseProxy(strings.Split(string(data), "\n")...)
proxyList = append(proxyList, p...) newProxies = p
} else { } else {
// 如果无法直接解析尝试Base64解码 // 如果无法直接解析尝试Base64解码
base64, err := parser.DecodeBase64(string(data)) base64, err := parser.DecodeBase64(string(data))
@ -83,16 +88,28 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
return nil, errors.New("加载订阅失败: " + err.Error()) return nil, errors.New("加载订阅失败: " + err.Error())
} }
p := utils.ParseProxy(strings.Split(base64, "\n")...) p := utils.ParseProxy(strings.Split(base64, "\n")...)
proxyList = append(proxyList, p...) newProxies = p
} }
} else { } else {
proxyList = append(proxyList, sub.Proxies...) newProxies = sub.Proxies
} }
if subName != "" {
for i := range newProxies {
newProxies[i].SubName = subName
}
}
proxyList = append(proxyList, newProxies...)
} }
// 添加自定义节点 // 添加自定义节点
if len(query.Proxies) != 0 { if len(query.Proxies) != 0 {
proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...) 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)
}
}
// 去掉配置相同的节点 // 去掉配置相同的节点
proxies := make(map[string]*model.Proxy) proxies := make(map[string]*model.Proxy)
newProxies := make([]model.Proxy, 0, len(proxyList)) newProxies := make([]model.Proxy, 0, len(proxyList))
@ -233,11 +250,28 @@ func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription) {
continue continue
} }
newProxies := make([]string, 0, len(temp.ProxyGroups[i].Proxies)) newProxies := make([]string, 0, len(temp.ProxyGroups[i].Proxies))
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 { for j := range temp.ProxyGroups[i].Proxies {
if temp.ProxyGroups[i].Proxies[j] == "<all>" { 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...) newProxies = append(newProxies, proxyNames...)
} else if temp.ProxyGroups[i].Proxies[j] == "<countries>" { case "countries":
newProxies = append(newProxies, countryGroupNames...) newProxies = append(newProxies, countryGroupNames...)
default:
if len(key) == 2 {
newProxies = append(
newProxies, countryGroupMap[utils.GetContryName(key)].Proxies...,
)
}
}
} else { } else {
newProxies = append(newProxies, temp.ProxyGroups[i].Proxies[j]) newProxies = append(newProxies, temp.ProxyGroups[i].Proxies[j])
} }

View File

@ -44,6 +44,7 @@ type Proxy struct {
AuthenticatedLength bool `yaml:"authenticated-length,omitempty"` AuthenticatedLength bool `yaml:"authenticated-length,omitempty"`
UDPOverTCP bool `yaml:"udp-over-tcp,omitempty"` UDPOverTCP bool `yaml:"udp-over-tcp,omitempty"`
UDPOverTCPVersion int `yaml:"udp-over-tcp-version,omitempty"` UDPOverTCPVersion int `yaml:"udp-over-tcp-version,omitempty"`
SubName string `yaml:"-"`
} }
func (p Proxy) MarshalYAML() (interface{}, error) { func (p Proxy) MarshalYAML() (interface{}, error) {

View File

@ -8,7 +8,7 @@ import (
"sub2clash/parser" "sub2clash/parser"
) )
func GetContryName(proxy model.Proxy) string { func GetContryName(countryKey string) string {
// 创建一个切片包含所有的国家映射 // 创建一个切片包含所有的国家映射
countryMaps := []map[string]string{ countryMaps := []map[string]string{
model.CountryFlag, model.CountryFlag,
@ -25,7 +25,7 @@ func GetContryName(proxy model.Proxy) string {
splitChars := []string{"-", "_", " "} splitChars := []string{"-", "_", " "}
key := make([]string, 0) key := make([]string, 0)
for _, splitChar := range splitChars { for _, splitChar := range splitChars {
slic := strings.Split(proxy.Name, splitChar) slic := strings.Split(countryKey, splitChar)
for _, v := range slic { for _, v := range slic {
if len(v) == 2 { if len(v) == 2 {
key = append(key, v) key = append(key, v)
@ -41,12 +41,11 @@ func GetContryName(proxy model.Proxy) string {
} }
} }
for k, v := range countryMap { for k, v := range countryMap {
if strings.Contains(proxy.Name, k) { if strings.Contains(countryKey, k) {
return v return v
} }
} }
} }
return "其他地区" return "其他地区"
} }
@ -62,7 +61,7 @@ func AddProxy(
} }
sub.Proxies = append(sub.Proxies, proxy) sub.Proxies = append(sub.Proxies, proxy)
haveProxyGroup := false haveProxyGroup := false
countryName := GetContryName(proxy) countryName := GetContryName(proxy.Name)
for i := range sub.ProxyGroups { for i := range sub.ProxyGroups {
group := &sub.ProxyGroups[i] group := &sub.ProxyGroups[i]