2019-12-08 04:17:24 +00:00
|
|
|
package outboundgroup
|
2019-02-15 06:25:20 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-02 11:18:03 +00:00
|
|
|
"context"
|
2019-02-15 06:25:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
"github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
"github.com/Dreamacro/clash/adapters/provider"
|
2019-02-15 06:25:20 +00:00
|
|
|
"github.com/Dreamacro/clash/common/murmur3"
|
2019-12-10 07:04:22 +00:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
2019-02-15 06:25:20 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoadBalance struct {
|
2019-12-08 04:17:24 +00:00
|
|
|
*outbound.Base
|
2019-12-10 07:04:22 +00:00
|
|
|
single *singledo.Single
|
2019-12-08 04:17:24 +00:00
|
|
|
maxRetry int
|
|
|
|
providers []provider.ProxyProvider
|
2019-02-15 06:25:20 +00: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 13:00:29 +00:00
|
|
|
if metadata.DstIP == nil {
|
2019-02-15 06:25:20 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-05-09 13:00:29 +00:00
|
|
|
return metadata.DstIP.String()
|
2019-02-15 06:25:20 +00: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 15:55:39 +00:00
|
|
|
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
2019-08-08 17:28:37 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(lb)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-15 06:25:20 +00:00
|
|
|
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
|
2019-12-08 04:17:24 +00:00
|
|
|
proxies := lb.proxies()
|
|
|
|
buckets := int32(len(proxies))
|
2019-03-15 16:43:16 +00:00
|
|
|
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {
|
2019-02-15 06:25:20 +00:00
|
|
|
idx := jumpHash(key, buckets)
|
2019-12-08 04:17:24 +00:00
|
|
|
proxy := proxies[idx]
|
2019-03-15 16:43:16 +00:00
|
|
|
if proxy.Alive() {
|
2019-10-12 15:55:39 +00:00
|
|
|
c, err = proxy.DialContext(ctx, metadata)
|
2019-08-08 17:28:37 +00:00
|
|
|
return
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 04:17:24 +00:00
|
|
|
c, err = proxies[0].DialContext(ctx, metadata)
|
2019-08-08 17:28:37 +00:00
|
|
|
return
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:28:37 +00: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 15:29:36 +00:00
|
|
|
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
|
2019-12-08 04:17:24 +00:00
|
|
|
proxies := lb.proxies()
|
|
|
|
buckets := int32(len(proxies))
|
2019-04-23 15:29:36 +00:00
|
|
|
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {
|
|
|
|
idx := jumpHash(key, buckets)
|
2019-12-08 04:17:24 +00:00
|
|
|
proxy := proxies[idx]
|
2019-04-23 15:29:36 +00:00
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy.DialUDP(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
return proxies[0].DialUDP(metadata)
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lb *LoadBalance) SupportUDP() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
func (lb *LoadBalance) proxies() []C.Proxy {
|
2019-12-10 07:04:22 +00:00
|
|
|
elm, _, _ := lb.single.Do(func() (interface{}, error) {
|
|
|
|
return getProvidersProxies(lb.providers), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
2019-03-28 11:00:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 06:25:20 +00:00
|
|
|
func (lb *LoadBalance) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
2019-12-08 04:17:24 +00:00
|
|
|
for _, proxy := range lb.proxies() {
|
2019-02-15 06:25:20 +00:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": lb.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
func NewLoadBalance(name string, providers []provider.ProxyProvider) *LoadBalance {
|
|
|
|
return &LoadBalance{
|
|
|
|
Base: outbound.NewBase(name, C.LoadBalance, false),
|
2019-12-10 07:04:22 +00:00
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
2019-12-08 04:17:24 +00:00
|
|
|
maxRetry: 3,
|
|
|
|
providers: providers,
|
2019-03-28 11:00:41 +00:00
|
|
|
}
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|