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"
|
2020-10-28 14:35:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-02-15 06:25:20 +00:00
|
|
|
"net"
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
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"
|
2021-11-07 08:48:51 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2019-02-15 06:25:20 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 12:32:59 +00:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2019-02-15 06:25:20 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
)
|
|
|
|
|
2020-10-28 14:35:02 +00:00
|
|
|
type strategyFn = func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy
|
|
|
|
|
2019-02-15 06:25:20 +00:00
|
|
|
type LoadBalance struct {
|
2019-12-08 04:17:24 +00:00
|
|
|
*outbound.Base
|
2020-11-13 13:48:52 +00:00
|
|
|
disableUDP bool
|
2020-10-28 14:35:02 +00:00
|
|
|
single *singledo.Single
|
|
|
|
providers []provider.ProxyProvider
|
|
|
|
strategyFn strategyFn
|
|
|
|
}
|
|
|
|
|
|
|
|
var errStrategy = errors.New("unsupported strategy")
|
|
|
|
|
|
|
|
func parseStrategy(config map[string]interface{}) string {
|
|
|
|
if elm, ok := config["strategy"]; ok {
|
|
|
|
if strategy, ok := elm.(string); ok {
|
|
|
|
return strategy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "consistent-hashing"
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
2019-08-08 17:28:37 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(lb)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-07 13:42:52 +00:00
|
|
|
proxy := lb.Unwrap(metadata)
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
c, err = proxy.DialContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
2019-08-08 17:28:37 +00:00
|
|
|
return
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (pc C.PacketConn, err error) {
|
2019-08-08 17:28:37 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(lb)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-07 13:42:52 +00:00
|
|
|
proxy := lb.Unwrap(metadata)
|
2021-11-07 08:48:51 +00:00
|
|
|
return proxy.ListenPacketContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
2020-05-07 13:42:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// SupportUDP implements C.ProxyAdapter
|
2020-05-07 13:42:52 +00:00
|
|
|
func (lb *LoadBalance) SupportUDP() bool {
|
2020-11-13 13:48:52 +00:00
|
|
|
return !lb.disableUDP
|
2020-05-07 13:42:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:35:02 +00:00
|
|
|
func strategyRoundRobin() strategyFn {
|
|
|
|
idx := 0
|
|
|
|
return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy {
|
|
|
|
length := len(proxies)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
idx = (idx + 1) % length
|
|
|
|
proxy := proxies[idx]
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy
|
|
|
|
}
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
2020-10-28 14:35:02 +00:00
|
|
|
|
|
|
|
return proxies[0]
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
2020-10-28 14:35:02 +00:00
|
|
|
}
|
2019-04-23 15:29:36 +00:00
|
|
|
|
2020-10-28 14:35:02 +00:00
|
|
|
func strategyConsistentHashing() strategyFn {
|
|
|
|
maxRetry := 5
|
|
|
|
return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy {
|
|
|
|
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
|
|
|
|
buckets := int32(len(proxies))
|
|
|
|
for i := 0; i < maxRetry; i, key = i+1, key+1 {
|
|
|
|
idx := jumpHash(key, buckets)
|
|
|
|
proxy := proxies[idx]
|
|
|
|
if proxy.Alive() {
|
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// Unwrap implements C.ProxyAdapter
|
2020-10-28 14:35:02 +00:00
|
|
|
func (lb *LoadBalance) Unwrap(metadata *C.Metadata) C.Proxy {
|
2020-11-18 16:53:22 +00:00
|
|
|
proxies := lb.proxies(true)
|
2020-10-28 14:35:02 +00:00
|
|
|
return lb.strategyFn(proxies, metadata)
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
func (lb *LoadBalance) proxies(touch bool) []C.Proxy {
|
2019-12-10 07:04:22 +00:00
|
|
|
elm, _, _ := lb.single.Do(func() (interface{}, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
return getProvidersProxies(lb.providers, touch), nil
|
2019-12-10 07:04:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
2019-03-28 11:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
2019-02-15 06:25:20 +00:00
|
|
|
func (lb *LoadBalance) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
2020-11-18 16:53:22 +00:00
|
|
|
for _, proxy := range lb.proxies(false) {
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvider, strategy string) (lb *LoadBalance, err error) {
|
2020-10-28 14:35:02 +00:00
|
|
|
var strategyFn strategyFn
|
|
|
|
switch strategy {
|
|
|
|
case "consistent-hashing":
|
|
|
|
strategyFn = strategyConsistentHashing()
|
|
|
|
case "round-robin":
|
|
|
|
strategyFn = strategyRoundRobin()
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: %s", errStrategy, strategy)
|
2019-03-28 11:00:41 +00:00
|
|
|
}
|
2020-10-28 14:35:02 +00:00
|
|
|
return &LoadBalance{
|
2021-11-07 08:48:51 +00:00
|
|
|
Base: outbound.NewBase(outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.LoadBalance,
|
|
|
|
Interface: option.Interface,
|
|
|
|
}),
|
2020-10-28 14:35:02 +00:00
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
|
|
providers: providers,
|
|
|
|
strategyFn: strategyFn,
|
2021-11-07 08:48:51 +00:00
|
|
|
disableUDP: option.DisableUDP,
|
2020-10-28 14:35:02 +00:00
|
|
|
}, nil
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|