clash/adapter/outboundgroup/selector.go

116 lines
2.8 KiB
Go
Raw Normal View History

2019-12-08 04:17:24 +00:00
package outboundgroup
import (
"context"
2018-11-21 05:47:46 +00:00
"encoding/json"
"errors"
2021-06-10 06:05:56 +00:00
"github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/common/singledo"
"github.com/Dreamacro/clash/component/dialer"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
)
type Selector struct {
2019-12-08 04:17:24 +00:00
*outbound.Base
disableUDP bool
single *singledo.Single
selected string
2022-01-05 04:19:49 +00:00
filter string
providers []provider.ProxyProvider
}
2021-04-29 03:23:14 +00:00
// DialContext implements C.ProxyAdapter
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
c, err := s.selectedProxy(true).DialContext(ctx, metadata, s.Base.DialOptions(opts...)...)
if err == nil {
c.AppendToChains(s)
}
return c, err
}
// ListenPacketContext implements C.ProxyAdapter
func (s *Selector) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
pc, err := s.selectedProxy(true).ListenPacketContext(ctx, metadata, s.Base.DialOptions(opts...)...)
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 {
if s.disableUDP {
return false
}
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) {
2022-03-23 05:48:21 +00:00
all := []string{}
2022-01-05 04:19:49 +00:00
for _, proxy := range getProvidersProxies(s.providers, false, s.filter) {
2019-12-08 04:17:24 +00:00
all = append(all, proxy.Name())
}
2022-03-16 04:10:13 +00:00
return json.Marshal(map[string]any{
2018-11-21 05:47:46 +00:00
"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 {
return s.selectedProxy(false).Name()
}
func (s *Selector) Set(name string) error {
2022-01-05 04:19:49 +00:00
for _, proxy := range getProvidersProxies(s.providers, false, s.filter) {
2019-12-08 04:17:24 +00:00
if proxy.Name() == name {
s.selected = name
2020-04-30 12:13:27 +00:00
s.single.Reset()
2019-12-08 04:17:24 +00:00
return nil
}
}
2020-08-25 14:19:59 +00:00
return errors.New("proxy not exist")
2019-12-08 04:17:24 +00:00
}
2021-04-29 03:23:14 +00:00
// Unwrap implements C.ProxyAdapter
2022-03-16 17:51:28 +00:00
func (s *Selector) Unwrap(*C.Metadata) C.Proxy {
return s.selectedProxy(true)
}
func (s *Selector) selectedProxy(touch bool) C.Proxy {
2022-03-16 04:10:13 +00:00
elm, _, _ := s.single.Do(func() (any, error) {
2022-01-05 04:19:49 +00:00
proxies := getProvidersProxies(s.providers, touch, s.filter)
for _, proxy := range proxies {
if proxy.Name() == s.selected {
return proxy, nil
}
}
return proxies[0], nil
})
return elm.(C.Proxy)
2019-12-08 04:17:24 +00:00
}
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
2019-12-08 04:17:24 +00:00
return &Selector{
Base: outbound.NewBase(outbound.BaseOption{
2021-11-08 08:59:48 +00:00
Name: option.Name,
Type: C.Selector,
Interface: option.Interface,
RoutingMark: option.RoutingMark,
}),
single: singledo.NewSingle(defaultGetProxiesDuration),
providers: providers,
2022-01-21 14:38:02 +00:00
selected: "COMPATIBLE",
disableUDP: option.DisableUDP,
2022-01-05 04:19:49 +00:00
filter: option.Filter,
}
}