clash/adapter/outboundgroup/fallback.go

152 lines
3.7 KiB
Go
Raw Normal View History

2019-12-08 04:17:24 +00:00
package outboundgroup
import (
"context"
"encoding/json"
2021-12-03 06:35:21 +00:00
"github.com/Dreamacro/clash/log"
"go.uber.org/atomic"
"time"
2019-12-08 04:17:24 +00:00
2021-06-10 06:05:56 +00:00
"github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/common/singledo"
"github.com/Dreamacro/clash/component/dialer"
2019-12-08 04:17:24 +00:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
2019-12-08 04:17:24 +00:00
)
type Fallback struct {
*outbound.Base
2021-12-03 06:35:21 +00:00
disableUDP bool
2022-01-05 04:19:49 +00:00
filter string
2022-04-23 18:07:57 +00:00
single *singledo.Single[[]C.Proxy]
2021-12-03 06:35:21 +00:00
providers []provider.ProxyProvider
failedTimes *atomic.Int32
failedTime *atomic.Int64
2019-12-08 04:17:24 +00:00
}
func (f *Fallback) Now() string {
proxy := f.findAliveProxy(false)
2019-12-08 04:17:24 +00:00
return proxy.Name()
}
2021-04-29 03:23:14 +00:00
// DialContext implements C.ProxyAdapter
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
proxy := f.findAliveProxy(true)
c, err := proxy.DialContext(ctx, metadata, f.Base.DialOptions(opts...)...)
2019-12-08 04:17:24 +00:00
if err == nil {
c.AppendToChains(f)
f.failedTimes.Store(-1)
f.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
} else {
f.onDialFailed()
2019-12-08 04:17:24 +00:00
}
2019-12-08 04:17:24 +00:00
return c, err
}
// ListenPacketContext implements C.ProxyAdapter
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
proxy := f.findAliveProxy(true)
pc, err := proxy.ListenPacketContext(ctx, metadata, f.Base.DialOptions(opts...)...)
2019-12-08 04:17:24 +00:00
if err == nil {
pc.AppendToChains(f)
f.failedTimes.Store(-1)
f.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
} else {
f.onDialFailed()
2019-12-08 04:17:24 +00:00
}
2020-01-31 06:43:54 +00:00
return pc, err
2019-12-08 04:17:24 +00:00
}
2021-12-03 06:35:21 +00:00
func (f *Fallback) onDialFailed() {
if f.failedTime.Load() == -1 {
log.Warnln("%s first failed", f.Name())
now := time.Now().UnixMilli()
f.failedTime.Store(now)
f.failedTimes.Store(1)
} else {
2022-01-08 01:23:49 +00:00
if f.failedTime.Load()-time.Now().UnixMilli() > 5*time.Second.Milliseconds() {
2021-12-03 06:35:21 +00:00
f.failedTimes.Store(-1)
f.failedTime.Store(-1)
} else {
failedCount := f.failedTimes.Inc()
2021-12-03 06:35:21 +00:00
log.Warnln("%s failed count: %d", f.Name(), failedCount)
if failedCount >= 5 {
log.Warnln("because %s failed multiple times, active health check", f.Name())
2021-12-03 06:35:21 +00:00
for _, proxyProvider := range f.providers {
go proxyProvider.HealthCheck()
}
2022-01-08 01:23:49 +00:00
f.failedTimes.Store(-1)
f.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
}
}
}
}
2021-04-29 03:23:14 +00:00
// SupportUDP implements C.ProxyAdapter
2019-12-08 04:17:24 +00:00
func (f *Fallback) SupportUDP() bool {
if f.disableUDP {
return false
}
proxy := f.findAliveProxy(false)
2019-12-08 04:17:24 +00:00
return proxy.SupportUDP()
}
2021-04-29 03:23:14 +00:00
// MarshalJSON implements C.ProxyAdapter
2019-12-08 04:17:24 +00:00
func (f *Fallback) MarshalJSON() ([]byte, error) {
2022-03-23 05:48:21 +00:00
all := []string{}
for _, proxy := range f.proxies(false) {
2019-12-08 04:17:24 +00:00
all = append(all, proxy.Name())
}
2022-03-16 04:10:13 +00:00
return json.Marshal(map[string]any{
2019-12-08 04:17:24 +00:00
"type": f.Type().String(),
"now": f.Now(),
"all": all,
})
}
2021-04-29 03:23:14 +00:00
// Unwrap implements C.ProxyAdapter
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
proxy := f.findAliveProxy(true)
return proxy
}
func (f *Fallback) proxies(touch bool) []C.Proxy {
2022-04-23 18:07:57 +00:00
elm, _, _ := f.single.Do(func() ([]C.Proxy, error) {
2022-03-16 17:51:28 +00:00
return getProvidersProxies(f.providers, touch, f.filter), nil
})
2022-04-23 18:07:57 +00:00
return elm
2019-12-08 04:17:24 +00:00
}
func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
proxies := f.proxies(touch)
for _, proxy := range proxies {
if proxy.Alive() {
return proxy
2019-12-08 04:17:24 +00:00
}
}
return proxies[0]
2019-12-08 04:17:24 +00:00
}
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
2019-12-08 04:17:24 +00:00
return &Fallback{
Base: outbound.NewBase(outbound.BaseOption{
2021-11-08 08:59:48 +00:00
Name: option.Name,
Type: C.Fallback,
Interface: option.Interface,
RoutingMark: option.RoutingMark,
}),
2022-04-23 18:07:57 +00:00
single: singledo.NewSingle[[]C.Proxy](defaultGetProxiesDuration),
providers: providers,
disableUDP: option.DisableUDP,
2022-01-05 04:19:49 +00:00
filter: option.Filter,
failedTimes: atomic.NewInt32(-1),
failedTime: atomic.NewInt64(-1),
2019-12-08 04:17:24 +00:00
}
}