2024-04-23 02:47:53 -04:00
|
|
|
package common
|
2023-09-12 06:40:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/constant"
|
|
|
|
"github.com/nitezs/sub2clash/logger"
|
|
|
|
"github.com/nitezs/sub2clash/model"
|
|
|
|
"github.com/nitezs/sub2clash/parser"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2023-09-12 06:40:24 -04:00
|
|
|
)
|
|
|
|
|
2023-09-25 11:58:13 -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
|
|
|
|
2023-09-24 02:48:42 -04:00
|
|
|
for i, countryMap := range countryMaps {
|
|
|
|
if i == 2 {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-24 02:48:42 -04:00
|
|
|
splitChars := []string{"-", "_", " "}
|
|
|
|
key := make([]string, 0)
|
|
|
|
for _, splitChar := range splitChars {
|
2023-09-25 11:58:13 -04:00
|
|
|
slic := strings.Split(countryKey, splitChar)
|
2023-09-24 02:48:42 -04:00
|
|
|
for _, v := range slic {
|
|
|
|
if len(v) == 2 {
|
|
|
|
key = append(key, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-24 02:48:42 -04:00
|
|
|
for _, v := range key {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-24 02:48:42 -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 {
|
2023-09-25 11:58:13 -04:00
|
|
|
if strings.Contains(countryKey, k) {
|
2023-09-12 12:46:17 -04:00
|
|
|
return v
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "其他地区"
|
|
|
|
}
|
|
|
|
|
2023-09-14 12:13:45 -04:00
|
|
|
func AddProxy(
|
2023-09-16 09:46:59 -04:00
|
|
|
sub *model.Subscription, autotest bool,
|
2023-09-20 21:08:02 -04:00
|
|
|
lazy bool, clashType model.ClashType, proxies ...model.Proxy,
|
2023-09-14 12:13:45 -04:00
|
|
|
) {
|
2023-09-16 09:46:59 -04:00
|
|
|
proxyTypes := model.GetSupportProxyTypes(clashType)
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-12 12:46:17 -04:00
|
|
|
for _, proxy := range proxies {
|
2023-09-16 09:46:59 -04:00
|
|
|
if !proxyTypes[proxy.Type] {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
sub.Proxies = append(sub.Proxies, proxy)
|
|
|
|
haveProxyGroup := false
|
2023-09-25 11:58:13 -04:00
|
|
|
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)
|
2023-09-14 12:13:45 -04:00
|
|
|
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,
|
2023-09-14 12:13:45 -04:00
|
|
|
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,
|
2023-09-14 12:13:45 -04:00
|
|
|
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)
|
2023-09-24 06:06:44 -04:00
|
|
|
} else {
|
|
|
|
logger.Logger.Debug(
|
|
|
|
"parse proxy failed", zap.String("proxy", proxy), zap.Error(err),
|
|
|
|
)
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|