mihomo/adapter/outboundgroup/urltest.go

156 lines
3.5 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outboundgroup
import (
"context"
"encoding/json"
"time"
2019-12-08 12:17:24 +08:00
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/common/singledo"
"github.com/Dreamacro/clash/component/dialer"
2019-12-08 12:17:24 +08:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
2019-12-08 12:17:24 +08:00
)
2020-05-29 17:47:50 +08:00
type urlTestOption func(*URLTest)
func urlTestWithTolerance(tolerance uint16) urlTestOption {
return func(u *URLTest) {
u.tolerance = tolerance
}
}
2019-12-08 12:17:24 +08:00
type URLTest struct {
*outbound.Base
2020-05-29 17:47:50 +08:00
tolerance uint16
disableUDP bool
2020-05-29 17:47:50 +08:00
fastNode C.Proxy
single *singledo.Single
fastSingle *singledo.Single
providers []provider.ProxyProvider
2019-12-08 12:17:24 +08:00
}
func (u *URLTest) Now() string {
return u.fast(false).Name()
2019-12-08 12:17:24 +08:00
}
2021-04-29 11:23:14 +08: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)
2019-12-08 12:17:24 +08:00
}
return c, err
2019-12-08 12:17:24 +08: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 12:17:24 +08:00
if err == nil {
pc.AppendToChains(u)
}
2020-01-31 14:43:54 +08:00
return pc, err
2019-12-08 12:17:24 +08:00
}
2021-04-29 11:23:14 +08:00
// Unwrap implements C.ProxyAdapter
func (u *URLTest) Unwrap(metadata *C.Metadata) C.Proxy {
return u.fast(true)
}
func (u *URLTest) proxies(touch bool) []C.Proxy {
elm, _, _ := u.single.Do(func() (interface{}, error) {
return getProvidersProxies(u.providers, touch), nil
})
return elm.([]C.Proxy)
}
func (u *URLTest) fast(touch bool) C.Proxy {
elm, _, _ := u.fastSingle.Do(func() (interface{}, 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 17:47:50 +08:00
2020-08-06 20:12:03 +08:00
// tolerance
if u.fastNode == nil || fastNotExist || !u.fastNode.Alive() || u.fastNode.LastDelay() > fast.LastDelay()+u.tolerance {
2020-08-06 20:12:03 +08:00
u.fastNode = fast
}
return u.fastNode, nil
})
return elm.(C.Proxy)
2019-12-08 12:17:24 +08:00
}
2021-04-29 11:23:14 +08:00
// SupportUDP implements C.ProxyAdapter
2019-12-08 12:17:24 +08:00
func (u *URLTest) SupportUDP() bool {
if u.disableUDP {
return false
}
return u.fast(false).SupportUDP()
2019-12-08 12:17:24 +08:00
}
2021-04-29 11:23:14 +08:00
// MarshalJSON implements C.ProxyAdapter
2019-12-08 12:17:24 +08:00
func (u *URLTest) MarshalJSON() ([]byte, error) {
var all []string
for _, proxy := range u.proxies(false) {
2019-12-08 12:17:24 +08:00
all = append(all, proxy.Name())
}
return json.Marshal(map[string]interface{}{
"type": u.Type().String(),
"now": u.Now(),
"all": all,
})
}
2020-05-29 17:47:50 +08:00
func parseURLTestOption(config map[string]interface{}) []urlTestOption {
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 17:47:50 +08:00
urlTest := &URLTest{
Base: outbound.NewBase(outbound.BaseOption{
Name: option.Name,
Type: C.URLTest,
Interface: option.Interface,
}),
single: singledo.NewSingle(defaultGetProxiesDuration),
fastSingle: singledo.NewSingle(time.Second * 10),
providers: providers,
disableUDP: option.DisableUDP,
2019-12-08 12:17:24 +08:00
}
2020-05-29 17:47:50 +08:00
for _, option := range options {
option(urlTest)
}
return urlTest
2019-12-08 12:17:24 +08:00
}