2018-09-25 16:34:15 +00:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2019-07-02 11:18:03 +00:00
|
|
|
"context"
|
2018-11-21 05:47:46 +00:00
|
|
|
"encoding/json"
|
2018-09-25 16:34:15 +00:00
|
|
|
"errors"
|
2018-12-22 15:56:42 +00:00
|
|
|
"net"
|
2018-09-25 16:34:15 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Fallback struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2019-03-15 16:43:16 +00:00
|
|
|
proxies []C.Proxy
|
2018-10-06 07:13:44 +00:00
|
|
|
rawURL string
|
|
|
|
interval time.Duration
|
|
|
|
done chan struct{}
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type FallbackOption struct {
|
2018-10-06 07:13:44 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Proxies []string `proxy:"proxies"`
|
|
|
|
URL string `proxy:"url"`
|
|
|
|
Interval int `proxy:"interval"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 16:34:15 +00:00
|
|
|
func (f *Fallback) Now() string {
|
2019-03-15 16:43:16 +00:00
|
|
|
proxy := f.findAliveProxy()
|
|
|
|
return proxy.Name()
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:59:07 +00:00
|
|
|
func (f *Fallback) Dial(metadata *C.Metadata) (net.Conn, error) {
|
2019-03-15 16:43:16 +00:00
|
|
|
proxy := f.findAliveProxy()
|
|
|
|
return proxy.Dial(metadata)
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:29:36 +00:00
|
|
|
func (f *Fallback) DialUDP(metadata *C.Metadata) (net.PacketConn, net.Addr, error) {
|
|
|
|
proxy := f.findAliveProxy()
|
|
|
|
return proxy.DialUDP(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) SupportUDP() bool {
|
|
|
|
proxy := f.findAliveProxy()
|
|
|
|
return proxy.SupportUDP()
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func (f *Fallback) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
|
|
|
for _, proxy := range f.proxies {
|
2019-03-15 16:43:16 +00:00
|
|
|
all = append(all, proxy.Name())
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": f.Type().String(),
|
|
|
|
"now": f.Now(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:43:16 +00:00
|
|
|
func (f *Fallback) Destroy() {
|
2018-09-25 16:34:15 +00:00
|
|
|
f.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) loop() {
|
2018-10-06 07:13:44 +00:00
|
|
|
tick := time.NewTicker(f.interval)
|
2018-09-25 16:34:15 +00:00
|
|
|
go f.validTest()
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
go f.validTest()
|
|
|
|
case <-f.done:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:43:16 +00:00
|
|
|
func (f *Fallback) findAliveProxy() C.Proxy {
|
|
|
|
for _, proxy := range f.proxies {
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 16:43:16 +00:00
|
|
|
return f.proxies[0]
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) validTest() {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(f.proxies))
|
|
|
|
|
|
|
|
for _, p := range f.proxies {
|
2019-03-15 16:43:16 +00:00
|
|
|
go func(p C.Proxy) {
|
2019-07-02 11:18:03 +00:00
|
|
|
p.URLTest(context.Background(), f.rawURL)
|
2018-09-25 16:34:15 +00:00
|
|
|
wg.Done()
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
func NewFallback(option FallbackOption, proxies []C.Proxy) (*Fallback, error) {
|
|
|
|
_, err := urlToMetadata(option.URL)
|
2018-09-25 16:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxies) < 1 {
|
|
|
|
return nil, errors.New("The number of proxies cannot be 0")
|
|
|
|
}
|
|
|
|
|
2018-10-06 07:13:44 +00:00
|
|
|
interval := time.Duration(option.Interval) * time.Second
|
2018-09-25 16:34:15 +00:00
|
|
|
|
|
|
|
Fallback := &Fallback{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.Fallback,
|
|
|
|
},
|
2019-03-15 16:43:16 +00:00
|
|
|
proxies: proxies,
|
2018-10-06 07:13:44 +00:00
|
|
|
rawURL: option.URL,
|
|
|
|
interval: interval,
|
|
|
|
done: make(chan struct{}),
|
2018-09-25 16:34:15 +00:00
|
|
|
}
|
|
|
|
go Fallback.loop()
|
|
|
|
return Fallback, nil
|
|
|
|
}
|