2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-04-08 02:04:16 +08:00
|
|
|
"errors"
|
2023-12-27 09:02:02 +08:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2019-12-10 15:04:22 +08:00
|
|
|
"time"
|
2019-12-08 12:17:24 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/outbound"
|
|
|
|
"github.com/metacubex/mihomo/common/callback"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
"github.com/metacubex/mihomo/common/singledo"
|
2023-12-27 09:02:02 +08:00
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/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 {
|
2022-04-28 19:01:13 +08:00
|
|
|
*GroupBase
|
2023-06-04 11:51:30 +08:00
|
|
|
selected string
|
|
|
|
testUrl string
|
|
|
|
expectedStatus string
|
|
|
|
tolerance uint16
|
|
|
|
disableUDP bool
|
2024-01-07 23:32:22 +08:00
|
|
|
Hidden bool
|
|
|
|
Icon string
|
2023-06-04 11:51:30 +08:00
|
|
|
fastNode C.Proxy
|
|
|
|
fastSingle *singledo.Single[C.Proxy]
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) Now() string {
|
2020-11-19 00:53:22 +08:00
|
|
|
return u.fast(false).Name()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:04:16 +08: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
|
|
|
|
}
|
|
|
|
|
2023-04-24 08:07:17 +08:00
|
|
|
func (u *URLTest) ForceSet(name string) {
|
|
|
|
u.selected = name
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
2022-10-16 13:12:49 +08:00
|
|
|
proxy := u.fast(true)
|
|
|
|
c, err = proxy.DialContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
2019-12-10 15:04:22 +08:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(u)
|
2021-12-03 14:35:21 +08:00
|
|
|
} else {
|
2022-10-16 13:12:49 +08:00
|
|
|
u.onDialFailed(proxy.Type(), err)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2023-02-24 09:54:54 +08:00
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
if N.NeedHandshake(c) {
|
2023-04-01 20:56:49 +08:00
|
|
|
c = callback.NewFirstWriteCallBackConn(c, func(err error) {
|
|
|
|
if err == nil {
|
|
|
|
u.onDialSuccess()
|
|
|
|
} else {
|
|
|
|
u.onDialFailed(proxy.Type(), err)
|
|
|
|
}
|
|
|
|
})
|
2023-02-24 09:54:54 +08:00
|
|
|
}
|
2023-02-27 00:26:49 +08:00
|
|
|
|
2019-12-10 15:04:22 +08:00
|
|
|
return c, err
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08: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 12:17:24 +08:00
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(u)
|
|
|
|
}
|
2022-05-02 13:50:10 +08:00
|
|
|
|
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
|
2022-10-30 23:08:18 +08:00
|
|
|
func (u *URLTest) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|
|
|
return u.fast(touch)
|
2020-05-07 21:42:52 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (u *URLTest) fast(touch bool) C.Proxy {
|
2023-06-02 18:26:51 +08:00
|
|
|
|
|
|
|
proxies := u.GetProxies(touch)
|
|
|
|
if u.selected != "" {
|
|
|
|
for _, proxy := range proxies {
|
2023-12-22 21:18:17 +08:00
|
|
|
if !proxy.AliveForTestUrl(u.testUrl) {
|
2023-06-02 18:26:51 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if proxy.Name() == u.selected {
|
|
|
|
u.fastNode = proxy
|
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:58:29 +08:00
|
|
|
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
2019-12-10 15:04:22 +08:00
|
|
|
fast := proxies[0]
|
2023-12-22 21:18:17 +08:00
|
|
|
minDelay := fast.LastDelayForTestUrl(u.testUrl)
|
2021-05-01 17:21:09 +08:00
|
|
|
fastNotExist := true
|
|
|
|
|
2019-12-10 15:04:22 +08:00
|
|
|
for _, proxy := range proxies[1:] {
|
2021-05-01 17:21:09 +08:00
|
|
|
if u.fastNode != nil && proxy.Name() == u.fastNode.Name() {
|
|
|
|
fastNotExist = false
|
|
|
|
}
|
|
|
|
|
2023-12-22 21:18:17 +08:00
|
|
|
if !proxy.AliveForTestUrl(u.testUrl) {
|
2019-12-10 15:04:22 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
delay := proxy.LastDelayForTestUrl(u.testUrl)
|
2023-12-22 21:18:17 +08:00
|
|
|
if delay < minDelay {
|
2019-12-10 15:04:22 +08:00
|
|
|
fast = proxy
|
2023-12-22 21:18:17 +08:00
|
|
|
minDelay = delay
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|
2023-06-02 18:26:51 +08:00
|
|
|
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|
2020-08-06 20:12:03 +08:00
|
|
|
// tolerance
|
2023-12-22 21:18:17 +08:00
|
|
|
if u.fastNode == nil || fastNotExist || !u.fastNode.AliveForTestUrl(u.testUrl) || u.fastNode.LastDelayForTestUrl(u.testUrl) > fast.LastDelayForTestUrl(u.testUrl)+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
|
|
|
})
|
2022-10-31 16:58:29 +08:00
|
|
|
if shared && touch { // a shared fastSingle.Do() may cause providers untouched, so we touch them again
|
|
|
|
u.Touch()
|
|
|
|
}
|
2019-12-10 15:04:22 +08:00
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
return elm
|
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 {
|
2020-11-13 21:48:52 +08:00
|
|
|
if u.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-19 00:53:22 +08:00
|
|
|
return u.fast(false).SupportUDP()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
// IsL3Protocol implements C.ProxyAdapter
|
|
|
|
func (u *URLTest) IsL3Protocol(metadata *C.Metadata) bool {
|
|
|
|
return u.fast(false).IsL3Protocol(metadata)
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-03-23 13:48:21 +08:00
|
|
|
all := []string{}
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, proxy := range u.GetProxies(false) {
|
2019-12-08 12:17:24 +08:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
2022-03-16 12:10:13 +08:00
|
|
|
return json.Marshal(map[string]any{
|
2023-12-01 16:44:30 +08:00
|
|
|
"type": u.Type().String(),
|
|
|
|
"now": u.Now(),
|
|
|
|
"all": all,
|
|
|
|
"testUrl": u.testUrl,
|
|
|
|
"expectedStatus": u.expectedStatus,
|
2023-12-26 03:49:00 +08:00
|
|
|
"fixed": u.selected,
|
2024-01-07 23:32:22 +08:00
|
|
|
"hidden": u.Hidden,
|
|
|
|
"icon": u.Icon,
|
2019-12-08 12:17:24 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-27 09:02:02 +08:00
|
|
|
func (u *URLTest) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (map[string]uint16, error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var lock sync.Mutex
|
|
|
|
mp := map[string]uint16{}
|
|
|
|
proxies := u.GetProxies(false)
|
|
|
|
for _, proxy := range proxies {
|
|
|
|
proxy := proxy
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
delay, err := proxy.URLTest(ctx, u.testUrl, expectedStatus)
|
|
|
|
if err == nil {
|
|
|
|
lock.Lock()
|
|
|
|
mp[proxy.Name()] = delay
|
|
|
|
lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if len(mp) == 0 {
|
|
|
|
return mp, fmt.Errorf("get delay: all proxies timeout")
|
|
|
|
} else {
|
|
|
|
return mp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 12:10:13 +08:00
|
|
|
func parseURLTestOption(config map[string]any) []urlTestOption {
|
2020-05-29 17:47:50 +08: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 16:48:51 +08:00
|
|
|
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
2020-05-29 17:47:50 +08:00
|
|
|
urlTest := &URLTest{
|
2022-04-28 19:01:13 +08:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.URLTest,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
2022-05-02 13:50:10 +08:00
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
option.Filter,
|
2022-11-09 08:06:37 +08:00
|
|
|
option.ExcludeFilter,
|
2023-01-07 12:24:28 +08:00
|
|
|
option.ExcludeType,
|
2022-04-28 19:01:13 +08:00
|
|
|
providers,
|
2021-11-07 16:48:51 +08:00
|
|
|
}),
|
2023-06-04 11:51:30 +08:00
|
|
|
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
|
|
|
disableUDP: option.DisableUDP,
|
|
|
|
testUrl: option.URL,
|
|
|
|
expectedStatus: option.ExpectedStatus,
|
2024-01-07 23:32:22 +08:00
|
|
|
Hidden: option.Hidden,
|
|
|
|
Icon: option.Icon,
|
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
|
|
|
}
|