2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
2018-07-12 23:28:38 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2018-11-21 13:47:46 +08:00
|
|
|
"encoding/json"
|
2018-07-12 23:28:38 +08:00
|
|
|
"errors"
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2021-11-07 16:48:51 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-07-12 23:28:38 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 20:32:59 +08:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2018-07-12 23:28:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Selector struct {
|
2022-04-28 19:01:13 +08:00
|
|
|
*GroupBase
|
2020-11-13 21:48:52 +08:00
|
|
|
disableUDP bool
|
|
|
|
selected string
|
2018-10-02 15:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
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...)...)
|
2019-08-09 01:28:37 +08:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(s)
|
|
|
|
}
|
|
|
|
return c, err
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
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...)...)
|
2019-08-09 01:28:37 +08:00
|
|
|
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 {
|
2020-11-13 21:48:52 +08:00
|
|
|
if s.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
return s.selectedProxy(false).SupportUDP()
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +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{}
|
2022-04-28 19:01:13 +08:00
|
|
|
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{
|
2018-11-21 13:47:46 +08:00
|
|
|
"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 {
|
2020-11-19 00:53:22 +08:00
|
|
|
return s.selectedProxy(false).Name()
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Set(name string) error {
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, proxy := range s.GetProxies(false) {
|
2019-12-08 12:17:24 +08:00
|
|
|
if proxy.Name() == name {
|
2020-04-27 21:23:03 +08:00
|
|
|
s.selected = name
|
2019-12-08 12:17:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 22:19:59 +08:00
|
|
|
return errors.New("proxy not exist")
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2023-04-24 08:07:17 +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)
|
2020-05-07 21:42:52 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (s *Selector) selectedProxy(touch bool) C.Proxy {
|
2022-04-28 19:01:13 +08:00
|
|
|
proxies := s.GetProxies(touch)
|
|
|
|
for _, proxy := range proxies {
|
|
|
|
if proxy.Name() == s.selected {
|
|
|
|
return proxy
|
2020-04-27 21:23:03 +08:00
|
|
|
}
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
2020-04-27 21:23:03 +08:00
|
|
|
|
2022-04-28 19:01:13 +08:00
|
|
|
return proxies[0]
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2018-10-18 23:24:04 +08:00
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
|
2019-12-08 12:17:24 +08:00
|
|
|
return &Selector{
|
2022-04-28 19:01:13 +08:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.Selector,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
|
|
|
option.Filter,
|
2022-11-09 08:06:37 +08:00
|
|
|
option.ExcludeFilter,
|
2023-01-07 12:24:28 +08:00
|
|
|
option.ExcludeType,
|
2022-04-28 19:01:13 +08:00
|
|
|
providers,
|
2021-11-07 16:48:51 +08:00
|
|
|
}),
|
2022-01-21 22:38:02 +08:00
|
|
|
selected: "COMPATIBLE",
|
2021-11-07 16:48:51 +08:00
|
|
|
disableUDP: option.DisableUDP,
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
}
|