2018-06-16 21:34:13 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-10-02 15:26:36 +08:00
|
|
|
"errors"
|
2018-06-16 21:34:13 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type URLTest struct {
|
2018-10-06 15:13:44 +08:00
|
|
|
name string
|
|
|
|
proxies []C.Proxy
|
|
|
|
rawURL string
|
|
|
|
fast C.Proxy
|
|
|
|
interval time.Duration
|
|
|
|
done chan struct{}
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
type URLTestOption struct {
|
2018-10-06 15:13:44 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Proxies []string `proxy:"proxies"`
|
|
|
|
URL string `proxy:"url"`
|
|
|
|
Interval int `proxy:"interval"`
|
2018-10-02 15:26:36 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
func (u *URLTest) Name() string {
|
|
|
|
return u.name
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
func (u *URLTest) Type() C.AdapterType {
|
|
|
|
return C.URLTest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) Now() string {
|
|
|
|
return u.fast.Name()
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (u *URLTest) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
|
|
|
return u.fast.Generator(metadata)
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:31:36 +08:00
|
|
|
func (u *URLTest) Close() {
|
|
|
|
u.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
func (u *URLTest) loop() {
|
2018-10-06 15:13:44 +08:00
|
|
|
tick := time.NewTicker(u.interval)
|
2018-06-16 21:34:13 +08:00
|
|
|
go u.speedTest()
|
2018-06-19 20:31:36 +08:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
go u.speedTest()
|
|
|
|
case <-u.done:
|
|
|
|
break Loop
|
|
|
|
}
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) speedTest() {
|
|
|
|
wg := sync.WaitGroup{}
|
2018-07-18 21:50:16 +08:00
|
|
|
wg.Add(len(u.proxies))
|
2018-06-16 21:34:13 +08:00
|
|
|
c := make(chan interface{})
|
|
|
|
fast := selectFast(c)
|
2018-10-06 15:13:44 +08:00
|
|
|
timer := time.NewTimer(u.interval)
|
2018-06-16 21:34:13 +08:00
|
|
|
|
2018-07-18 21:50:16 +08:00
|
|
|
for _, p := range u.proxies {
|
2018-06-16 21:34:13 +08:00
|
|
|
go func(p C.Proxy) {
|
2018-08-08 11:51:06 +08:00
|
|
|
_, err := DelayTest(p, u.rawURL)
|
2018-06-16 21:34:13 +08:00
|
|
|
if err == nil {
|
|
|
|
c <- p
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
// Wait for fast to return or close.
|
|
|
|
<-fast
|
|
|
|
case p, open := <-fast:
|
|
|
|
if open {
|
|
|
|
u.fast = p.(C.Proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
func NewURLTest(option URLTestOption, proxies []C.Proxy) (*URLTest, error) {
|
|
|
|
_, err := urlToMetadata(option.URL)
|
2018-06-16 21:34:13 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-02 15:26:36 +08:00
|
|
|
if len(proxies) < 1 {
|
|
|
|
return nil, errors.New("The number of proxies cannot be 0")
|
|
|
|
}
|
2018-06-16 21:34:13 +08:00
|
|
|
|
2018-10-06 15:13:44 +08:00
|
|
|
interval := time.Duration(option.Interval) * time.Second
|
2018-06-16 21:34:13 +08:00
|
|
|
urlTest := &URLTest{
|
2018-10-06 15:13:44 +08:00
|
|
|
name: option.Name,
|
|
|
|
proxies: proxies[:],
|
|
|
|
rawURL: option.URL,
|
|
|
|
fast: proxies[0],
|
|
|
|
interval: interval,
|
|
|
|
done: make(chan struct{}),
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
go urlTest.loop()
|
|
|
|
return urlTest, nil
|
|
|
|
}
|