Files
Xray-core/app/router/strategy_leastping.go
T

68 lines
1.6 KiB
Go
Raw Normal View History

package router
import (
"context"
"github.com/xtls/xray-core/app/observatory"
"github.com/xtls/xray-core/common"
2024-06-29 14:32:57 -04:00
"github.com/xtls/xray-core/common/errors"
2021-12-14 19:28:47 -05:00
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/extension"
)
type LeastPingStrategy struct {
ctx context.Context
observatory extension.Observatory
}
2024-02-17 22:51:37 -05:00
func (l *LeastPingStrategy) GetPrincipleTarget(strings []string) []string {
return []string{l.PickOutbound(strings)}
}
func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
l.ctx = ctx
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
l.observatory = observatory
return nil
}))
}
func (l *LeastPingStrategy) PickOutbound(strings []string) string {
2021-11-13 23:23:38 -05:00
if l.observatory == nil {
errors.LogError(l.ctx, "observer is nil")
return ""
2021-11-13 23:23:38 -05:00
}
observeReport, err := l.observatory.GetObservation(l.ctx)
if err != nil {
errors.LogInfoInner(l.ctx, err, "cannot get observer report")
return ""
}
outboundsList := outboundList(strings)
if result, ok := observeReport.(*observatory.ObservationResult); ok {
status := result.Status
leastPing := int64(99999999)
selectedOutboundName := ""
for _, v := range status {
if outboundsList.contains(v.OutboundTag) && v.Alive && v.Delay < leastPing {
selectedOutboundName = v.OutboundTag
2021-10-26 01:00:31 -04:00
leastPing = v.Delay
}
}
return selectedOutboundName
}
2022-05-18 15:29:01 +08:00
// No way to understand observeReport
return ""
}
type outboundList []string
func (o outboundList) contains(name string) bool {
for _, v := range o {
if v == name {
return true
}
}
return false
}