2019-12-08 04:17:24 +00:00
|
|
|
package outboundgroup
|
2018-07-12 15:28:38 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2018-11-21 05:47:46 +00:00
|
|
|
"encoding/json"
|
2018-07-12 15:28:38 +00:00
|
|
|
"errors"
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
2019-12-10 07:04:22 +00:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
2018-07-12 15:28:38 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Selector struct {
|
2019-12-08 04:17:24 +00:00
|
|
|
*outbound.Base
|
2020-11-13 13:48:52 +00:00
|
|
|
disableUDP bool
|
|
|
|
single *singledo.Single
|
|
|
|
selected string
|
|
|
|
providers []provider.ProxyProvider
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2019-10-12 15:55:39 +00:00
|
|
|
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
c, err := s.selectedProxy(true).DialContext(ctx, metadata)
|
2019-08-08 17:28:37 +00:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(s)
|
|
|
|
}
|
|
|
|
return c, err
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialUDP implements C.ProxyAdapter
|
2020-01-31 06:43:54 +00:00
|
|
|
func (s *Selector) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
pc, err := s.selectedProxy(true).DialUDP(metadata)
|
2019-08-08 17:28:37 +00:00
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(s)
|
|
|
|
}
|
2020-01-31 06:43:54 +00:00
|
|
|
return pc, err
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// SupportUDP implements C.ProxyAdapter
|
2019-04-23 15:29:36 +00:00
|
|
|
func (s *Selector) SupportUDP() bool {
|
2020-11-13 13:48:52 +00:00
|
|
|
if s.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
return s.selectedProxy(false).SupportUDP()
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
2018-11-21 05:47:46 +00:00
|
|
|
func (s *Selector) MarshalJSON() ([]byte, error) {
|
2019-12-08 04:17:24 +00:00
|
|
|
var all []string
|
2020-11-18 16:53:22 +00:00
|
|
|
for _, proxy := range getProvidersProxies(s.providers, false) {
|
2019-12-08 04:17:24 +00:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": s.Type().String(),
|
|
|
|
"now": s.Now(),
|
2019-12-08 04:17:24 +00:00
|
|
|
"all": all,
|
2018-11-21 05:47:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Now() string {
|
2020-11-18 16:53:22 +00:00
|
|
|
return s.selectedProxy(false).Name()
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Set(name string) error {
|
2020-11-18 16:53:22 +00:00
|
|
|
for _, proxy := range getProvidersProxies(s.providers, false) {
|
2019-12-08 04:17:24 +00:00
|
|
|
if proxy.Name() == name {
|
2020-04-27 13:23:03 +00:00
|
|
|
s.selected = name
|
2020-04-30 12:13:27 +00:00
|
|
|
s.single.Reset()
|
2019-12-08 04:17:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 14:19:59 +00:00
|
|
|
return errors.New("proxy not exist")
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
2018-07-12 15:28:38 +00:00
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// Unwrap implements C.ProxyAdapter
|
2020-05-07 13:42:52 +00:00
|
|
|
func (s *Selector) Unwrap(metadata *C.Metadata) C.Proxy {
|
2020-11-18 16:53:22 +00:00
|
|
|
return s.selectedProxy(true)
|
2020-05-07 13:42:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
func (s *Selector) selectedProxy(touch bool) C.Proxy {
|
2019-12-10 07:04:22 +00:00
|
|
|
elm, _, _ := s.single.Do(func() (interface{}, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
proxies := getProvidersProxies(s.providers, touch)
|
2020-04-27 13:23:03 +00:00
|
|
|
for _, proxy := range proxies {
|
|
|
|
if proxy.Name() == s.selected {
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies[0], nil
|
2019-12-10 07:04:22 +00:00
|
|
|
})
|
|
|
|
|
2020-04-27 13:23:03 +00:00
|
|
|
return elm.(C.Proxy)
|
2019-12-08 04:17:24 +00:00
|
|
|
}
|
2018-10-18 15:24:04 +00:00
|
|
|
|
2020-11-13 13:48:52 +00:00
|
|
|
func NewSelector(options *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
|
2020-04-27 13:23:03 +00:00
|
|
|
selected := providers[0].Proxies()[0].Name()
|
2019-12-08 04:17:24 +00:00
|
|
|
return &Selector{
|
2020-11-13 13:48:52 +00:00
|
|
|
Base: outbound.NewBase(options.Name, "", C.Selector, false),
|
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
|
|
providers: providers,
|
|
|
|
selected: selected,
|
|
|
|
disableUDP: options.DisableUDP,
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
}
|