Clash.Meta/adapter/outboundgroup/parser.go

153 lines
4.1 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outboundgroup
import (
"errors"
"fmt"
"github.com/Dreamacro/clash/adapter/outbound"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter/provider"
2019-12-08 12:17:24 +08:00
"github.com/Dreamacro/clash/common/structure"
C "github.com/Dreamacro/clash/constant"
types "github.com/Dreamacro/clash/constant/provider"
2019-12-08 12:17:24 +08:00
)
var (
errFormat = errors.New("format error")
errType = errors.New("unsupport type")
errMissProxy = errors.New("`use` or `proxies` missing")
2019-12-08 12:17:24 +08:00
errMissHealthCheck = errors.New("`url` or `interval` 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"`
2022-01-05 12:19:49 +08:00
Filter string `group:"filter,omitempty"`
2019-12-08 12:17:24 +08:00
}
2022-03-16 12:10:13 +08:00
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (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 len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
return nil, errMissProxy
}
2019-12-08 12:17:24 +08:00
if len(groupOption.Proxies) != 0 {
ps, err := getProxies(proxyMap, groupOption.Proxies)
if err != nil {
return nil, err
}
if _, ok := providersMap[groupName]; ok {
return nil, errDuplicateProvider
}
// select don't need health check
if groupOption.Type == "select" || groupOption.Type == "relay" {
hc := provider.NewHealthCheck(ps, "", 0, true)
2020-01-11 21:02:55 +08:00
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
2019-12-08 12:17:24 +08:00
if err != nil {
return nil, err
}
providers = append(providers, pd)
providersMap[groupName] = pd
2019-12-08 12:17:24 +08:00
} else {
if groupOption.URL == "" || groupOption.Interval == 0 {
2022-05-23 00:40:27 +08:00
//return nil, errMissHealthCheck
groupOption.URL = "http://www.gstatic.com/generate_204"
groupOption.Interval = 300
2020-08-25 22:19:59 +08:00
}
hc := provider.NewHealthCheck(ps, groupOption.URL, uint(groupOption.Interval), groupOption.Lazy)
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
if err != nil {
return nil, 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(groupOption.Use) != 0 {
list, err := getProviders(providersMap, groupOption.Use)
if err != nil {
return nil, err
}
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
}