2018-09-26 00:34:15 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type proxy struct {
|
|
|
|
RawProxy C.Proxy
|
|
|
|
Valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Fallback struct {
|
|
|
|
name string
|
|
|
|
proxies []*proxy
|
|
|
|
rawURL string
|
|
|
|
delay time.Duration
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
type FallbackOption struct {
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
Proxies []string `proxy:"proxies"`
|
|
|
|
URL string `proxy:"url"`
|
|
|
|
Delay int `proxy:"delay"`
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:34:15 +08:00
|
|
|
func (f *Fallback) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) Type() C.AdapterType {
|
|
|
|
return C.Fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) Now() string {
|
|
|
|
_, proxy := f.findNextValidProxy(0)
|
|
|
|
if proxy != nil {
|
|
|
|
return proxy.RawProxy.Name()
|
|
|
|
}
|
|
|
|
return f.proxies[0].RawProxy.Name()
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (f *Fallback) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
2018-09-26 00:34:15 +08:00
|
|
|
idx := 0
|
|
|
|
var proxy *proxy
|
|
|
|
for {
|
|
|
|
idx, proxy = f.findNextValidProxy(idx)
|
|
|
|
if proxy == nil {
|
|
|
|
break
|
|
|
|
}
|
2018-09-30 12:25:52 +08:00
|
|
|
adapter, err = proxy.RawProxy.Generator(metadata)
|
2018-09-26 00:34:15 +08:00
|
|
|
if err != nil {
|
|
|
|
proxy.Valid = false
|
|
|
|
idx++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-10-06 14:17:50 +08:00
|
|
|
return f.proxies[0].RawProxy.Generator(metadata)
|
2018-09-26 00:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) Close() {
|
|
|
|
f.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) loop() {
|
|
|
|
tick := time.NewTicker(f.delay)
|
|
|
|
go f.validTest()
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
go f.validTest()
|
|
|
|
case <-f.done:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) findNextValidProxy(start int) (int, *proxy) {
|
|
|
|
for i := start; i < len(f.proxies); i++ {
|
|
|
|
if f.proxies[i].Valid {
|
|
|
|
return i, f.proxies[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) validTest() {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(f.proxies))
|
|
|
|
|
|
|
|
for _, p := range f.proxies {
|
|
|
|
go func(p *proxy) {
|
|
|
|
_, err := DelayTest(p.RawProxy, f.rawURL)
|
|
|
|
p.Valid = err == nil
|
|
|
|
wg.Done()
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
func NewFallback(option FallbackOption, proxies []C.Proxy) (*Fallback, error) {
|
|
|
|
_, err := urlToMetadata(option.URL)
|
2018-09-26 00:34:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxies) < 1 {
|
|
|
|
return nil, errors.New("The number of proxies cannot be 0")
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
delay := time.Duration(option.Delay) * time.Second
|
2018-09-26 00:34:15 +08:00
|
|
|
warpperProxies := make([]*proxy, len(proxies))
|
|
|
|
for idx := range proxies {
|
|
|
|
warpperProxies[idx] = &proxy{
|
|
|
|
RawProxy: proxies[idx],
|
|
|
|
Valid: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Fallback := &Fallback{
|
2018-10-02 15:26:36 +08:00
|
|
|
name: option.Name,
|
2018-09-26 00:34:15 +08:00
|
|
|
proxies: warpperProxies,
|
2018-10-02 15:26:36 +08:00
|
|
|
rawURL: option.URL,
|
2018-09-26 00:34:15 +08:00
|
|
|
delay: delay,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
go Fallback.loop()
|
|
|
|
return Fallback, nil
|
|
|
|
}
|