From 23cfe20eab5bee2e6e8683ca6f3a17834730fbb8 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 3 Jun 2022 13:31:56 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=80=89=E6=8B=A9fallback=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=BD=93=E8=8A=82=E7=82=B9=E4=B8=8D=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E6=97=B6=E8=A7=A6=E5=8F=91urltest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/outboundgroup/fallback.go | 23 +++++++++++++++++++---- adapter/outboundgroup/groupbase.go | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/adapter/outboundgroup/fallback.go b/adapter/outboundgroup/fallback.go index cd09b747..ef237c64 100644 --- a/adapter/outboundgroup/fallback.go +++ b/adapter/outboundgroup/fallback.go @@ -8,11 +8,13 @@ import ( "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/constant/provider" + "time" ) type Fallback struct { *GroupBase disableUDP bool + testUrl string selected string } @@ -90,15 +92,27 @@ func (f *Fallback) findAliveProxy(touch bool) C.Proxy { 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) { if proxy.Name() == name { - f.selected = name - return nil + p = proxy + break } } - return errors.New("proxy not exist") + if p == nil { + 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 { @@ -114,5 +128,6 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) providers, }), disableUDP: option.DisableUDP, + testUrl: option.URL, } } diff --git a/adapter/outboundgroup/groupbase.go b/adapter/outboundgroup/groupbase.go index 4aa290c0..707e1a6a 100644 --- a/adapter/outboundgroup/groupbase.go +++ b/adapter/outboundgroup/groupbase.go @@ -146,14 +146,14 @@ func (gb *GroupBase) onDialFailed() { gb.failedTimes++ if gb.failedTimes == 1 { - log.Warnln("ProxyGroup: %s first failed", gb.Name()) + log.Debugln("ProxyGroup: %s first failed", gb.Name()) gb.failedTime = time.Now() } else { if time.Since(gb.failedTime) > gb.failedTimeoutInterval() { 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() { gb.failedTesting.Store(true) log.Warnln("because %s failed multiple times, active health check", gb.Name())