Clash.Meta/adapter/outboundgroup/parser.go

198 lines
5.6 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outboundgroup
import (
"errors"
"fmt"
"strings"
2019-12-08 12:17:24 +08:00
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/adapter/outbound"
"github.com/metacubex/mihomo/adapter/provider"
"github.com/metacubex/mihomo/common/structure"
"github.com/metacubex/mihomo/common/utils"
C "github.com/metacubex/mihomo/constant"
types "github.com/metacubex/mihomo/constant/provider"
2019-12-08 12:17:24 +08:00
)
var (
errFormat = errors.New("format error")
errType = errors.New("unsupported type")
errMissProxy = errors.New("`use` or `proxies` missing")
2022-01-03 17:21:27 +08:00
errDuplicateProvider = errors.New("duplicate provider name")
2019-12-08 12:17:24 +08:00
)
type GroupCommonOption struct {
outbound.BasicOption
Name string `group:"name"`
Type string `group:"type"`
Proxies []string `group:"proxies,omitempty"`
Use []string `group:"use,omitempty"`
URL string `group:"url,omitempty"`
Interval int `group:"interval,omitempty"`
Lazy bool `group:"lazy,omitempty"`
DisableUDP bool `group:"disable-udp,omitempty"`
Filter string `group:"filter,omitempty"`
ExcludeFilter string `group:"exclude-filter,omitempty"`
ExcludeType string `group:"exclude-type,omitempty"`
ExpectedStatus string `group:"expected-status,omitempty"`
IncludeAll bool `group:"include-all,omitempty"`
IncludeAllProxies bool `group:"include-all-proxies,omitempty"`
IncludeAllProviders bool `group:"include-all-providers,omitempty"`
2019-12-08 12:17:24 +08:00
}
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider, AllProxies []string, AllProviders []string) (C.ProxyAdapter, error) {
2019-12-08 12:17:24 +08:00
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
groupOption := &GroupCommonOption{
Lazy: true,
}
2019-12-08 12:17:24 +08:00
if err := decoder.Decode(config, groupOption); err != nil {
return nil, errFormat
}
if groupOption.Type == "" || groupOption.Name == "" {
return nil, errFormat
}
groupName := groupOption.Name
providers := []types.ProxyProvider{}
if groupOption.IncludeAll {
groupOption.IncludeAllProviders = true
groupOption.IncludeAllProxies = true
}
var GroupUse []string
var GroupProxies []string
if groupOption.IncludeAllProviders {
GroupUse = append(GroupUse, AllProviders...)
} else {
GroupUse = groupOption.Use
}
if groupOption.IncludeAllProxies {
GroupProxies = append(groupOption.Proxies, AllProxies...)
} else {
GroupProxies = groupOption.Proxies
}
if len(GroupProxies) == 0 && len(GroupUse) == 0 {
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
}
expectedStatus, err := utils.NewIntRanges[uint16](groupOption.ExpectedStatus)
if err != nil {
return nil, fmt.Errorf("%s: %w", groupName, err)
}
status := strings.TrimSpace(groupOption.ExpectedStatus)
if status == "" {
status = "*"
}
groupOption.ExpectedStatus = status
testUrl := groupOption.URL
2023-12-31 07:39:17 +08:00
if groupOption.URL == "" {
groupOption.URL = C.DefaultTestURL
testUrl = groupOption.URL
}
if len(GroupProxies) != 0 {
ps, err := getProxies(proxyMap, GroupProxies)
2019-12-08 12:17:24 +08:00
if err != nil {
return nil, fmt.Errorf("%s: %w", groupName, err)
2019-12-08 12:17:24 +08:00
}
if _, ok := providersMap[groupName]; ok {
return nil, fmt.Errorf("%s: %w", groupName, errDuplicateProvider)
}
// select don't need health check
if groupOption.Type != "select" && groupOption.Type != "relay" {
2022-06-07 17:19:25 +08:00
if groupOption.Interval == 0 {
2022-05-23 00:40:27 +08:00
groupOption.Interval = 300
2020-08-25 22:19:59 +08:00
}
}
hc := provider.NewHealthCheck(ps, testUrl, uint(groupOption.Interval), groupOption.Lazy, expectedStatus)
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
if err != nil {
return nil, fmt.Errorf("%s: %w", groupName, err)
2019-12-08 12:17:24 +08:00
}
providers = append(providers, pd)
providersMap[groupName] = pd
2019-12-08 12:17:24 +08:00
}
if len(GroupUse) != 0 {
list, err := getProviders(providersMap, GroupUse)
2019-12-08 12:17:24 +08:00
if err != nil {
return nil, fmt.Errorf("%s: %w", groupName, err)
2019-12-08 12:17:24 +08:00
}
// different proxy groups use different test URL
addTestUrlToProviders(list, testUrl, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
2019-12-08 12:17:24 +08:00
providers = append(providers, list...)
2022-01-05 12:19:49 +08:00
} else {
groupOption.Filter = ""
2019-12-08 12:17:24 +08:00
}
var group C.ProxyAdapter
switch groupOption.Type {
case "url-test":
2020-05-29 17:47:50 +08:00
opts := parseURLTestOption(config)
group = NewURLTest(groupOption, providers, opts...)
2019-12-08 12:17:24 +08:00
case "select":
group = NewSelector(groupOption, providers)
2019-12-08 12:17:24 +08:00
case "fallback":
group = NewFallback(groupOption, providers)
2019-12-08 12:17:24 +08:00
case "load-balance":
strategy := parseStrategy(config)
return NewLoadBalance(groupOption, providers, strategy)
case "relay":
group = NewRelay(groupOption, providers)
2019-12-08 12:17:24 +08:00
default:
return nil, fmt.Errorf("%w: %s", errType, groupOption.Type)
}
return group, nil
}
func getProxies(mapping map[string]C.Proxy, list []string) ([]C.Proxy, error) {
var ps []C.Proxy
for _, name := range list {
p, ok := mapping[name]
if !ok {
return nil, fmt.Errorf("'%s' not found", name)
}
ps = append(ps, p)
}
return ps, nil
}
func getProviders(mapping map[string]types.ProxyProvider, list []string) ([]types.ProxyProvider, error) {
var ps []types.ProxyProvider
2019-12-08 12:17:24 +08:00
for _, name := range list {
p, ok := mapping[name]
if !ok {
return nil, fmt.Errorf("'%s' not found", name)
}
if p.VehicleType() == types.Compatible {
2019-12-08 12:17:24 +08:00
return nil, fmt.Errorf("proxy group %s can't contains in `use`", name)
}
ps = append(ps, p)
}
return ps, nil
}
func addTestUrlToProviders(providers []types.ProxyProvider, url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
if len(providers) == 0 || len(url) == 0 {
return
}
for _, pd := range providers {
pd.RegisterHealthCheckTask(url, expectedStatus, filter, interval)
}
}