2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-12-10 15:04:22 +08:00
|
|
|
"time"
|
2019-12-08 12:17:24 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
2019-12-10 15:04:22 +08:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
2019-12-08 12:17:24 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
fastNode C.Proxy
|
2019-12-10 15:04:22 +08:00
|
|
|
single *singledo.Single
|
|
|
|
fastSingle *singledo.Single
|
|
|
|
providers []provider.ProxyProvider
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) Now() string {
|
2019-12-10 15:04:22 +08:00
|
|
|
return u.fast().Name()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
2019-12-10 15:04:22 +08:00
|
|
|
c, err = u.fast().DialContext(ctx, metadata)
|
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(u)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2019-12-10 15:04:22 +08:00
|
|
|
return c, err
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (u *URLTest) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
|
|
|
pc, err := u.fast().DialUDP(metadata)
|
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
|
|
|
}
|
|
|
|
|
2020-05-07 21:42:52 +08:00
|
|
|
func (u *URLTest) Unwrap(metadata *C.Metadata) C.Proxy {
|
|
|
|
return u.fast()
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (u *URLTest) proxies() []C.Proxy {
|
2019-12-10 15:04:22 +08:00
|
|
|
elm, _, _ := u.single.Do(func() (interface{}, error) {
|
|
|
|
return getProvidersProxies(u.providers), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) fast() C.Proxy {
|
|
|
|
elm, _, _ := u.fastSingle.Do(func() (interface{}, error) {
|
|
|
|
proxies := u.proxies()
|
|
|
|
fast := proxies[0]
|
|
|
|
min := fast.LastDelay()
|
|
|
|
for _, proxy := range proxies[1:] {
|
|
|
|
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
|
2020-08-30 19:53:00 +08:00
|
|
|
if u.fastNode == nil || u.fastNode.LastDelay() > fast.LastDelay()+u.tolerance {
|
2020-08-06 20:12:03 +08:00
|
|
|
u.fastNode = fast
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.fastNode, nil
|
2019-12-10 15:04:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
return elm.(C.Proxy)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) SupportUDP() bool {
|
2019-12-10 15:04:22 +08:00
|
|
|
return u.fast().SupportUDP()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
|
|
|
for _, proxy := range u.proxies() {
|
|
|
|
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(name string, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
|
|
|
urlTest := &URLTest{
|
2020-03-21 23:46:49 +08:00
|
|
|
Base: outbound.NewBase(name, "", C.URLTest, false),
|
2019-12-10 15:04:22 +08:00
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
|
|
fastSingle: singledo.NewSingle(time.Second * 10),
|
|
|
|
providers: providers,
|
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
|
|
|
}
|