2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
2019-12-10 15:04:22 +08:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
2019-12-08 12:17:24 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Fallback struct {
|
|
|
|
*outbound.Base
|
2019-12-10 15:04:22 +08:00
|
|
|
single *singledo.Single
|
2019-12-08 12:17:24 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (f *Fallback) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
2019-12-08 12:17:24 +08:00
|
|
|
proxy := f.findAliveProxy()
|
2020-01-31 14:43:54 +08:00
|
|
|
pc, err := proxy.DialUDP(metadata)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-07 21:42:52 +08:00
|
|
|
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
|
|
|
|
proxy := f.findAliveProxy()
|
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (f *Fallback) proxies() []C.Proxy {
|
2019-12-10 15:04:22 +08:00
|
|
|
elm, _, _ := f.single.Do(func() (interface{}, error) {
|
|
|
|
return getProvidersProxies(f.providers), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) findAliveProxy() C.Proxy {
|
2019-12-10 15:04:22 +08:00
|
|
|
proxies := f.proxies()
|
|
|
|
for _, proxy := range proxies {
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:04:22 +08:00
|
|
|
return f.proxies()[0]
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFallback(name string, providers []provider.ProxyProvider) *Fallback {
|
|
|
|
return &Fallback{
|
2020-03-21 23:46:49 +08:00
|
|
|
Base: outbound.NewBase(name, "", C.Fallback, false),
|
2019-12-10 15:04:22 +08:00
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
2019-12-08 12:17:24 +08:00
|
|
|
providers: providers,
|
|
|
|
}
|
|
|
|
}
|