2019-12-08 04:17:24 +00:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-04-07 18:04:16 +00:00
|
|
|
"errors"
|
2019-12-10 07:04:22 +00:00
|
|
|
"time"
|
2019-12-08 04:17:24 +00:00
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2023-02-24 01:54:54 +00:00
|
|
|
"github.com/Dreamacro/clash/common/callback"
|
2023-02-26 16:26:49 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2019-12-10 07:04:22 +00:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
2021-11-07 08:48:51 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2019-12-08 04:17:24 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 12:32:59 +00:00
|
|
|
"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 {
|
2022-04-28 11:01:13 +00:00
|
|
|
*GroupBase
|
2023-04-07 18:04:16 +00:00
|
|
|
selected string
|
|
|
|
testUrl string
|
2022-05-02 05:50:10 +00:00
|
|
|
tolerance uint16
|
|
|
|
disableUDP bool
|
|
|
|
fastNode C.Proxy
|
|
|
|
fastSingle *singledo.Single[C.Proxy]
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) Now() string {
|
2020-11-18 16:53:22 +00:00
|
|
|
return u.fast(false).Name()
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 18:04:16 +00:00
|
|
|
func (u *URLTest) Set(name string) error {
|
|
|
|
var p C.Proxy
|
|
|
|
for _, proxy := range u.GetProxies(false) {
|
|
|
|
if proxy.Name() == name {
|
|
|
|
p = proxy
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return errors.New("proxy not exist")
|
|
|
|
}
|
|
|
|
u.selected = name
|
|
|
|
u.fast(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
2022-10-16 05:12:49 +00:00
|
|
|
proxy := u.fast(true)
|
|
|
|
c, err = proxy.DialContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
2019-12-10 07:04:22 +00:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(u)
|
2021-12-03 06:35:21 +00:00
|
|
|
} else {
|
2022-10-16 05:12:49 +00:00
|
|
|
u.onDialFailed(proxy.Type(), err)
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
2023-02-24 01:54:54 +00:00
|
|
|
|
2023-02-26 16:26:49 +00:00
|
|
|
if N.NeedHandshake(c) {
|
2023-04-01 12:56:49 +00:00
|
|
|
c = callback.NewFirstWriteCallBackConn(c, func(err error) {
|
|
|
|
if err == nil {
|
|
|
|
u.onDialSuccess()
|
|
|
|
} else {
|
|
|
|
u.onDialFailed(proxy.Type(), err)
|
|
|
|
}
|
|
|
|
})
|
2023-02-24 01:54:54 +00:00
|
|
|
}
|
2023-02-26 16:26:49 +00:00
|
|
|
|
2019-12-10 07:04:22 +00:00
|
|
|
return c, err
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-05-02 05:50:10 +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-10-30 15:08:18 +00:00
|
|
|
func (u *URLTest) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|
|
|
return u.fast(touch)
|
2020-05-07 13:42:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
func (u *URLTest) fast(touch bool) C.Proxy {
|
2022-10-31 08:58:29 +00:00
|
|
|
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
2023-04-07 18:04:16 +00:00
|
|
|
var s C.Proxy
|
2022-04-28 11:01:13 +00:00
|
|
|
proxies := u.GetProxies(touch)
|
2019-12-10 07:04:22 +00:00
|
|
|
fast := proxies[0]
|
2023-04-07 18:04:16 +00:00
|
|
|
if fast.Name() == u.selected {
|
|
|
|
s = fast
|
|
|
|
}
|
2019-12-10 07:04:22 +00:00
|
|
|
min := fast.LastDelay()
|
2021-05-01 09:21:09 +00:00
|
|
|
fastNotExist := true
|
|
|
|
|
2019-12-10 07:04:22 +00:00
|
|
|
for _, proxy := range proxies[1:] {
|
2023-04-07 18:04:16 +00:00
|
|
|
|
2021-05-01 09:21:09 +00:00
|
|
|
if u.fastNode != nil && proxy.Name() == u.fastNode.Name() {
|
|
|
|
fastNotExist = false
|
|
|
|
}
|
|
|
|
|
2023-04-07 18:04:16 +00:00
|
|
|
if proxy.Name() == u.selected {
|
|
|
|
s = proxy
|
|
|
|
}
|
2019-12-10 07:04:22 +00:00
|
|
|
if !proxy.Alive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := proxy.LastDelay()
|
|
|
|
if delay < min {
|
|
|
|
fast = proxy
|
|
|
|
min = delay
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 12:12:03 +00:00
|
|
|
// tolerance
|
2021-05-01 09:21:09 +00:00
|
|
|
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
|
|
|
|
}
|
2023-04-07 18:04:16 +00:00
|
|
|
if s != nil {
|
|
|
|
if s.Alive() && s.LastDelay() < fast.LastDelay()+u.tolerance {
|
|
|
|
u.fastNode = s
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 12:12:03 +00:00
|
|
|
return u.fastNode, nil
|
2019-12-10 07:04:22 +00:00
|
|
|
})
|
2022-10-31 08:58:29 +00:00
|
|
|
if shared && touch { // a shared fastSingle.Do() may cause providers untouched, so we touch them again
|
|
|
|
u.Touch()
|
|
|
|
}
|
2019-12-10 07:04:22 +00:00
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
return elm
|
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 {
|
2020-11-13 13:48:52 +00:00
|
|
|
if u.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-18 16:53:22 +00:00
|
|
|
return u.fast(false).SupportUDP()
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 06:10:57 +00:00
|
|
|
// IsL3Protocol implements C.ProxyAdapter
|
|
|
|
func (u *URLTest) IsL3Protocol(metadata *C.Metadata) bool {
|
|
|
|
return u.fast(false).IsL3Protocol(metadata)
|
|
|
|
}
|
|
|
|
|
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{}
|
2022-04-28 11:01:13 +00:00
|
|
|
for _, proxy := range u.GetProxies(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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
2020-05-29 09:47:50 +00:00
|
|
|
urlTest := &URLTest{
|
2022-04-28 11:01:13 +00:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.URLTest,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
2022-05-02 05:50:10 +00:00
|
|
|
|
2022-04-28 11:01:13 +00:00
|
|
|
option.Filter,
|
2022-11-09 00:06:37 +00:00
|
|
|
option.ExcludeFilter,
|
2023-01-07 04:24:28 +00:00
|
|
|
option.ExcludeType,
|
2022-04-28 11:01:13 +00:00
|
|
|
providers,
|
2021-11-07 08:48:51 +00:00
|
|
|
}),
|
2022-05-02 05:50:10 +00:00
|
|
|
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
|
|
|
disableUDP: option.DisableUDP,
|
2023-04-07 18:04:16 +00:00
|
|
|
testUrl: option.URL,
|
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
|
|
|
}
|