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"
|
2023-03-01 06:04:42 +00:00
|
|
|
"sync"
|
2022-05-09 10:56:36 +00:00
|
|
|
"time"
|
2019-02-15 06:25:20 +00:00
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2023-01-07 04:24:28 +00:00
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2023-02-24 01:54:54 +00:00
|
|
|
"github.com/Dreamacro/clash/common/callback"
|
2023-02-26 16:26:49 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2023-06-10 09:35:19 +00:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
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"
|
|
|
|
)
|
|
|
|
|
2023-03-01 06:04:42 +00:00
|
|
|
type strategyFn = func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy
|
2020-10-28 14:35:02 +00:00
|
|
|
|
2019-02-15 06:25:20 +00:00
|
|
|
type LoadBalance struct {
|
2022-04-28 11:01:13 +00:00
|
|
|
*GroupBase
|
2023-06-04 03:51:30 +00:00
|
|
|
disableUDP bool
|
|
|
|
strategyFn strategyFn
|
|
|
|
testUrl string
|
|
|
|
expectedStatus string
|
2020-10-28 14:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errStrategy = errors.New("unsupported strategy")
|
|
|
|
|
2022-03-16 04:10:13 +00:00
|
|
|
func parseStrategy(config map[string]any) string {
|
2022-07-04 13:36:33 +00:00
|
|
|
if strategy, ok := config["strategy"].(string); ok {
|
|
|
|
return strategy
|
2020-10-28 14:35:02 +00:00
|
|
|
}
|
|
|
|
return "consistent-hashing"
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getKey(metadata *C.Metadata) string {
|
2022-05-04 11:52:48 +00:00
|
|
|
if metadata == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-02-15 06:25:20 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
if !metadata.DstIP.IsValid() {
|
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
|
|
|
}
|
|
|
|
|
2022-05-17 05:28:54 +00:00
|
|
|
func getKeyWithSrcAndDst(metadata *C.Metadata) string {
|
|
|
|
dst := getKey(metadata)
|
|
|
|
src := ""
|
|
|
|
if metadata != nil {
|
|
|
|
src = metadata.SrcIP.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s", src, dst)
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-30 15:08:18 +00:00
|
|
|
proxy := lb.Unwrap(metadata, true)
|
2023-02-24 01:54:54 +00:00
|
|
|
c, err = proxy.DialContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
2022-10-16 05:12:49 +00:00
|
|
|
|
2023-02-24 01:54:54 +00:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(lb)
|
|
|
|
} else {
|
|
|
|
lb.onDialFailed(proxy.Type(), err)
|
|
|
|
}
|
2019-08-08 17:28:37 +00:00
|
|
|
|
2023-02-26 16:26:49 +00:00
|
|
|
if N.NeedHandshake(c) {
|
2023-04-01 12:56:49 +00:00
|
|
|
c = callback.NewFirstWriteCallBackConn(c, func(err error) {
|
|
|
|
if err == nil {
|
|
|
|
lb.onDialSuccess()
|
|
|
|
} else {
|
|
|
|
lb.onDialFailed(proxy.Type(), err)
|
|
|
|
}
|
|
|
|
})
|
2023-02-24 01:54:54 +00:00
|
|
|
}
|
2023-02-26 16:26:49 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-30 15:08:18 +00:00
|
|
|
proxy := lb.Unwrap(metadata, true)
|
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
|
|
|
}
|
|
|
|
|
2023-04-11 06:10:57 +00:00
|
|
|
// IsL3Protocol implements C.ProxyAdapter
|
|
|
|
func (lb *LoadBalance) IsL3Protocol(metadata *C.Metadata) bool {
|
|
|
|
return lb.Unwrap(metadata, false).IsL3Protocol(metadata)
|
|
|
|
}
|
|
|
|
|
2023-06-04 03:51:30 +00:00
|
|
|
func strategyRoundRobin(url string) strategyFn {
|
2020-10-28 14:35:02 +00:00
|
|
|
idx := 0
|
2023-03-01 06:04:42 +00:00
|
|
|
idxMutex := sync.Mutex{}
|
|
|
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
2023-03-10 04:47:01 +00:00
|
|
|
idxMutex.Lock()
|
|
|
|
defer idxMutex.Unlock()
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
length := len(proxies)
|
|
|
|
|
2023-03-01 06:04:42 +00:00
|
|
|
if touch {
|
|
|
|
defer func() {
|
2023-03-10 04:47:01 +00:00
|
|
|
idx = (idx + i) % length
|
2023-03-01 06:04:42 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-03-10 04:47:01 +00:00
|
|
|
for ; i < length; i++ {
|
|
|
|
id := (idx + i) % length
|
2023-03-01 06:04:42 +00:00
|
|
|
proxy := proxies[id]
|
2023-06-04 03:51:30 +00:00
|
|
|
// if proxy.Alive() {
|
|
|
|
if proxy.AliveForTestUrl(url) {
|
2023-03-10 04:47:01 +00:00
|
|
|
i++
|
2020-10-28 14:35:02 +00:00
|
|
|
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
|
|
|
|
2023-06-04 03:51:30 +00:00
|
|
|
func strategyConsistentHashing(url string) strategyFn {
|
2020-10-28 14:35:02 +00:00
|
|
|
maxRetry := 5
|
2023-03-01 06:04:42 +00:00
|
|
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
2023-06-10 09:35:19 +00:00
|
|
|
key := utils.MapHash(getKey(metadata))
|
2020-10-28 14:35:02 +00:00
|
|
|
buckets := int32(len(proxies))
|
|
|
|
for i := 0; i < maxRetry; i, key = i+1, key+1 {
|
|
|
|
idx := jumpHash(key, buckets)
|
|
|
|
proxy := proxies[idx]
|
2023-06-04 03:51:30 +00:00
|
|
|
// if proxy.Alive() {
|
|
|
|
if proxy.AliveForTestUrl(url) {
|
2020-10-28 14:35:02 +00:00
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 13:36:33 +00:00
|
|
|
// when availability is poor, traverse the entire list to get the available nodes
|
|
|
|
for _, proxy := range proxies {
|
2023-06-04 03:51:30 +00:00
|
|
|
// if proxy.Alive() {
|
|
|
|
if proxy.AliveForTestUrl(url) {
|
2022-07-04 13:36:33 +00:00
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:35:02 +00:00
|
|
|
return proxies[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 03:51:30 +00:00
|
|
|
func strategyStickySessions(url string) strategyFn {
|
2022-05-17 05:28:54 +00:00
|
|
|
ttl := time.Minute * 10
|
2022-05-19 12:27:26 +00:00
|
|
|
maxRetry := 5
|
2022-11-12 12:43:48 +00:00
|
|
|
lruCache := cache.New[uint64, int](
|
2022-05-18 14:29:27 +00:00
|
|
|
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
|
|
|
cache.WithSize[uint64, int](1000))
|
2023-03-01 06:04:42 +00:00
|
|
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
2023-06-10 09:35:19 +00:00
|
|
|
key := utils.MapHash(getKeyWithSrcAndDst(metadata))
|
2022-05-09 10:56:36 +00:00
|
|
|
length := len(proxies)
|
2022-05-18 14:29:27 +00:00
|
|
|
idx, has := lruCache.Get(key)
|
|
|
|
if !has {
|
2022-05-19 12:27:26 +00:00
|
|
|
idx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length)))
|
2022-05-09 10:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 12:27:26 +00:00
|
|
|
nowIdx := idx
|
|
|
|
for i := 1; i < maxRetry; i++ {
|
2022-05-17 05:28:54 +00:00
|
|
|
proxy := proxies[nowIdx]
|
2023-06-04 03:51:30 +00:00
|
|
|
// if proxy.Alive() {
|
|
|
|
if proxy.AliveForTestUrl(url) {
|
2022-05-17 05:28:54 +00:00
|
|
|
if nowIdx != idx {
|
2022-05-18 14:29:27 +00:00
|
|
|
lruCache.Delete(key)
|
|
|
|
lruCache.Set(key, nowIdx)
|
2022-05-17 05:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return proxy
|
2022-05-19 12:27:26 +00:00
|
|
|
} else {
|
|
|
|
nowIdx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length)))
|
2022-05-09 10:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 09:46:28 +00:00
|
|
|
|
2022-05-19 12:27:26 +00:00
|
|
|
lruCache.Delete(key)
|
|
|
|
lruCache.Set(key, 0)
|
2022-05-17 05:28:54 +00:00
|
|
|
return proxies[0]
|
2022-05-09 10:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// Unwrap implements C.ProxyAdapter
|
2022-10-30 15:08:18 +00:00
|
|
|
func (lb *LoadBalance) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|
|
|
proxies := lb.GetProxies(touch)
|
2023-03-09 03:09:36 +00:00
|
|
|
return lb.strategyFn(proxies, metadata, touch)
|
2019-04-23 15:29:36 +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) {
|
2022-05-09 10:56:36 +00:00
|
|
|
var all []string
|
2022-04-28 11:01:13 +00:00
|
|
|
for _, proxy := range lb.GetProxies(false) {
|
2019-02-15 06:25:20 +00:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
2022-03-16 04:10:13 +00:00
|
|
|
return json.Marshal(map[string]any{
|
2023-06-04 03:51:30 +00:00
|
|
|
"type": lb.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
"testUrl": lb.testUrl,
|
|
|
|
"expectedStatus": lb.expectedStatus,
|
2019-02-15 06:25:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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":
|
2023-06-04 03:51:30 +00:00
|
|
|
strategyFn = strategyConsistentHashing(option.URL)
|
2020-10-28 14:35:02 +00:00
|
|
|
case "round-robin":
|
2023-06-04 03:51:30 +00:00
|
|
|
strategyFn = strategyRoundRobin(option.URL)
|
2022-05-09 10:56:36 +00:00
|
|
|
case "sticky-sessions":
|
2023-06-04 03:51:30 +00:00
|
|
|
strategyFn = strategyStickySessions(option.URL)
|
2020-10-28 14:35:02 +00:00
|
|
|
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{
|
2022-04-28 11:01:13 +00:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.LoadBalance,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
|
|
|
option.Filter,
|
2022-11-09 00:06:37 +00:00
|
|
|
option.ExcludeFilter,
|
2023-01-07 04:24:28 +00:00
|
|
|
option.ExcludeType,
|
2022-04-28 11:01:13 +00:00
|
|
|
providers,
|
2021-11-07 08:48:51 +00:00
|
|
|
}),
|
2023-06-04 03:51:30 +00:00
|
|
|
strategyFn: strategyFn,
|
|
|
|
disableUDP: option.DisableUDP,
|
|
|
|
testUrl: option.URL,
|
|
|
|
expectedStatus: option.ExpectedStatus,
|
2020-10-28 14:35:02 +00:00
|
|
|
}, nil
|
2019-02-15 06:25:20 +00:00
|
|
|
}
|