2021-01-29 19:31:11 -05:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2024-03-10 07:00:06 -04:00
|
|
|
"context"
|
|
|
|
|
2024-03-13 15:32:51 -04:00
|
|
|
"google.golang.org/protobuf/runtime/protoiface"
|
|
|
|
|
2024-03-10 07:00:06 -04:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/observatory"
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/dice"
|
2024-03-10 07:00:06 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v5/features"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/extension"
|
2021-01-29 19:31:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// RandomStrategy represents a random balancing strategy
|
2024-03-10 07:00:06 -04:00
|
|
|
type RandomStrategy struct {
|
|
|
|
ctx context.Context
|
|
|
|
settings *StrategyRandomConfig
|
|
|
|
observatory extension.Observatory
|
|
|
|
}
|
2021-01-29 19:31:11 -05:00
|
|
|
|
2021-06-18 19:13:09 -04:00
|
|
|
func (s *RandomStrategy) GetPrincipleTarget(strings []string) []string {
|
|
|
|
return strings
|
|
|
|
}
|
|
|
|
|
2024-03-10 07:00:06 -04:00
|
|
|
// NewRandomStrategy creates a new RandomStrategy with settings
|
|
|
|
func NewRandomStrategy(settings *StrategyRandomConfig) *RandomStrategy {
|
|
|
|
return &RandomStrategy{
|
|
|
|
settings: settings,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RandomStrategy) InjectContext(ctx context.Context) {
|
|
|
|
if s != nil {
|
|
|
|
s.ctx = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:02:46 -04:00
|
|
|
func (s *RandomStrategy) PickOutbound(candidates []string) string {
|
2024-03-10 07:00:06 -04:00
|
|
|
if s != nil && s.settings.AliveOnly {
|
|
|
|
// candidates are considered alive unless observed otherwise
|
|
|
|
if s.observatory == nil {
|
|
|
|
core.RequireFeatures(s.ctx, func(observatory extension.Observatory) error {
|
|
|
|
s.observatory = observatory
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if s.observatory != nil {
|
|
|
|
var observeReport protoiface.MessageV1
|
|
|
|
var err error
|
|
|
|
if s.settings.ObserverTag == "" {
|
|
|
|
observeReport, err = s.observatory.GetObservation(s.ctx)
|
|
|
|
} else {
|
|
|
|
observeReport, err = common.Must2(s.observatory.(features.TaggedFeatures).GetFeaturesByTag(s.settings.ObserverTag)).(extension.Observatory).GetObservation(s.ctx)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
aliveTags := make([]string, 0)
|
|
|
|
if result, ok := observeReport.(*observatory.ObservationResult); ok {
|
|
|
|
status := result.Status
|
|
|
|
statusMap := make(map[string]*observatory.OutboundStatus)
|
|
|
|
for _, outboundStatus := range status {
|
|
|
|
statusMap[outboundStatus.OutboundTag] = outboundStatus
|
|
|
|
}
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
if outboundStatus, found := statusMap[candidate]; found {
|
|
|
|
if outboundStatus.Alive {
|
|
|
|
aliveTags = append(aliveTags, candidate)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// unfound candidate is considered alive
|
|
|
|
aliveTags = append(aliveTags, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates = aliveTags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
count := len(candidates)
|
|
|
|
if count == 0 {
|
|
|
|
// goes to fallbackTag
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return candidates[dice.Roll(count)]
|
|
|
|
}
|
2021-09-07 06:48:24 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*StrategyRandomConfig)(nil), nil))
|
|
|
|
}
|