2021-04-09 00:20:30 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/app/observatory"
|
|
|
|
"github.com/xtls/xray-core/common"
|
2021-12-15 02:28:47 +02:00
|
|
|
"github.com/xtls/xray-core/core"
|
2021-04-09 00:20:30 +03:00
|
|
|
"github.com/xtls/xray-core/features/extension"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LeastPingStrategy struct {
|
|
|
|
ctx context.Context
|
|
|
|
observatory extension.Observatory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
|
|
|
|
l.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) PickOutbound(strings []string) string {
|
2021-11-14 06:23:38 +02:00
|
|
|
if l.observatory == nil {
|
|
|
|
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
|
|
|
|
l.observatory = observatory
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-04-09 00:20:30 +03:00
|
|
|
observeReport, err := l.observatory.GetObservation(l.ctx)
|
|
|
|
if err != nil {
|
|
|
|
newError("cannot get observe report").Base(err).WriteToLog()
|
|
|
|
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 08:00:31 +03:00
|
|
|
leastPing = v.Delay
|
2021-04-09 00:20:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return selectedOutboundName
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
|
}
|