mihomo/adapter/outboundgroup/fallback.go

134 lines
2.9 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"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter/outbound"
"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"
"time"
2019-12-08 12:17:24 +08:00
)
type Fallback struct {
*GroupBase
disableUDP bool
testUrl string
2022-05-23 00:40:27 +08:00
selected 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)
f.onDialSuccess()
2021-12-03 14:35:21 +08:00
} else {
f.onDialFailed()
2019-12-08 12:17:24 +08:00
}
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()
}
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{
2019-12-08 12:17:24 +08:00
"type": f.Type().String(),
"now": f.Now(),
"all": all,
})
}
2021-04-29 11:23:14 +08:00
// Unwrap implements C.ProxyAdapter
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
proxy := f.findAliveProxy(true)
return proxy
}
func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
proxies := f.GetProxies(touch)
2022-05-23 00:40:27 +08:00
al := proxies[0]
for i := len(proxies) - 1; i > -1; i-- {
proxy := proxies[i]
if proxy.Name() == f.selected && proxy.Alive() {
return proxy
2019-12-08 12:17:24 +08:00
}
2022-05-23 00:40:27 +08:00
if proxy.Alive() {
al = proxy
}
}
return al
}
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.Alive() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cancel()
_, _ = p.URLTest(ctx, f.testUrl)
}
return nil
2019-12-08 12:17:24 +08:00
}
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,
providers,
}),
disableUDP: option.DisableUDP,
testUrl: option.URL,
2019-12-08 12:17:24 +08:00
}
}