2018-07-12 15:28:38 +00:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-11-21 05:47:46 +00:00
|
|
|
"encoding/json"
|
2018-07-12 15:28:38 +00:00
|
|
|
"errors"
|
2018-12-22 15:56:42 +00:00
|
|
|
"net"
|
2018-08-07 06:45:24 +00:00
|
|
|
"sort"
|
2018-07-12 15:28:38 +00:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Selector struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2018-07-12 15:28:38 +00:00
|
|
|
selected C.Proxy
|
2018-07-18 13:50:16 +00:00
|
|
|
proxies map[string]C.Proxy
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type SelectorOption struct {
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
Proxies []string `proxy:"proxies"`
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:59:07 +00:00
|
|
|
func (s *Selector) Dial(metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
return s.selected.Dial(metadata)
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:29:36 +00:00
|
|
|
func (s *Selector) DialUDP(metadata *C.Metadata) (net.PacketConn, net.Addr, error) {
|
|
|
|
return s.selected.DialUDP(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) SupportUDP() bool {
|
|
|
|
return s.selected.SupportUDP()
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func (s *Selector) MarshalJSON() ([]byte, error) {
|
2018-07-12 15:28:38 +00:00
|
|
|
var all []string
|
2018-07-18 13:50:16 +00:00
|
|
|
for k := range s.proxies {
|
2018-07-12 15:28:38 +00:00
|
|
|
all = append(all, k)
|
|
|
|
}
|
2018-08-07 06:45:24 +00:00
|
|
|
sort.Strings(all)
|
2018-11-21 05:47:46 +00:00
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": s.Type().String(),
|
|
|
|
"now": s.Now(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Now() string {
|
|
|
|
return s.selected.Name()
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Selector) Set(name string) error {
|
2018-07-18 13:50:16 +00:00
|
|
|
proxy, exist := s.proxies[name]
|
2018-07-12 15:28:38 +00:00
|
|
|
if !exist {
|
|
|
|
return errors.New("Proxy does not exist")
|
|
|
|
}
|
|
|
|
s.selected = proxy
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-18 15:24:04 +00:00
|
|
|
func NewSelector(name string, proxies []C.Proxy) (*Selector, error) {
|
2018-07-18 13:50:16 +00:00
|
|
|
if len(proxies) == 0 {
|
2018-07-12 15:28:38 +00:00
|
|
|
return nil, errors.New("Provide at least one proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping := make(map[string]C.Proxy)
|
2018-10-18 15:24:04 +00:00
|
|
|
for _, proxy := range proxies {
|
|
|
|
mapping[proxy.Name()] = proxy
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
2018-10-18 15:24:04 +00:00
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
s := &Selector{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
|
|
|
name: name,
|
|
|
|
tp: C.Selector,
|
|
|
|
},
|
2018-07-18 13:50:16 +00:00
|
|
|
proxies: mapping,
|
2018-10-18 15:24:04 +00:00
|
|
|
selected: proxies[0],
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|