2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
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"
|
2021-07-04 20:32:59 +08:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Fallback struct {
|
|
|
|
*outbound.Base
|
2020-11-13 21:48:52 +08:00
|
|
|
disableUDP bool
|
|
|
|
single *singledo.Single
|
|
|
|
providers []provider.ProxyProvider
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) Now() string {
|
2020-11-19 00:53:22 +08:00
|
|
|
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
|
2019-12-08 12:17:24 +08:00
|
|
|
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(true)
|
2019-12-08 12:17:24 +08:00
|
|
|
c, err := proxy.DialContext(ctx, metadata)
|
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(f)
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialUDP implements C.ProxyAdapter
|
2020-01-31 14:43:54 +08:00
|
|
|
func (f *Fallback) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(true)
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-13 21:48:52 +08:00
|
|
|
if f.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
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) {
|
|
|
|
var all []string
|
2020-11-19 00:53:22 +08:00
|
|
|
for _, proxy := range f.proxies(false) {
|
2019-12-08 12:17:24 +08:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": f.Type().String(),
|
|
|
|
"now": f.Now(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// Unwrap implements C.ProxyAdapter
|
2020-05-07 21:42:52 +08:00
|
|
|
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(true)
|
2020-05-07 21:42:52 +08:00
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (f *Fallback) proxies(touch bool) []C.Proxy {
|
2019-12-10 15:04:22 +08:00
|
|
|
elm, _, _ := f.single.Do(func() (interface{}, error) {
|
2020-11-19 00:53:22 +08:00
|
|
|
return getProvidersProxies(f.providers, touch), nil
|
2019-12-10 15:04:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
|
|
|
|
proxies := f.proxies(touch)
|
2019-12-10 15:04:22 +08:00
|
|
|
for _, proxy := range proxies {
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
return proxies[0]
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 21:48:52 +08:00
|
|
|
func NewFallback(options *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
|
2019-12-08 12:17:24 +08:00
|
|
|
return &Fallback{
|
2020-11-13 21:48:52 +08:00
|
|
|
Base: outbound.NewBase(options.Name, "", C.Fallback, false),
|
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
|
|
providers: providers,
|
|
|
|
disableUDP: options.DisableUDP,
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|