1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 14:44:42 -05:00
sub2clash/common/proxy.go

144 lines
3.5 KiB
Go
Raw Permalink Normal View History

package common
2023-09-12 06:40:24 -04:00
import (
"strings"
"github.com/nitezs/sub2clash/constant"
"github.com/nitezs/sub2clash/logger"
"github.com/nitezs/sub2clash/model"
"github.com/nitezs/sub2clash/parser"
"go.uber.org/zap"
2023-09-12 06:40:24 -04:00
)
func GetContryName(countryKey string) string {
2024-08-11 11:55:47 -04:00
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 {
2024-08-11 11:55:47 -04:00
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)
}
}
}
2024-08-11 11:55:47 -04:00
for _, v := range key {
2024-08-11 11:55:47 -04:00
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)
2024-08-11 11:55:47 -04:00
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-08-11 11:55:47 -04:00
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)
}
2024-11-12 20:21:31 -05:00
if strings.HasPrefix(proxy, constant.SocksPrefix) {
proxyItem, err = parser.ParseSocks(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
}