2022-04-28 19:01:13 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
2022-05-30 21:55:09 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-01-07 12:24:28 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2023-04-22 15:37:57 +08:00
|
|
|
"github.com/Dreamacro/clash/common/atomic"
|
2022-04-28 19:01:13 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
2022-05-02 13:50:10 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-04-28 19:01:13 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2023-01-07 12:24:28 +08:00
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
"github.com/dlclark/regexp2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GroupBase struct {
|
|
|
|
*outbound.Base
|
2022-11-09 08:06:37 +08:00
|
|
|
filterRegs []*regexp2.Regexp
|
|
|
|
excludeFilterReg *regexp2.Regexp
|
2023-01-07 12:24:28 +08:00
|
|
|
excludeTypeArray []string
|
2022-11-09 08:06:37 +08:00
|
|
|
providers []provider.ProxyProvider
|
|
|
|
failedTestMux sync.Mutex
|
|
|
|
failedTimes int
|
|
|
|
failedTime time.Time
|
|
|
|
failedTesting *atomic.Bool
|
|
|
|
proxies [][]C.Proxy
|
|
|
|
versions []atomic.Uint32
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type GroupBaseOption struct {
|
|
|
|
outbound.BaseOption
|
2022-11-09 08:06:37 +08:00
|
|
|
filter string
|
|
|
|
excludeFilter string
|
2023-01-07 12:24:28 +08:00
|
|
|
excludeType string
|
2022-11-09 08:06:37 +08:00
|
|
|
providers []provider.ProxyProvider
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
2022-11-09 08:06:37 +08:00
|
|
|
var excludeFilterReg *regexp2.Regexp
|
|
|
|
if opt.excludeFilter != "" {
|
|
|
|
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0)
|
|
|
|
}
|
2023-01-07 12:24:28 +08:00
|
|
|
var excludeTypeArray []string
|
|
|
|
if opt.excludeType != "" {
|
|
|
|
excludeTypeArray = strings.Split(opt.excludeType, "|")
|
|
|
|
}
|
2022-11-09 08:06:37 +08:00
|
|
|
|
2022-10-30 22:30:54 +08:00
|
|
|
var filterRegs []*regexp2.Regexp
|
2022-04-28 19:01:13 +08:00
|
|
|
if opt.filter != "" {
|
2022-10-30 22:30:54 +08:00
|
|
|
for _, filter := range strings.Split(opt.filter, "`") {
|
|
|
|
filterReg := regexp2.MustCompile(filter, 0)
|
|
|
|
filterRegs = append(filterRegs, filterReg)
|
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2022-07-20 08:53:54 +08:00
|
|
|
|
|
|
|
gb := &GroupBase{
|
2022-11-09 08:06:37 +08:00
|
|
|
Base: outbound.NewBase(opt.BaseOption),
|
|
|
|
filterRegs: filterRegs,
|
|
|
|
excludeFilterReg: excludeFilterReg,
|
2023-01-07 12:24:28 +08:00
|
|
|
excludeTypeArray: excludeTypeArray,
|
2022-11-09 08:06:37 +08:00
|
|
|
providers: opt.providers,
|
|
|
|
failedTesting: atomic.NewBool(false),
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2022-07-20 08:53:54 +08:00
|
|
|
|
|
|
|
gb.proxies = make([][]C.Proxy, len(opt.providers))
|
|
|
|
gb.versions = make([]atomic.Uint32, len(opt.providers))
|
|
|
|
|
|
|
|
return gb
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
2022-10-31 16:45:14 +08:00
|
|
|
func (gb *GroupBase) Touch() {
|
|
|
|
for _, pd := range gb.providers {
|
|
|
|
pd.Touch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
2022-11-09 08:41:30 +08:00
|
|
|
var proxies []C.Proxy
|
2022-10-30 22:30:54 +08:00
|
|
|
if len(gb.filterRegs) == 0 {
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, pd := range gb.providers {
|
|
|
|
if touch {
|
2022-06-07 17:19:25 +08:00
|
|
|
pd.Touch()
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2022-06-07 17:19:25 +08:00
|
|
|
proxies = append(proxies, pd.Proxies()...)
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2022-11-09 08:41:30 +08:00
|
|
|
} else {
|
|
|
|
for i, pd := range gb.providers {
|
|
|
|
if touch {
|
|
|
|
pd.Touch()
|
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
|
2022-11-09 08:41:30 +08:00
|
|
|
if pd.VehicleType() == types.Compatible {
|
|
|
|
gb.versions[i].Store(pd.Version())
|
|
|
|
gb.proxies[i] = pd.Proxies()
|
|
|
|
continue
|
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
|
2022-11-09 08:41:30 +08:00
|
|
|
version := gb.versions[i].Load()
|
|
|
|
if version != pd.Version() && gb.versions[i].CompareAndSwap(version, pd.Version()) {
|
|
|
|
var (
|
|
|
|
proxies []C.Proxy
|
|
|
|
newProxies []C.Proxy
|
|
|
|
)
|
|
|
|
|
|
|
|
proxies = pd.Proxies()
|
|
|
|
proxiesSet := map[string]struct{}{}
|
|
|
|
for _, filterReg := range gb.filterRegs {
|
|
|
|
for _, p := range proxies {
|
|
|
|
name := p.Name()
|
|
|
|
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
|
|
|
|
if _, ok := proxiesSet[name]; !ok {
|
|
|
|
proxiesSet[name] = struct{}{}
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
2022-10-30 22:30:54 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
2022-11-09 08:41:30 +08:00
|
|
|
gb.proxies[i] = newProxies
|
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2022-07-20 08:53:54 +08:00
|
|
|
|
2022-11-09 08:41:30 +08:00
|
|
|
for _, p := range gb.proxies {
|
|
|
|
proxies = append(proxies, p...)
|
|
|
|
}
|
2022-07-20 08:53:54 +08:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:30:54 +08:00
|
|
|
if len(gb.providers) > 1 && len(gb.filterRegs) > 1 {
|
|
|
|
var newProxies []C.Proxy
|
|
|
|
proxiesSet := map[string]struct{}{}
|
|
|
|
for _, filterReg := range gb.filterRegs {
|
|
|
|
for _, p := range proxies {
|
|
|
|
name := p.Name()
|
|
|
|
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
|
|
|
|
if _, ok := proxiesSet[name]; !ok {
|
|
|
|
proxiesSet[name] = struct{}{}
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 21:50:30 +08:00
|
|
|
for _, p := range proxies { // add not matched proxies at the end
|
|
|
|
name := p.Name()
|
|
|
|
if _, ok := proxiesSet[name]; !ok {
|
|
|
|
proxiesSet[name] = struct{}{}
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 22:30:54 +08:00
|
|
|
proxies = newProxies
|
|
|
|
}
|
2023-01-07 12:24:28 +08:00
|
|
|
if gb.excludeTypeArray != nil {
|
|
|
|
var newProxies []C.Proxy
|
|
|
|
for _, p := range proxies {
|
|
|
|
mType := p.Type().String()
|
|
|
|
flag := false
|
|
|
|
for i := range gb.excludeTypeArray {
|
|
|
|
if strings.EqualFold(mType, gb.excludeTypeArray[i]) {
|
|
|
|
flag = true
|
2023-01-24 16:34:52 +08:00
|
|
|
break
|
2023-01-07 12:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if flag {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
proxies = newProxies
|
|
|
|
}
|
2022-10-30 22:30:54 +08:00
|
|
|
|
2022-11-09 08:06:37 +08:00
|
|
|
if gb.excludeFilterReg != nil {
|
|
|
|
var newProxies []C.Proxy
|
|
|
|
for _, p := range proxies {
|
|
|
|
name := p.Name()
|
|
|
|
if mat, _ := gb.excludeFilterReg.FindStringMatch(name); mat != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
proxies = newProxies
|
|
|
|
}
|
|
|
|
|
2023-05-19 19:57:55 +08:00
|
|
|
if len(proxies) == 0 {
|
|
|
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
return proxies
|
|
|
|
}
|
2022-05-02 13:50:10 +08:00
|
|
|
|
2022-05-30 21:55:09 +08:00
|
|
|
func (gb *GroupBase) URLTest(ctx context.Context, url string) (map[string]uint16, error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var lock sync.Mutex
|
|
|
|
mp := map[string]uint16{}
|
|
|
|
proxies := gb.GetProxies(false)
|
|
|
|
for _, proxy := range proxies {
|
|
|
|
proxy := proxy
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
delay, err := proxy.URLTest(ctx, url)
|
|
|
|
if err == nil {
|
2022-06-25 08:53:04 +08:00
|
|
|
lock.Lock()
|
2022-05-30 21:55:09 +08:00
|
|
|
mp[proxy.Name()] = delay
|
2022-06-25 08:53:04 +08:00
|
|
|
lock.Unlock()
|
2022-05-30 21:55:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if len(mp) == 0 {
|
|
|
|
return mp, fmt.Errorf("get delay: all proxies timeout")
|
|
|
|
} else {
|
|
|
|
return mp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:12:49 +08:00
|
|
|
func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
|
|
|
|
if adapterType == C.Direct || adapterType == C.Compatible || adapterType == C.Reject || adapterType == C.Pass {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "connection refused") {
|
|
|
|
go gb.healthCheck()
|
2022-05-17 21:15:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
gb.failedTestMux.Lock()
|
2022-05-18 12:00:57 +08:00
|
|
|
defer gb.failedTestMux.Unlock()
|
2022-05-17 21:15:14 +08:00
|
|
|
|
|
|
|
gb.failedTimes++
|
|
|
|
if gb.failedTimes == 1 {
|
2022-06-03 13:31:56 +08:00
|
|
|
log.Debugln("ProxyGroup: %s first failed", gb.Name())
|
2022-05-17 21:15:14 +08:00
|
|
|
gb.failedTime = time.Now()
|
2022-05-02 13:50:10 +08:00
|
|
|
} else {
|
2022-05-17 21:15:14 +08:00
|
|
|
if time.Since(gb.failedTime) > gb.failedTimeoutInterval() {
|
2022-08-13 17:23:42 +08:00
|
|
|
gb.failedTimes = 0
|
2022-05-17 21:15:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:31:56 +08:00
|
|
|
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
|
2022-05-17 21:15:14 +08:00
|
|
|
if gb.failedTimes >= gb.maxFailedTimes() {
|
2022-05-02 13:50:10 +08:00
|
|
|
log.Warnln("because %s failed multiple times, active health check", gb.Name())
|
2022-10-16 13:12:49 +08:00
|
|
|
gb.healthCheck()
|
2022-05-02 13:50:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 21:15:14 +08:00
|
|
|
}()
|
2022-05-02 13:50:10 +08:00
|
|
|
}
|
|
|
|
|
2022-10-16 13:12:49 +08:00
|
|
|
func (gb *GroupBase) healthCheck() {
|
|
|
|
if gb.failedTesting.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gb.failedTesting.Store(true)
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for _, proxyProvider := range gb.providers {
|
|
|
|
wg.Add(1)
|
|
|
|
proxyProvider := proxyProvider
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
proxyProvider.HealthCheck()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
gb.failedTesting.Store(false)
|
|
|
|
gb.failedTimes = 0
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:50:10 +08:00
|
|
|
func (gb *GroupBase) failedIntervalTime() int64 {
|
|
|
|
return 5 * time.Second.Milliseconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gb *GroupBase) onDialSuccess() {
|
2022-05-17 21:15:14 +08:00
|
|
|
if !gb.failedTesting.Load() {
|
|
|
|
gb.failedTimes = 0
|
|
|
|
}
|
2022-05-02 13:50:10 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 21:15:14 +08:00
|
|
|
func (gb *GroupBase) maxFailedTimes() int {
|
2022-05-02 13:50:10 +08:00
|
|
|
return 5
|
|
|
|
}
|
2022-05-17 21:15:14 +08:00
|
|
|
|
|
|
|
func (gb *GroupBase) failedTimeoutInterval() time.Duration {
|
|
|
|
return 5 * time.Second
|
|
|
|
}
|