2018-06-16 21:34:13 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2019-03-16 00:43:16 +08:00
|
|
|
"context"
|
2018-11-21 13:47:46 +08:00
|
|
|
"encoding/json"
|
2018-10-02 15:26:36 +08:00
|
|
|
"errors"
|
2018-12-22 23:56:42 +08:00
|
|
|
"net"
|
2018-10-24 17:06:08 +08:00
|
|
|
"sync/atomic"
|
2018-06-16 21:34:13 +08:00
|
|
|
"time"
|
|
|
|
|
2019-03-16 00:43:16 +08:00
|
|
|
"github.com/Dreamacro/clash/common/picker"
|
2018-06-16 21:34:13 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type URLTest struct {
|
2018-12-22 23:56:42 +08:00
|
|
|
*Base
|
2018-10-06 15:13:44 +08:00
|
|
|
proxies []C.Proxy
|
|
|
|
rawURL string
|
|
|
|
fast C.Proxy
|
|
|
|
interval time.Duration
|
|
|
|
done chan struct{}
|
2018-10-24 17:06:08 +08:00
|
|
|
once int32
|
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-07-12 23:28:38 +08:00
|
|
|
func (u *URLTest) Now() string {
|
|
|
|
return u.fast.Name()
|
|
|
|
}
|
|
|
|
|
2019-10-12 23:55:39 +08:00
|
|
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
2019-08-28 22:28:02 +08:00
|
|
|
for i := 0; i < 3; i++ {
|
2019-10-12 23:55:39 +08:00
|
|
|
c, err = u.fast.DialContext(ctx, metadata)
|
2019-08-28 22:28:02 +08:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(u)
|
|
|
|
return
|
|
|
|
}
|
2019-03-17 14:52:39 +08:00
|
|
|
u.fallback()
|
2018-10-24 17:06:08 +08:00
|
|
|
}
|
2019-08-28 22:28:02 +08:00
|
|
|
return
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (u *URLTest) DialUDP(metadata *C.Metadata) (C.PacketConn, net.Addr, error) {
|
|
|
|
pc, addr, err := u.fast.DialUDP(metadata)
|
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(u)
|
|
|
|
}
|
|
|
|
return pc, addr, err
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *URLTest) SupportUDP() bool {
|
|
|
|
return u.fast.SupportUDP()
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func (u *URLTest) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
|
|
|
for _, proxy := range u.proxies {
|
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": u.Type().String(),
|
|
|
|
"now": u.Now(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:43:16 +08:00
|
|
|
func (u *URLTest) Destroy() {
|
2018-06-19 20:31:36 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:52:39 +08:00
|
|
|
func (u *URLTest) fallback() {
|
|
|
|
fast := u.proxies[0]
|
|
|
|
min := fast.LastDelay()
|
|
|
|
for _, proxy := range u.proxies[1:] {
|
|
|
|
if !proxy.Alive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := proxy.LastDelay()
|
|
|
|
if delay < min {
|
|
|
|
fast = proxy
|
|
|
|
min = delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.fast = fast
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
func (u *URLTest) speedTest() {
|
2019-03-29 10:27:26 +08:00
|
|
|
if !atomic.CompareAndSwapInt32(&u.once, 0, 1) {
|
2018-10-24 17:06:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&u.once, 0)
|
|
|
|
|
2019-10-12 23:29:00 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
|
2019-07-02 19:18:03 +08:00
|
|
|
defer cancel()
|
2019-10-12 23:29:00 +08:00
|
|
|
picker := picker.WithoutAutoCancel(ctx)
|
2018-07-18 21:50:16 +08:00
|
|
|
for _, p := range u.proxies {
|
2019-07-17 22:24:26 +08:00
|
|
|
proxy := p
|
2019-07-02 19:18:03 +08:00
|
|
|
picker.Go(func() (interface{}, error) {
|
2019-07-18 00:12:01 +08:00
|
|
|
_, err := proxy.URLTest(ctx, u.rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
2019-07-17 22:24:26 +08:00
|
|
|
return proxy, nil
|
2019-07-02 19:18:03 +08:00
|
|
|
})
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:29:00 +08:00
|
|
|
fast := picker.WaitWithoutCancel()
|
2019-07-02 19:18:03 +08:00
|
|
|
if fast != nil {
|
|
|
|
u.fast = fast.(C.Proxy)
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
2019-10-12 23:29:00 +08:00
|
|
|
|
|
|
|
picker.Wait()
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
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-12-22 23:56:42 +08:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.URLTest,
|
|
|
|
},
|
2018-10-06 15:13:44 +08:00
|
|
|
proxies: proxies[:],
|
|
|
|
rawURL: option.URL,
|
|
|
|
fast: proxies[0],
|
|
|
|
interval: interval,
|
|
|
|
done: make(chan struct{}),
|
2018-10-24 17:06:08 +08:00
|
|
|
once: 0,
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
go urlTest.loop()
|
|
|
|
return urlTest, nil
|
|
|
|
}
|