clash/adapter/outboundgroup/urltest.go

199 lines
4.6 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
)
2020-05-29 09:47:50 +00:00
type urlTestOption func(*URLTest)
func urlTestWithTolerance(tolerance uint16) urlTestOption {
return func(u *URLTest) {
u.tolerance = tolerance
}
}
2019-12-08 04:17:24 +00:00
type URLTest struct {
*outbound.Base
2021-12-03 06:35:21 +00:00
tolerance uint16
disableUDP bool
fastNode C.Proxy
2022-01-05 04:19:49 +00:00
filter string
2021-12-03 06:35:21 +00:00
single *singledo.Single
fastSingle *singledo.Single
providers []provider.ProxyProvider
failedTimes *atomic.Int32
failedTime *atomic.Int64
2019-12-08 04:17:24 +00:00
}
func (u *URLTest) Now() string {
return u.fast(false).Name()
2019-12-08 04:17:24 +00:00
}
2021-04-29 03:23:14 +00:00
// DialContext implements C.ProxyAdapter
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
c, err = u.fast(true).DialContext(ctx, metadata, u.Base.DialOptions(opts...)...)
if err == nil {
c.AppendToChains(u)
u.failedTimes.Store(-1)
u.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
} else {
u.onDialFailed()
2019-12-08 04:17:24 +00:00
}
return c, err
2019-12-08 04:17:24 +00:00
}
// ListenPacketContext implements C.ProxyAdapter
func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
pc, err := u.fast(true).ListenPacketContext(ctx, metadata, u.Base.DialOptions(opts...)...)
2019-12-08 04:17:24 +00:00
if err == nil {
pc.AppendToChains(u)
u.failedTimes.Store(-1)
u.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
} else {
u.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-04-29 03:23:14 +00:00
// Unwrap implements C.ProxyAdapter
2022-03-16 17:51:28 +00:00
func (u *URLTest) Unwrap(*C.Metadata) C.Proxy {
return u.fast(true)
}
func (u *URLTest) proxies(touch bool) []C.Proxy {
2022-03-16 04:10:13 +00:00
elm, _, _ := u.single.Do(func() (any, error) {
2022-01-05 04:19:49 +00:00
return getProvidersProxies(u.providers, touch, u.filter), nil
})
return elm.([]C.Proxy)
}
func (u *URLTest) fast(touch bool) C.Proxy {
2022-03-16 04:10:13 +00:00
elm, _, _ := u.fastSingle.Do(func() (any, error) {
proxies := u.proxies(touch)
fast := proxies[0]
min := fast.LastDelay()
fastNotExist := true
for _, proxy := range proxies[1:] {
if u.fastNode != nil && proxy.Name() == u.fastNode.Name() {
fastNotExist = false
}
if !proxy.Alive() {
continue
}
delay := proxy.LastDelay()
if delay < min {
fast = proxy
min = delay
}
}
2020-05-29 09:47:50 +00:00
2020-08-06 12:12:03 +00:00
// tolerance
if u.fastNode == nil || fastNotExist || !u.fastNode.Alive() || u.fastNode.LastDelay() > fast.LastDelay()+u.tolerance {
2020-08-06 12:12:03 +00:00
u.fastNode = fast
}
return u.fastNode, nil
})
return elm.(C.Proxy)
2019-12-08 04:17:24 +00:00
}
2021-04-29 03:23:14 +00:00
// SupportUDP implements C.ProxyAdapter
2019-12-08 04:17:24 +00:00
func (u *URLTest) SupportUDP() bool {
if u.disableUDP {
return false
}
return u.fast(false).SupportUDP()
2019-12-08 04:17:24 +00:00
}
2021-04-29 03:23:14 +00:00
// MarshalJSON implements C.ProxyAdapter
2019-12-08 04:17:24 +00:00
func (u *URLTest) MarshalJSON() ([]byte, error) {
2022-03-23 05:48:21 +00:00
all := []string{}
for _, proxy := range u.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": u.Type().String(),
"now": u.Now(),
"all": all,
})
}
2021-12-03 06:35:21 +00:00
func (u *URLTest) onDialFailed() {
if u.failedTime.Load() == -1 {
log.Warnln("%s first failed", u.Name())
now := time.Now().UnixMilli()
u.failedTime.Store(now)
u.failedTimes.Store(1)
} else {
if u.failedTime.Load()-time.Now().UnixMilli() > 5*1000 {
u.failedTimes.Store(-1)
u.failedTime.Store(-1)
} else {
failedCount := u.failedTimes.Inc()
2021-12-03 06:35:21 +00:00
log.Warnln("%s failed count: %d", u.Name(), failedCount)
if failedCount >= 5 {
log.Warnln("because %s failed multiple times, active health check", u.Name())
2021-12-03 06:35:21 +00:00
for _, proxyProvider := range u.providers {
go proxyProvider.HealthCheck()
}
2022-01-08 01:23:49 +00:00
u.failedTimes.Store(-1)
u.failedTime.Store(-1)
2021-12-03 06:35:21 +00:00
}
}
}
}
2022-03-16 04:10:13 +00:00
func parseURLTestOption(config map[string]any) []urlTestOption {
2020-05-29 09:47:50 +00:00
opts := []urlTestOption{}
// tolerance
if elm, ok := config["tolerance"]; ok {
if tolerance, ok := elm.(int); ok {
opts = append(opts, urlTestWithTolerance(uint16(tolerance)))
}
}
return opts
}
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
2020-05-29 09:47:50 +00:00
urlTest := &URLTest{
Base: outbound.NewBase(outbound.BaseOption{
2021-11-08 08:59:48 +00:00
Name: option.Name,
Type: C.URLTest,
Interface: option.Interface,
RoutingMark: option.RoutingMark,
}),
2021-12-03 06:35:21 +00:00
single: singledo.NewSingle(defaultGetProxiesDuration),
fastSingle: singledo.NewSingle(time.Second * 10),
providers: providers,
disableUDP: option.DisableUDP,
2022-01-05 04:19:49 +00:00
filter: option.Filter,
2021-12-03 06:35:21 +00:00
failedTimes: atomic.NewInt32(-1),
failedTime: atomic.NewInt64(-1),
2019-12-08 04:17:24 +00:00
}
2020-05-29 09:47:50 +00:00
for _, option := range options {
option(urlTest)
}
return urlTest
2019-12-08 04:17:24 +00:00
}