1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-09-25 11:44:02 -04:00
Files
v2fly/app/router/strategy_leastping.go
Xiaokang Wang (Shelikhoo) 69b09074f8 Add WebUI Support for V2Ray (#3284)
* Add support for web based grpc

* Avoid panic in observatory command: incorrect tag

* Add WebCommander app for web based client

* Add WebCommander app for web based client

* Add urlline subscription container format support

* avoid panic when serving user request

* refine subscription info api

* fix start consistency issue in web commander

* fix webcommander import style

* fix urlline parsing

* fix crash in subscription commander

* add webroot file serving at webcommmander

* fix crash when observatory is not found

* add autogenerated subscriptionmgr err
2025-01-17 17:42:53 +00:00

85 lines
2.0 KiB
Go

//go:build !confonly
// +build !confonly
package router
import (
"context"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/observatory"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/features"
"github.com/v2fly/v2ray-core/v5/features/extension"
)
type LeastPingStrategy struct {
ctx context.Context
observatory extension.Observatory
config *StrategyLeastPingConfig
}
func (l *LeastPingStrategy) GetPrincipleTarget(strings []string) []string {
return []string{l.PickOutbound(strings)}
}
func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
l.ctx = ctx
}
func (l *LeastPingStrategy) PickOutbound(strings []string) string {
if l.observatory == nil {
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
if l.config.ObserverTag != "" {
l.observatory = common.Must2(observatory.(features.TaggedFeatures).GetFeaturesByTag(l.config.ObserverTag)).(extension.Observatory)
} else {
l.observatory = observatory
}
return nil
}))
}
if l.observatory == nil {
newError("cannot find observatory").WriteToLog()
return ""
}
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
leastPing = v.Delay
}
}
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
}
func init() {
common.Must(common.RegisterConfig((*StrategyLeastPingConfig)(nil), nil))
}