Clash.Meta/adapter/outboundgroup/selector.go

109 lines
2.4 KiB
Go
Raw Normal View History

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