2022-04-28 19:01:13 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
2022-05-30 21:55:09 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-04-28 19:01:13 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
|
|
|
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"
|
|
|
|
"github.com/dlclark/regexp2"
|
2022-05-02 13:50:10 +08:00
|
|
|
"go.uber.org/atomic"
|
2022-04-28 23:43:10 +08:00
|
|
|
"sync"
|
2022-05-02 13:50:10 +08:00
|
|
|
"time"
|
2022-04-28 19:01:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type GroupBase struct {
|
|
|
|
*outbound.Base
|
2022-05-17 21:15:14 +08:00
|
|
|
filter *regexp2.Regexp
|
|
|
|
providers []provider.ProxyProvider
|
|
|
|
versions sync.Map // map[string]uint
|
|
|
|
proxies sync.Map // map[string][]C.Proxy
|
|
|
|
failedTestMux sync.Mutex
|
|
|
|
failedTimes int
|
|
|
|
failedTime time.Time
|
|
|
|
failedTesting *atomic.Bool
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type GroupBaseOption struct {
|
|
|
|
outbound.BaseOption
|
|
|
|
filter string
|
|
|
|
providers []provider.ProxyProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
|
|
|
var filter *regexp2.Regexp = nil
|
|
|
|
if opt.filter != "" {
|
|
|
|
filter = regexp2.MustCompile(opt.filter, 0)
|
|
|
|
}
|
|
|
|
return &GroupBase{
|
2022-05-17 21:15:14 +08:00
|
|
|
Base: outbound.NewBase(opt.BaseOption),
|
|
|
|
filter: filter,
|
|
|
|
providers: opt.providers,
|
|
|
|
failedTesting: atomic.NewBool(false),
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|
|
|
if gb.filter == nil {
|
|
|
|
var proxies []C.Proxy
|
|
|
|
for _, pd := range gb.providers {
|
|
|
|
if touch {
|
|
|
|
proxies = append(proxies, pd.ProxiesWithTouch()...)
|
|
|
|
} else {
|
|
|
|
proxies = append(proxies, pd.Proxies()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
|
|
|
}
|
|
|
|
return proxies
|
|
|
|
}
|
2022-05-12 18:57:30 +08:00
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, pd := range gb.providers {
|
2022-04-28 23:06:35 +08:00
|
|
|
if pd.VehicleType() == types.Compatible {
|
2022-04-28 19:01:13 +08:00
|
|
|
if touch {
|
2022-04-28 23:43:10 +08:00
|
|
|
gb.proxies.Store(pd.Name(), pd.ProxiesWithTouch())
|
2022-04-28 19:01:13 +08:00
|
|
|
} else {
|
2022-04-28 23:43:10 +08:00
|
|
|
gb.proxies.Store(pd.Name(), pd.Proxies())
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 23:43:10 +08:00
|
|
|
gb.versions.Store(pd.Name(), pd.Version())
|
2022-04-28 19:01:13 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:43:10 +08:00
|
|
|
if version, ok := gb.versions.Load(pd.Name()); !ok || version != pd.Version() {
|
2022-04-28 19:01:13 +08:00
|
|
|
var (
|
|
|
|
proxies []C.Proxy
|
|
|
|
newProxies []C.Proxy
|
|
|
|
)
|
|
|
|
|
|
|
|
if touch {
|
|
|
|
proxies = pd.ProxiesWithTouch()
|
|
|
|
} else {
|
|
|
|
proxies = pd.Proxies()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range proxies {
|
|
|
|
if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil {
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:43:10 +08:00
|
|
|
gb.proxies.Store(pd.Name(), newProxies)
|
|
|
|
gb.versions.Store(pd.Name(), pd.Version())
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var proxies []C.Proxy
|
2022-04-28 23:43:10 +08:00
|
|
|
gb.proxies.Range(func(key, value any) bool {
|
|
|
|
proxies = append(proxies, value.([]C.Proxy)...)
|
|
|
|
return true
|
|
|
|
})
|
2022-04-28 19:01:13 +08:00
|
|
|
if len(proxies) == 0 {
|
|
|
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
lock.Lock()
|
|
|
|
if err == nil {
|
|
|
|
mp[proxy.Name()] = delay
|
|
|
|
}
|
|
|
|
lock.Unlock()
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if len(mp) == 0 {
|
|
|
|
return mp, fmt.Errorf("get delay: all proxies timeout")
|
|
|
|
} else {
|
|
|
|
return mp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:50:10 +08:00
|
|
|
func (gb *GroupBase) onDialFailed() {
|
2022-05-17 21:15:14 +08:00
|
|
|
if gb.failedTesting.Load() {
|
|
|
|
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() {
|
|
|
|
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() {
|
|
|
|
gb.failedTesting.Store(true)
|
2022-05-02 13:50:10 +08:00
|
|
|
log.Warnln("because %s failed multiple times, active health check", gb.Name())
|
2022-05-17 21:15:14 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-02 13:50:10 +08:00
|
|
|
for _, proxyProvider := range gb.providers {
|
2022-05-17 21:15:14 +08:00
|
|
|
wg.Add(1)
|
|
|
|
proxyProvider := proxyProvider
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
proxyProvider.HealthCheck()
|
|
|
|
}()
|
2022-05-02 13:50:10 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 21:15:14 +08:00
|
|
|
wg.Wait()
|
|
|
|
gb.failedTesting.Store(false)
|
|
|
|
gb.failedTimes = 0
|
2022-05-02 13:50:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 21:15:14 +08:00
|
|
|
}()
|
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
|
|
|
|
}
|