2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-06-04 11:51:30 +08:00
|
|
|
"strings"
|
2019-12-08 12:17:24 +08:00
|
|
|
|
2024-05-13 20:30:31 +08:00
|
|
|
"github.com/dlclark/regexp2"
|
|
|
|
|
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")
|
2023-06-04 11:51:30 +08:00
|
|
|
errType = errors.New("unsupported type")
|
2019-12-14 18:13:33 +08:00
|
|
|
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 {
|
2021-11-07 16:48:51 +08:00
|
|
|
outbound.BasicOption
|
2023-11-11 22:15:57 +08: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"`
|
2024-01-24 12:45:35 +08:00
|
|
|
TestTimeout int `group:"timeout,omitempty"`
|
2024-03-07 03:35:11 +08:00
|
|
|
MaxFailedTimes int `group:"max-failed-times,omitempty"`
|
2023-11-11 22:15:57 +08:00
|
|
|
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"`
|
2023-12-31 09:43:52 +08:00
|
|
|
IncludeAll bool `group:"include-all,omitempty"`
|
|
|
|
IncludeAllProxies bool `group:"include-all-proxies,omitempty"`
|
2023-11-11 22:15:57 +08:00
|
|
|
IncludeAllProviders bool `group:"include-all-providers,omitempty"`
|
2024-01-07 23:32:22 +08:00
|
|
|
Hidden bool `group:"hidden,omitempty"`
|
|
|
|
Icon string `group:"icon,omitempty"`
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-12-31 09:43:52 +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})
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
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
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
providers := []types.ProxyProvider{}
|
2019-12-14 18:13:33 +08:00
|
|
|
|
2023-12-31 09:43:52 +08:00
|
|
|
if groupOption.IncludeAll {
|
|
|
|
groupOption.IncludeAllProviders = true
|
|
|
|
groupOption.IncludeAllProxies = true
|
|
|
|
}
|
2024-02-21 22:49:28 +08:00
|
|
|
|
2023-11-11 22:15:57 +08:00
|
|
|
if groupOption.IncludeAllProviders {
|
2024-07-15 13:12:40 +08:00
|
|
|
groupOption.Use = AllProviders
|
2023-11-11 22:15:57 +08:00
|
|
|
}
|
2023-12-31 09:43:52 +08:00
|
|
|
if groupOption.IncludeAllProxies {
|
2024-05-13 20:30:31 +08:00
|
|
|
if groupOption.Filter != "" {
|
|
|
|
var filterRegs []*regexp2.Regexp
|
|
|
|
for _, filter := range strings.Split(groupOption.Filter, "`") {
|
2024-05-15 10:44:56 +08:00
|
|
|
filterReg := regexp2.MustCompile(filter, regexp2.None)
|
2024-05-13 20:30:31 +08:00
|
|
|
filterRegs = append(filterRegs, filterReg)
|
|
|
|
}
|
|
|
|
for _, p := range AllProxies {
|
|
|
|
for _, filterReg := range filterRegs {
|
2024-05-15 10:44:56 +08:00
|
|
|
if mat, _ := filterReg.MatchString(p); mat {
|
2024-05-13 20:30:31 +08:00
|
|
|
groupOption.Proxies = append(groupOption.Proxies, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
groupOption.Proxies = append(groupOption.Proxies, AllProxies...)
|
|
|
|
}
|
2024-08-19 16:02:43 +08:00
|
|
|
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
|
|
|
groupOption.Proxies = []string{"COMPATIBLE"}
|
|
|
|
}
|
2023-12-31 09:43:52 +08:00
|
|
|
}
|
2023-11-11 22:15:57 +08:00
|
|
|
|
2024-02-21 22:49:28 +08:00
|
|
|
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
2023-06-06 09:45:05 +08:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
2019-12-14 18:13:33 +08:00
|
|
|
}
|
|
|
|
|
2024-01-20 11:00:06 +08:00
|
|
|
expectedStatus, err := utils.NewUnsignedRanges[uint16](groupOption.ExpectedStatus)
|
2023-06-04 11:51:30 +08:00
|
|
|
if err != nil {
|
2023-06-06 09:45:05 +08:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2023-06-04 11:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
status := strings.TrimSpace(groupOption.ExpectedStatus)
|
|
|
|
if status == "" {
|
|
|
|
status = "*"
|
|
|
|
}
|
|
|
|
groupOption.ExpectedStatus = status
|
2023-12-31 07:39:17 +08:00
|
|
|
|
2024-04-05 19:45:36 +08:00
|
|
|
if len(groupOption.Use) != 0 {
|
|
|
|
PDs, err := getProviders(providersMap, groupOption.Use)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if test URL is empty, use the first health check URL of providers
|
|
|
|
if groupOption.URL == "" {
|
|
|
|
for _, pd := range PDs {
|
|
|
|
if pd.HealthCheckURL() != "" {
|
|
|
|
groupOption.URL = pd.HealthCheckURL()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if groupOption.URL == "" {
|
|
|
|
groupOption.URL = C.DefaultTestURL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addTestUrlToProviders(PDs, groupOption.URL, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
|
|
|
|
}
|
|
|
|
providers = append(providers, PDs...)
|
|
|
|
}
|
|
|
|
|
2024-02-21 22:49:28 +08:00
|
|
|
if len(groupOption.Proxies) != 0 {
|
|
|
|
ps, err := getProxies(proxyMap, groupOption.Proxies)
|
2019-12-08 12:17:24 +08:00
|
|
|
if err != nil {
|
2023-06-06 09:45:05 +08:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2021-12-12 20:37:30 +08:00
|
|
|
if _, ok := providersMap[groupName]; ok {
|
2023-06-06 09:45:05 +08:00
|
|
|
return nil, fmt.Errorf("%s: %w", groupName, errDuplicateProvider)
|
2021-12-12 20:37:30 +08:00
|
|
|
}
|
|
|
|
|
2024-04-05 01:51:18 +08:00
|
|
|
if groupOption.URL == "" {
|
|
|
|
groupOption.URL = C.DefaultTestURL
|
2023-06-07 11:04:03 +08:00
|
|
|
}
|
|
|
|
|
2024-04-05 19:45:36 +08:00
|
|
|
// select don't need auto health check
|
|
|
|
if groupOption.Type != "select" && groupOption.Type != "relay" {
|
|
|
|
if groupOption.Interval == 0 {
|
|
|
|
groupOption.Interval = 300
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 22:49:28 +08:00
|
|
|
hc := provider.NewHealthCheck(ps, groupOption.URL, uint(groupOption.TestTimeout), uint(groupOption.Interval), groupOption.Lazy, expectedStatus)
|
2023-11-13 17:42:25 +08:00
|
|
|
|
2023-06-07 11:04:03 +08:00
|
|
|
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
|
|
|
}
|
2023-06-04 11:51:30 +08:00
|
|
|
|
2024-04-05 19:45:36 +08:00
|
|
|
providers = append([]types.ProxyProvider{pd}, providers...)
|
2023-06-04 11:51:30 +08:00
|
|
|
providersMap[groupName] = pd
|
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)
|
2020-11-13 21:48:52 +08:00
|
|
|
group = NewURLTest(groupOption, providers, opts...)
|
2019-12-08 12:17:24 +08:00
|
|
|
case "select":
|
2020-11-13 21:48:52 +08:00
|
|
|
group = NewSelector(groupOption, providers)
|
2019-12-08 12:17:24 +08:00
|
|
|
case "fallback":
|
2020-11-13 21:48:52 +08:00
|
|
|
group = NewFallback(groupOption, providers)
|
2019-12-08 12:17:24 +08:00
|
|
|
case "load-balance":
|
2020-10-28 22:35:02 +08:00
|
|
|
strategy := parseStrategy(config)
|
2020-11-13 21:48:52 +08:00
|
|
|
return NewLoadBalance(groupOption, providers, strategy)
|
2020-03-21 23:46:49 +08:00
|
|
|
case "relay":
|
2020-11-13 21:48:52 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
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
|
|
|
|
}
|
2023-06-04 11:51:30 +08: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)
|
|
|
|
}
|
|
|
|
}
|