From 2cdad52334eadc175ba0f0396bf6fdb025d22f60 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Sat, 19 Jun 2021 00:29:50 +0100 Subject: [PATCH] add v4 json support for BurstObservatory & fix balancer reference --- infra/conf/observatory.go | 15 +++++++++++++++ infra/conf/router_strategy.go | 26 ++++++++++++-------------- infra/conf/v2ray.go | 9 +++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/infra/conf/observatory.go b/infra/conf/observatory.go index b8ca866ed..42c964aae 100644 --- a/infra/conf/observatory.go +++ b/infra/conf/observatory.go @@ -2,6 +2,7 @@ package conf import ( "github.com/golang/protobuf/proto" + "github.com/v2fly/v2ray-core/v4/app/observatory/burst" "github.com/v2fly/v2ray-core/v4/app/observatory" "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration" @@ -16,3 +17,17 @@ type ObservatoryConfig struct { func (o *ObservatoryConfig) Build() (proto.Message, error) { return &observatory.Config{SubjectSelector: o.SubjectSelector, ProbeUrl: o.ProbeURL, ProbeInterval: int64(o.ProbeInterval)}, nil } + +type BurstObservatoryConfig struct { + SubjectSelector []string `json:"subjectSelector"` + // health check settings + HealthCheck *healthCheckSettings `json:"pingConfig,omitempty"` +} + +func (b BurstObservatoryConfig) Build() (proto.Message, error) { + if result, err := b.HealthCheck.Build(); err != nil { + return &burst.Config{SubjectSelector: b.SubjectSelector, PingConfig: result.(*burst.HealthPingConfig)}, nil + } else { + return nil, err + } +} diff --git a/infra/conf/router_strategy.go b/infra/conf/router_strategy.go index 05b1d67f4..eb9b5b32e 100644 --- a/infra/conf/router_strategy.go +++ b/infra/conf/router_strategy.go @@ -2,6 +2,7 @@ package conf import ( "github.com/golang/protobuf/proto" + "github.com/v2fly/v2ray-core/v4/app/observatory/burst" "github.com/v2fly/v2ray-core/v4/app/router" ) @@ -27,8 +28,6 @@ func (v *strategyEmptyConfig) Build() (proto.Message, error) { } type strategyLeastLoadConfig struct { - // health check settings - HealthCheck *healthCheckSettings `json:"healthCheck,omitempty"` // weight settings Costs []*router.StrategyWeight `json:"costs,omitempty"` // ping rtt baselines @@ -50,20 +49,19 @@ type healthCheckSettings struct { Timeout Duration `json:"timeout"` } +func (h healthCheckSettings) Build() (proto.Message, error) { + return &burst.HealthPingConfig{ + Destination: h.Destination, + Connectivity: h.Connectivity, + Interval: int64(h.Interval), + Timeout: int64(h.Timeout), + SamplingCount: int32(h.SamplingCount), + }, nil +} + // Build implements Buildable. func (v *strategyLeastLoadConfig) Build() (proto.Message, error) { - config := &router.StrategyLeastLoadConfig{ - HealthCheck: &router.HealthPingConfig{}, - } - if v.HealthCheck != nil { - config.HealthCheck = &router.HealthPingConfig{ - Destination: v.HealthCheck.Destination, - Connectivity: v.HealthCheck.Connectivity, - Interval: int64(v.HealthCheck.Interval), - Timeout: int64(v.HealthCheck.Timeout), - SamplingCount: int32(v.HealthCheck.SamplingCount), - } - } + config := &router.StrategyLeastLoadConfig{} config.Costs = v.Costs config.Tolerance = float32(v.Tolerance) if config.Tolerance < 0 { diff --git a/infra/conf/v2ray.go b/infra/conf/v2ray.go index 2708aed04..d3924b8aa 100644 --- a/infra/conf/v2ray.go +++ b/infra/conf/v2ray.go @@ -351,6 +351,7 @@ type Config struct { FakeDNS *FakeDNSConfig `json:"fakeDns"` BrowserForwarder *BrowserForwarderConfig `json:"browserForwarder"` Observatory *ObservatoryConfig `json:"observatory"` + BurstObservatory *BurstObservatoryConfig `json:"burstObservatory"` Services map[string]*json.RawMessage `json:"services"` } @@ -491,6 +492,14 @@ func (c *Config) Build() (*core.Config, error) { config.App = append(config.App, serial.ToTypedMessage(r)) } + if c.BurstObservatory != nil { + r, err := c.BurstObservatory.Build() + if err != nil { + return nil, err + } + config.App = append(config.App, serial.ToTypedMessage(r)) + } + // Load Additional Services that do not have a json translator if msg, err := c.BuildServices(c.Services); err != nil {