chore: 选择fallback时,当节点不可用时触发urltest

This commit is contained in:
gVisor bot 2022-06-03 13:31:56 +08:00
parent b0e723f868
commit 23cfe20eab
2 changed files with 21 additions and 6 deletions

View file

@ -8,11 +8,13 @@ import (
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/constant/provider"
"time"
) )
type Fallback struct { type Fallback struct {
*GroupBase *GroupBase
disableUDP bool disableUDP bool
testUrl string
selected string selected string
} }
@ -90,17 +92,29 @@ func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
return al return al
} }
func (f *Fallback) Set(name string) error { func (f *Fallback) Set(name string) (err error) {
var p C.Proxy
for _, proxy := range f.GetProxies(false) { for _, proxy := range f.GetProxies(false) {
if proxy.Name() == name { if proxy.Name() == name {
f.selected = name p = proxy
return nil break
} }
} }
if p == nil {
return errors.New("proxy not exist") return errors.New("proxy not exist")
} }
f.selected = name
if !p.Alive() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cancel()
_, _ = p.URLTest(ctx, f.testUrl)
}
return nil
}
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback { func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
return &Fallback{ return &Fallback{
GroupBase: NewGroupBase(GroupBaseOption{ GroupBase: NewGroupBase(GroupBaseOption{
@ -114,5 +128,6 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider)
providers, providers,
}), }),
disableUDP: option.DisableUDP, disableUDP: option.DisableUDP,
testUrl: option.URL,
} }
} }

View file

@ -146,14 +146,14 @@ func (gb *GroupBase) onDialFailed() {
gb.failedTimes++ gb.failedTimes++
if gb.failedTimes == 1 { if gb.failedTimes == 1 {
log.Warnln("ProxyGroup: %s first failed", gb.Name()) log.Debugln("ProxyGroup: %s first failed", gb.Name())
gb.failedTime = time.Now() gb.failedTime = time.Now()
} else { } else {
if time.Since(gb.failedTime) > gb.failedTimeoutInterval() { if time.Since(gb.failedTime) > gb.failedTimeoutInterval() {
return return
} }
log.Warnln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes) log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
if gb.failedTimes >= gb.maxFailedTimes() { if gb.failedTimes >= gb.maxFailedTimes() {
gb.failedTesting.Store(true) gb.failedTesting.Store(true)
log.Warnln("because %s failed multiple times, active health check", gb.Name()) log.Warnln("because %s failed multiple times, active health check", gb.Name())