1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 22:14:41 -05:00
sub2clash/utils/proxy.go

110 lines
2.4 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package utils
import (
"strings"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
"sub2clash/parser"
2023-09-12 06:40:24 -04:00
)
2023-09-12 12:46:17 -04:00
func GetContryName(proxy model.Proxy) string {
// 创建一个切片包含所有的国家映射
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 _, countryMap := range countryMaps {
for k, v := range countryMap {
if strings.Contains(proxy.Name, k) {
return v
}
2023-09-12 06:40:24 -04:00
}
}
2023-09-12 12:46:17 -04:00
2023-09-12 06:40:24 -04:00
return "其他地区"
}
var skipGroups = map[string]bool{
"手动切换": true,
"全球直连": true,
"广告拦截": true,
"应用进化": true,
}
func AddProxy(sub *model.Subscription, proxies ...model.Proxy) {
2023-09-12 12:46:17 -04:00
newCountryGroupNames := make([]string, 0)
for _, proxy := range proxies {
2023-09-12 06:40:24 -04:00
sub.Proxies = append(sub.Proxies, proxy)
2023-09-12 12:46:17 -04:00
2023-09-12 06:40:24 -04:00
haveProxyGroup := false
2023-09-12 12:46:17 -04:00
countryName := GetContryName(proxy)
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)
haveProxyGroup = true
}
if group.Name == "手动切换" {
group.Proxies = append(group.Proxies, proxy.Name)
}
}
2023-09-12 12:46:17 -04:00
2023-09-12 06:40:24 -04:00
if !haveProxyGroup {
newGroup := model.ProxyGroup{
2023-09-12 12:46:17 -04:00
Name: countryName,
Type: "select",
Proxies: []string{proxy.Name},
IsCountryGrop: true,
2023-09-12 06:40:24 -04:00
}
sub.ProxyGroups = append(sub.ProxyGroups, newGroup)
2023-09-12 12:46:17 -04:00
newCountryGroupNames = append(newCountryGroupNames, countryName)
2023-09-12 06:40:24 -04:00
}
}
2023-09-12 12:46:17 -04:00
2023-09-12 06:40:24 -04:00
for i := range sub.ProxyGroups {
2023-09-12 12:46:17 -04:00
if sub.ProxyGroups[i].IsCountryGrop {
continue
}
if !skipGroups[sub.ProxyGroups[i].Name] {
sub.ProxyGroups[i].Proxies = append(newCountryGroupNames, sub.ProxyGroups[i].Proxies...)
2023-09-12 06:40:24 -04:00
}
}
}
func ParseProxy(proxies ...string) []model.Proxy {
var result []model.Proxy
for _, proxy := range proxies {
if proxy != "" {
var proxyItem model.Proxy
var err error
// 解析节点
if strings.HasPrefix(proxy, "ss://") {
proxyItem, err = parser.ParseSS(proxy)
}
if strings.HasPrefix(proxy, "trojan://") {
proxyItem, err = parser.ParseTrojan(proxy)
}
if strings.HasPrefix(proxy, "vmess://") {
proxyItem, err = parser.ParseVmess(proxy)
}
if strings.HasPrefix(proxy, "vless://") {
proxyItem, err = parser.ParseVless(proxy)
}
if strings.HasPrefix(proxy, "ssr://") {
proxyItem, err = parser.ParseShadowsocksR(proxy)
}
if err == nil {
result = append(result, proxyItem)
}
}
}
return result
}