2019-12-08 04:17:24 +00:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-06-04 03:51:30 +00:00
|
|
|
"strings"
|
2019-12-08 04:17:24 +00:00
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/provider"
|
2019-12-08 04:17:24 +00:00
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
2023-06-04 03:51:30 +00:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
2019-12-08 04:17:24 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 12:32:59 +00:00
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
2019-12-08 04:17:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errFormat = errors.New("format error")
|
2023-06-04 03:51:30 +00:00
|
|
|
errType = errors.New("unsupported type")
|
2019-12-14 10:13:33 +00:00
|
|
|
errMissProxy = errors.New("`use` or `proxies` missing")
|
2022-01-03 09:21:27 +00:00
|
|
|
errDuplicateProvider = errors.New("duplicate provider name")
|
2019-12-08 04:17:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GroupCommonOption struct {
|
2021-11-07 08:48:51 +00:00
|
|
|
outbound.BasicOption
|
2023-06-04 03:51:30 +00:00
|
|
|
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"`
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 04:10:13 +00:00
|
|
|
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
2019-12-08 04:17:24 +00:00
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
groupOption := &GroupCommonOption{
|
|
|
|
Lazy: true,
|
|
|
|
}
|
2019-12-08 04:17:24 +00:00
|
|
|
if err := decoder.Decode(config, groupOption); err != nil {
|
|
|
|
return nil, errFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
if groupOption.Type == "" || groupOption.Name == "" {
|
|
|
|
return nil, errFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
groupName := groupOption.Name
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
providers := []types.ProxyProvider{}
|
2019-12-14 10:13:33 +00:00
|
|
|
|
|
|
|
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
2023-06-06 01:45:05 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
2019-12-14 10:13:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 03:51:30 +00:00
|
|
|
expectedStatus, err := utils.NewIntRanges[uint16](groupOption.ExpectedStatus)
|
|
|
|
if err != nil {
|
2023-06-06 01:45:05 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2023-06-04 03:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status := strings.TrimSpace(groupOption.ExpectedStatus)
|
|
|
|
if status == "" {
|
|
|
|
status = "*"
|
|
|
|
}
|
|
|
|
groupOption.ExpectedStatus = status
|
|
|
|
testUrl := groupOption.URL
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
if len(groupOption.Proxies) != 0 {
|
|
|
|
ps, err := getProxies(proxyMap, groupOption.Proxies)
|
|
|
|
if err != nil {
|
2023-06-06 01:45:05 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-12 12:37:30 +00:00
|
|
|
if _, ok := providersMap[groupName]; ok {
|
2023-06-06 01:45:05 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, errDuplicateProvider)
|
2021-12-12 12:37:30 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 03:04:03 +00:00
|
|
|
var url string
|
|
|
|
var interval uint
|
2019-12-08 04:17:24 +00:00
|
|
|
|
2023-06-04 03:51:30 +00:00
|
|
|
// select don't need health check
|
|
|
|
if groupOption.Type != "select" && groupOption.Type != "relay" {
|
2022-06-07 09:19:25 +00:00
|
|
|
if groupOption.URL == "" {
|
2023-01-27 05:41:23 +00:00
|
|
|
groupOption.URL = "https://cp.cloudflare.com/generate_204"
|
2022-06-07 09:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if groupOption.Interval == 0 {
|
2022-05-22 16:40:27 +00:00
|
|
|
groupOption.Interval = 300
|
2020-08-25 14:19:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 03:04:03 +00:00
|
|
|
url = groupOption.URL
|
|
|
|
interval = uint(groupOption.Interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
hc := provider.NewHealthCheck(ps, url, interval, true, expectedStatus)
|
|
|
|
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
2023-06-04 03:51:30 +00:00
|
|
|
|
|
|
|
providers = append(providers, pd)
|
|
|
|
providersMap[groupName] = pd
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(groupOption.Use) != 0 {
|
|
|
|
list, err := getProviders(providersMap, groupOption.Use)
|
|
|
|
if err != nil {
|
2023-06-06 01:45:05 +00:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
2023-06-04 03:51:30 +00:00
|
|
|
|
|
|
|
// different proxy groups use different test URL
|
|
|
|
addTestUrlToProviders(list, testUrl, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
providers = append(providers, list...)
|
2022-01-05 04:19:49 +00:00
|
|
|
} else {
|
|
|
|
groupOption.Filter = ""
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var group C.ProxyAdapter
|
|
|
|
switch groupOption.Type {
|
|
|
|
case "url-test":
|
2020-05-29 09:47:50 +00:00
|
|
|
opts := parseURLTestOption(config)
|
2020-11-13 13:48:52 +00:00
|
|
|
group = NewURLTest(groupOption, providers, opts...)
|
2019-12-08 04:17:24 +00:00
|
|
|
case "select":
|
2020-11-13 13:48:52 +00:00
|
|
|
group = NewSelector(groupOption, providers)
|
2019-12-08 04:17:24 +00:00
|
|
|
case "fallback":
|
2020-11-13 13:48:52 +00:00
|
|
|
group = NewFallback(groupOption, providers)
|
2019-12-08 04:17:24 +00:00
|
|
|
case "load-balance":
|
2020-10-28 14:35:02 +00:00
|
|
|
strategy := parseStrategy(config)
|
2020-11-13 13:48:52 +00:00
|
|
|
return NewLoadBalance(groupOption, providers, strategy)
|
2020-03-21 15:46:49 +00:00
|
|
|
case "relay":
|
2020-11-13 13:48:52 +00:00
|
|
|
group = NewRelay(groupOption, providers)
|
2019-12-08 04:17:24 +00: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
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
func getProviders(mapping map[string]types.ProxyProvider, list []string) ([]types.ProxyProvider, error) {
|
|
|
|
var ps []types.ProxyProvider
|
2019-12-08 04:17:24 +00:00
|
|
|
for _, name := range list {
|
|
|
|
p, ok := mapping[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("'%s' not found", name)
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:32:59 +00:00
|
|
|
if p.VehicleType() == types.Compatible {
|
2019-12-08 04:17:24 +00:00
|
|
|
return nil, fmt.Errorf("proxy group %s can't contains in `use`", name)
|
|
|
|
}
|
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
|
|
|
return ps, nil
|
|
|
|
}
|
2023-06-04 03:51:30 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|