Clash.Meta/adapter/outboundgroup/selector.go

127 lines
2.9 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"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/adapter/outbound"
"github.com/metacubex/mihomo/component/dialer"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/constant/provider"
)
type Selector struct {
*GroupBase
disableUDP bool
selected string
Hidden bool
Icon string
}
2021-04-29 11:23:14 +08: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 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
}
// IsL3Protocol implements C.ProxyAdapter
func (s *Selector) IsL3Protocol(metadata *C.Metadata) bool {
return s.selectedProxy(false).IsL3Protocol(metadata)
}
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) {
2022-03-23 13:48:21 +08:00
all := []string{}
for _, proxy := range s.GetProxies(false) {
2019-12-08 12:17:24 +08:00
all = append(all, proxy.Name())
}
2022-03-16 12:10:13 +08:00
return json.Marshal(map[string]any{
"type": s.Type().String(),
"now": s.Now(),
"all": all,
"hidden": s.Hidden,
"icon": s.Icon,
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 s.GetProxies(false) {
2019-12-08 12:17:24 +08:00
if proxy.Name() == name {
s.selected = name
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
}
func (s *Selector) ForceSet(name string) {
s.selected = name
}
2021-04-29 11:23:14 +08:00
// Unwrap implements C.ProxyAdapter
2022-10-30 23:08:18 +08:00
func (s *Selector) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
return s.selectedProxy(touch)
}
func (s *Selector) selectedProxy(touch bool) C.Proxy {
proxies := s.GetProxies(touch)
for _, proxy := range proxies {
if proxy.Name() == s.selected {
return proxy
}
}
return proxies[0]
2019-12-08 12:17:24 +08:00
}
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
2019-12-08 12:17:24 +08:00
return &Selector{
GroupBase: NewGroupBase(GroupBaseOption{
outbound.BaseOption{
Name: option.Name,
Type: C.Selector,
Interface: option.Interface,
RoutingMark: option.RoutingMark,
},
option.Filter,
option.ExcludeFilter,
option.ExcludeType,
2024-03-07 03:35:11 +08:00
option.TestTimeout,
option.MaxFailedTimes,
providers,
}),
2022-01-21 22:38:02 +08:00
selected: "COMPATIBLE",
disableUDP: option.DisableUDP,
Hidden: option.Hidden,
Icon: option.Icon,
}
}