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

142 lines
3.6 KiB
Go
Raw Normal View History

package common
2023-09-12 06:40:24 -04:00
import (
"strings"
2024-04-23 02:39:16 -04:00
"sub2clash/constant"
"sub2clash/logger"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
"sub2clash/parser"
"go.uber.org/zap"
2023-09-12 06:40:24 -04:00
)
func GetContryName(countryKey string) string {
2023-09-12 12:46:17 -04:00
// 创建一个切片包含所有的国家映射
countryMaps := []map[string]string{
model.CountryFlag,
model.CountryChineseName,
model.CountryISO,
model.CountryEnglishName,
2023-09-12 06:40:24 -04:00
}
2023-09-12 12:46:17 -04:00
// 对每一个映射进行检查
for i, countryMap := range countryMaps {
if i == 2 {
// 对ISO匹配做特殊处理
// 根据常用分割字符分割字符串
splitChars := []string{"-", "_", " "}
key := make([]string, 0)
for _, splitChar := range splitChars {
slic := strings.Split(countryKey, splitChar)
for _, v := range slic {
if len(v) == 2 {
key = append(key, v)
}
}
}
// 对每一个分割后的字符串进行检查
for _, v := range key {
// 如果匹配到了国家
if country, ok := countryMap[strings.ToUpper(v)]; ok {
return country
}
}
}
2023-09-12 12:46:17 -04:00
for k, v := range countryMap {
if strings.Contains(countryKey, k) {
2023-09-12 12:46:17 -04:00
return v
}
2023-09-12 06:40:24 -04:00
}
}
return "其他地区"
}
func AddProxy(
sub *model.Subscription, autotest bool,
lazy bool, clashType model.ClashType, proxies ...model.Proxy,
) {
proxyTypes := model.GetSupportProxyTypes(clashType)
// 添加节点
2023-09-12 12:46:17 -04:00
for _, proxy := range proxies {
if !proxyTypes[proxy.Type] {
continue
}
2023-09-12 06:40:24 -04:00
sub.Proxies = append(sub.Proxies, proxy)
haveProxyGroup := false
countryName := GetContryName(proxy.Name)
2023-09-12 06:40:24 -04:00
for i := range sub.ProxyGroups {
group := &sub.ProxyGroups[i]
2023-09-12 12:46:17 -04:00
if group.Name == countryName {
2023-09-12 06:40:24 -04:00
group.Proxies = append(group.Proxies, proxy.Name)
group.Size++
2023-09-12 06:40:24 -04:00
haveProxyGroup = true
}
}
if !haveProxyGroup {
2023-09-13 01:47:22 -04:00
var newGroup model.ProxyGroup
if !autotest {
newGroup = model.ProxyGroup{
Name: countryName,
Type: "select",
Proxies: []string{proxy.Name},
IsCountryGrop: true,
Size: 1,
2023-09-13 01:47:22 -04:00
}
} else {
newGroup = model.ProxyGroup{
Name: countryName,
Type: "url-test",
Proxies: []string{proxy.Name},
IsCountryGrop: true,
Url: "http://www.gstatic.com/generate_204",
Interval: 300,
Tolerance: 50,
Lazy: lazy,
Size: 1,
2023-09-13 01:47:22 -04:00
}
2023-09-12 06:40:24 -04:00
}
sub.ProxyGroups = append(sub.ProxyGroups, newGroup)
}
}
}
func ParseProxy(proxies ...string) []model.Proxy {
var result []model.Proxy
for _, proxy := range proxies {
if proxy != "" {
var proxyItem model.Proxy
var err error
// 解析节点
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
proxyItem, err = parser.ParseShadowsocks(proxy)
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.TrojanPrefix) {
2023-09-12 06:40:24 -04:00
proxyItem, err = parser.ParseTrojan(proxy)
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.VMessPrefix) {
2023-09-12 06:40:24 -04:00
proxyItem, err = parser.ParseVmess(proxy)
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.VLESSPrefix) {
2023-09-12 06:40:24 -04:00
proxyItem, err = parser.ParseVless(proxy)
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.ShadowsocksRPrefix) {
2023-09-12 06:40:24 -04:00
proxyItem, err = parser.ParseShadowsocksR(proxy)
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.Hysteria2Prefix1) || strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
2023-10-31 03:14:29 -04:00
proxyItem, err = parser.ParseHysteria2(proxy)
}
2024-04-23 02:39:16 -04:00
if strings.HasPrefix(proxy, constant.HysteriaPrefix) {
2024-03-09 04:01:52 -05:00
proxyItem, err = parser.ParseHysteria(proxy)
}
2023-09-12 06:40:24 -04:00
if err == nil {
result = append(result, proxyItem)
} else {
logger.Logger.Debug(
"parse proxy failed", zap.String("proxy", proxy), zap.Error(err),
)
2023-09-12 06:40:24 -04:00
}
}
}
return result
}