2019-02-15 14:25:20 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2019-07-02 19:18:03 +08:00
|
|
|
"context"
|
2019-02-15 14:25:20 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2019-03-28 19:00:41 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-02-15 14:25:20 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/murmur3"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoadBalance struct {
|
|
|
|
*Base
|
|
|
|
proxies []C.Proxy
|
|
|
|
maxRetry int
|
2019-03-28 19:00:41 +08:00
|
|
|
rawURL string
|
|
|
|
interval time.Duration
|
|
|
|
done chan struct{}
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getKey(metadata *C.Metadata) string {
|
|
|
|
if metadata.Host != "" {
|
|
|
|
// ip host
|
|
|
|
if ip := net.ParseIP(metadata.Host); ip != nil {
|
|
|
|
return metadata.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
if etld, err := publicsuffix.EffectiveTLDPlusOne(metadata.Host); err == nil {
|
|
|
|
return etld
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:00:29 +08:00
|
|
|
if metadata.DstIP == nil {
|
2019-02-15 14:25:20 +08:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:00:29 +08:00
|
|
|
return metadata.DstIP.String()
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func jumpHash(key uint64, buckets int32) int32 {
|
|
|
|
var b, j int64
|
|
|
|
|
|
|
|
for j < int64(buckets) {
|
|
|
|
b = j
|
|
|
|
key = key*2862933555777941757 + 1
|
|
|
|
j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return int32(b)
|
|
|
|
}
|
|
|
|
|
2019-10-12 23:55:39 +08:00
|
|
|
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
2019-08-09 01:28:37 +08:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(lb)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-15 14:25:20 +08:00
|
|
|
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
|
|
|
|
buckets := int32(len(lb.proxies))
|
2019-03-16 00:43:16 +08:00
|
|
|
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {
|
2019-02-15 14:25:20 +08:00
|
|
|
idx := jumpHash(key, buckets)
|
2019-03-16 00:43:16 +08:00
|
|
|
proxy := lb.proxies[idx]
|
|
|
|
if proxy.Alive() {
|
2019-10-12 23:55:39 +08:00
|
|
|
c, err = proxy.DialContext(ctx, metadata)
|
2019-08-09 01:28:37 +08:00
|
|
|
return
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 23:55:39 +08:00
|
|
|
c, err = lb.proxies[0].DialContext(ctx, metadata)
|
2019-08-09 01:28:37 +08:00
|
|
|
return
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (lb *LoadBalance) DialUDP(metadata *C.Metadata) (pc C.PacketConn, addr net.Addr, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(lb)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-23 23:29:36 +08:00
|
|
|
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
|
|
|
|
buckets := int32(len(lb.proxies))
|
|
|
|
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {
|
|
|
|
idx := jumpHash(key, buckets)
|
|
|
|
proxy := lb.proxies[idx]
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy.DialUDP(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lb.proxies[0].DialUDP(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalance) SupportUDP() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:00:41 +08:00
|
|
|
func (lb *LoadBalance) Destroy() {
|
|
|
|
lb.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalance) validTest() {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(lb.proxies))
|
|
|
|
|
|
|
|
for _, p := range lb.proxies {
|
|
|
|
go func(p C.Proxy) {
|
2019-07-02 19:18:03 +08:00
|
|
|
p.URLTest(context.Background(), lb.rawURL)
|
2019-03-28 19:00:41 +08:00
|
|
|
wg.Done()
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalance) loop() {
|
|
|
|
tick := time.NewTicker(lb.interval)
|
|
|
|
go lb.validTest()
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
go lb.validTest()
|
|
|
|
case <-lb.done:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:25:20 +08:00
|
|
|
func (lb *LoadBalance) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
|
|
|
for _, proxy := range lb.proxies {
|
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": lb.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoadBalanceOption struct {
|
2019-03-28 19:00:41 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Proxies []string `proxy:"proxies"`
|
|
|
|
URL string `proxy:"url"`
|
|
|
|
Interval int `proxy:"interval"`
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:00:41 +08:00
|
|
|
func NewLoadBalance(option LoadBalanceOption, proxies []C.Proxy) (*LoadBalance, error) {
|
2019-02-15 14:25:20 +08:00
|
|
|
if len(proxies) == 0 {
|
|
|
|
return nil, errors.New("Provide at least one proxy")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:00:41 +08:00
|
|
|
interval := time.Duration(option.Interval) * time.Second
|
|
|
|
|
|
|
|
lb := &LoadBalance{
|
2019-02-15 14:25:20 +08:00
|
|
|
Base: &Base{
|
2019-03-28 19:00:41 +08:00
|
|
|
name: option.Name,
|
2019-02-15 14:25:20 +08:00
|
|
|
tp: C.LoadBalance,
|
|
|
|
},
|
|
|
|
proxies: proxies,
|
|
|
|
maxRetry: 3,
|
2019-03-28 19:00:41 +08:00
|
|
|
rawURL: option.URL,
|
|
|
|
interval: interval,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
go lb.loop()
|
|
|
|
return lb, nil
|
2019-02-15 14:25:20 +08:00
|
|
|
}
|