2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2021-04-08 17:04:08 -04:00
|
|
|
// +build !confonly
|
|
|
|
|
2021-04-08 15:55:25 -04:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-08 17:04:08 -04:00
|
|
|
|
2021-04-08 15:55:25 -04:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
2021-04-08 16:34:52 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/observatory"
|
2021-04-30 23:15:39 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
2021-04-08 15:55:25 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/extension"
|
2021-06-04 19:42:53 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/routing"
|
2021-04-08 15:55:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type LeastPingStrategy struct {
|
|
|
|
ctx context.Context
|
|
|
|
observatory extension.Observatory
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:31:11 -05:00
|
|
|
// TODO Fix PlaceHolder
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) Pick(candidates []string) string {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) SelectAndPick(candidates []string) string {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) GetInformation(tags []string) *routing.StrategyInfo {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:55:25 -04:00
|
|
|
func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
|
|
|
|
l.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LeastPingStrategy) PickOutbound(strings []string) string {
|
2021-04-08 16:34:52 -04:00
|
|
|
if l.observatory == nil {
|
|
|
|
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
|
|
|
|
l.observatory = observatory
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-04-08 15:55:25 -04: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-05-25 09:48:13 -04:00
|
|
|
leastPing = v.Delay
|
2021-04-08 15:55:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return selectedOutboundName
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:06:48 -04:00
|
|
|
// No way to understand observeReport
|
2021-04-08 15:55:25 -04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type outboundList []string
|
|
|
|
|
|
|
|
func (o outboundList) contains(name string) bool {
|
|
|
|
for _, v := range o {
|
|
|
|
if v == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|