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"
|
2018-12-22 23:56:42 +08:00
|
|
|
"net"
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
2018-07-12 23:28:38 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Selector struct {
|
2019-12-08 12:17:24 +08:00
|
|
|
*outbound.Base
|
2019-05-15 14:40:14 +08:00
|
|
|
selected C.Proxy
|
2019-12-08 12:17:24 +08:00
|
|
|
providers []provider.ProxyProvider
|
2018-10-02 15:26:36 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:55:39 +08:00
|
|
|
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
|
|
c, err := s.selected.DialContext(ctx, metadata)
|
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
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (s *Selector) DialUDP(metadata *C.Metadata) (C.PacketConn, net.Addr, error) {
|
|
|
|
pc, addr, err := s.selected.DialUDP(metadata)
|
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(s)
|
|
|
|
}
|
|
|
|
return pc, addr, err
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) SupportUDP() bool {
|
|
|
|
return s.selected.SupportUDP()
|
|
|
|
}
|
|
|
|
|
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 s.proxies() {
|
|
|
|
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.selected.Name()
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Set(name string) error {
|
2019-12-08 12:17:24 +08:00
|
|
|
for _, proxy := range s.proxies() {
|
|
|
|
if proxy.Name() == name {
|
|
|
|
s.selected = proxy
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
return errors.New("Proxy does not exist")
|
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func (s *Selector) proxies() []C.Proxy {
|
|
|
|
proxies := []C.Proxy{}
|
|
|
|
for _, provider := range s.providers {
|
|
|
|
proxies = append(proxies, provider.Proxies()...)
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
return proxies
|
|
|
|
}
|
2018-10-18 23:24:04 +08:00
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
func NewSelector(name string, providers []provider.ProxyProvider) *Selector {
|
|
|
|
selected := providers[0].Proxies()[0]
|
|
|
|
return &Selector{
|
|
|
|
Base: outbound.NewBase(name, C.Selector, false),
|
|
|
|
providers: providers,
|
|
|
|
selected: selected,
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
}
|