mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-04 00:05:21 -05:00
incorporate changes in router implementation
This commit is contained in:
@@ -8,16 +8,19 @@ import (
|
||||
|
||||
"github.com/v2fly/v2ray-core/v4/features/extension"
|
||||
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
||||
"github.com/v2fly/v2ray-core/v4/features/routing"
|
||||
)
|
||||
|
||||
type BalancingStrategy interface {
|
||||
PickOutbound([]string) string
|
||||
}
|
||||
|
||||
type BalancingPrincipleTarget interface {
|
||||
GetPrincipleTarget([]string) []string
|
||||
}
|
||||
|
||||
type Balancer struct {
|
||||
selectors []string
|
||||
strategy routing.BalancingStrategy
|
||||
strategy BalancingStrategy
|
||||
ohm outbound.Manager
|
||||
fallbackTag string
|
||||
|
||||
@@ -35,10 +38,10 @@ func (b *Balancer) PickOutbound() (string, error) {
|
||||
return "", err
|
||||
}
|
||||
var tag string
|
||||
if o := b.override.Get(); o != nil {
|
||||
tag = b.strategy.Pick(o.selects)
|
||||
if o := b.override.Get(); o != "" {
|
||||
tag = o
|
||||
} else {
|
||||
tag = b.strategy.SelectAndPick(candidates)
|
||||
tag = b.strategy.PickOutbound(candidates)
|
||||
}
|
||||
if tag == "" {
|
||||
if b.fallbackTag != "" {
|
||||
@@ -66,3 +69,35 @@ func (b *Balancer) SelectOutbounds() ([]string, error) {
|
||||
tags := hs.Select(b.selectors)
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// GetPrincipleTarget implements routing.BalancerPrincipleTarget
|
||||
func (r *Router) GetPrincipleTarget(tag string) ([]string, error) {
|
||||
if b, ok := r.balancers[tag]; ok {
|
||||
if s, ok := b.strategy.(BalancingPrincipleTarget); ok {
|
||||
candidates, err := b.SelectOutbounds()
|
||||
if err != nil {
|
||||
return nil, newError("unable to select outbounds").Base(err)
|
||||
}
|
||||
return s.GetPrincipleTarget(candidates), nil
|
||||
}
|
||||
return nil, newError("unsupported GetPrincipleTarget")
|
||||
}
|
||||
return nil, newError("cannot find tag")
|
||||
}
|
||||
|
||||
// SetOverrideTarget implements routing.BalancerOverrider
|
||||
func (r *Router) SetOverrideTarget(tag, target string) error {
|
||||
if b, ok := r.balancers[tag]; ok {
|
||||
b.override.Put(target)
|
||||
return nil
|
||||
}
|
||||
return newError("cannot find tag")
|
||||
}
|
||||
|
||||
// GetOverrideTarget implements routing.BalancerOverrider
|
||||
func (r *Router) GetOverrideTarget(tag string) (string, error) {
|
||||
if b, ok := r.balancers[tag]; ok {
|
||||
return b.override.Get(), nil
|
||||
}
|
||||
return "", newError("cannot find tag")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user