Clash.Meta/adapter/outboundgroup/fallback.go

176 lines
4.0 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outboundgroup
import (
"context"
"encoding/json"
2022-05-23 00:40:27 +08:00
"errors"
"time"
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/utils"
"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
)
type Fallback struct {
*GroupBase
disableUDP bool
testUrl string
selected string
expectedStatus string
Hidden bool
Icon string
2019-12-08 12:17:24 +08:00
}
func (f *Fallback) Now() string {
proxy := f.findAliveProxy(false)
2019-12-08 12:17:24 +08:00
return proxy.Name()
}
2021-04-29 11:23:14 +08:00
// DialContext implements C.ProxyAdapter
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
proxy := f.findAliveProxy(true)
c, err := proxy.DialContext(ctx, metadata, f.Base.DialOptions(opts...)...)
2019-12-08 12:17:24 +08:00
if err == nil {
c.AppendToChains(f)
2021-12-03 14:35:21 +08:00
} else {
f.onDialFailed(proxy.Type(), err)
2019-12-08 12:17:24 +08:00
}
if N.NeedHandshake(c) {
c = callback.NewFirstWriteCallBackConn(c, func(err error) {
if err == nil {
f.onDialSuccess()
} else {
f.onDialFailed(proxy.Type(), err)
}
})
}
2019-12-08 12:17:24 +08:00
return c, err
}
// ListenPacketContext implements C.ProxyAdapter
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
proxy := f.findAliveProxy(true)
pc, err := proxy.ListenPacketContext(ctx, metadata, f.Base.DialOptions(opts...)...)
2019-12-08 12:17:24 +08:00
if err == nil {
pc.AppendToChains(f)
}
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
// SupportUDP implements C.ProxyAdapter
2019-12-08 12:17:24 +08:00
func (f *Fallback) SupportUDP() bool {
if f.disableUDP {
return false
}
proxy := f.findAliveProxy(false)
2019-12-08 12:17:24 +08:00
return proxy.SupportUDP()
}
// IsL3Protocol implements C.ProxyAdapter
func (f *Fallback) IsL3Protocol(metadata *C.Metadata) bool {
return f.findAliveProxy(false).IsL3Protocol(metadata)
}
2021-04-29 11:23:14 +08:00
// MarshalJSON implements C.ProxyAdapter
2019-12-08 12:17:24 +08:00
func (f *Fallback) MarshalJSON() ([]byte, error) {
2022-03-23 13:48:21 +08:00
all := []string{}
for _, proxy := range f.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{
"type": f.Type().String(),
"now": f.Now(),
"all": all,
"testUrl": f.testUrl,
"expectedStatus": f.expectedStatus,
2023-12-26 03:49:00 +08:00
"fixed": f.selected,
"hidden": f.Hidden,
"icon": f.Icon,
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 (f *Fallback) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
proxy := f.findAliveProxy(touch)
return proxy
}
func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
proxies := f.GetProxies(touch)
for _, proxy := range proxies {
if len(f.selected) == 0 {
if proxy.AliveForTestUrl(f.testUrl) {
return proxy
}
} else {
if proxy.Name() == f.selected {
if proxy.AliveForTestUrl(f.testUrl) {
return proxy
} else {
f.selected = ""
}
}
2022-05-23 00:40:27 +08:00
}
}
return proxies[0]
2022-05-23 00:40:27 +08:00
}
2022-06-03 16:50:05 +08:00
func (f *Fallback) Set(name string) error {
var p C.Proxy
2022-05-23 00:40:27 +08:00
for _, proxy := range f.GetProxies(false) {
if proxy.Name() == name {
p = proxy
break
2022-05-23 00:40:27 +08:00
}
2019-12-08 12:17:24 +08:00
}
if p == nil {
return errors.New("proxy not exist")
}
f.selected = name
if !p.AliveForTestUrl(f.testUrl) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cancel()
expectedStatus, _ := utils.NewIntRanges[uint16](f.expectedStatus)
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus)
}
return nil
2019-12-08 12:17:24 +08:00
}
func (f *Fallback) ForceSet(name string) {
f.selected = name
}
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
2019-12-08 12:17:24 +08:00
return &Fallback{
GroupBase: NewGroupBase(GroupBaseOption{
outbound.BaseOption{
Name: option.Name,
Type: C.Fallback,
Interface: option.Interface,
RoutingMark: option.RoutingMark,
},
option.Filter,
option.ExcludeFilter,
option.ExcludeType,
providers,
}),
disableUDP: option.DisableUDP,
testUrl: option.URL,
expectedStatus: option.ExpectedStatus,
Hidden: option.Hidden,
Icon: option.Icon,
2019-12-08 12:17:24 +08:00
}
}