mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package outboundgroup
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
|
"github.com/Dreamacro/clash/common/singledo"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type Fallback struct {
|
|
*outbound.Base
|
|
single *singledo.Single
|
|
providers []provider.ProxyProvider
|
|
}
|
|
|
|
func (f *Fallback) Now() string {
|
|
proxy := f.findAliveProxy()
|
|
return proxy.Name()
|
|
}
|
|
|
|
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
proxy := f.findAliveProxy()
|
|
c, err := proxy.DialContext(ctx, metadata)
|
|
if err == nil {
|
|
c.AppendToChains(f)
|
|
}
|
|
return c, err
|
|
}
|
|
|
|
func (f *Fallback) DialUDP(metadata *C.Metadata) (C.PacketConn, net.Addr, error) {
|
|
proxy := f.findAliveProxy()
|
|
pc, addr, err := proxy.DialUDP(metadata)
|
|
if err == nil {
|
|
pc.AppendToChains(f)
|
|
}
|
|
return pc, addr, err
|
|
}
|
|
|
|
func (f *Fallback) SupportUDP() bool {
|
|
proxy := f.findAliveProxy()
|
|
return proxy.SupportUDP()
|
|
}
|
|
|
|
func (f *Fallback) MarshalJSON() ([]byte, error) {
|
|
var all []string
|
|
for _, proxy := range f.proxies() {
|
|
all = append(all, proxy.Name())
|
|
}
|
|
return json.Marshal(map[string]interface{}{
|
|
"type": f.Type().String(),
|
|
"now": f.Now(),
|
|
"all": all,
|
|
})
|
|
}
|
|
|
|
func (f *Fallback) proxies() []C.Proxy {
|
|
elm, _, _ := f.single.Do(func() (interface{}, error) {
|
|
return getProvidersProxies(f.providers), nil
|
|
})
|
|
|
|
return elm.([]C.Proxy)
|
|
}
|
|
|
|
func (f *Fallback) findAliveProxy() C.Proxy {
|
|
proxies := f.proxies()
|
|
for _, proxy := range proxies {
|
|
if proxy.Alive() {
|
|
return proxy
|
|
}
|
|
}
|
|
|
|
return f.proxies()[0]
|
|
}
|
|
|
|
func NewFallback(name string, providers []provider.ProxyProvider) *Fallback {
|
|
return &Fallback{
|
|
Base: outbound.NewBase(name, C.Fallback, false),
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
providers: providers,
|
|
}
|
|
}
|