2021-06-19 08:45:43 -04:00
|
|
|
package v4
|
2021-04-08 17:20:30 -04:00
|
|
|
|
|
|
|
import (
|
2021-06-19 06:15:48 -04:00
|
|
|
"encoding/json"
|
2021-04-08 17:20:30 -04:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-06-18 19:29:50 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/observatory/burst"
|
2021-06-19 06:15:48 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/observatory/multiObservatory"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2021-06-19 07:34:58 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/taggedfeatures"
|
2021-06-19 08:28:20 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/router"
|
2021-06-19 09:36:54 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2021-07-01 14:02:27 -04:00
|
|
|
|
2021-04-08 17:20:30 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/observatory"
|
2021-08-09 23:08:45 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration"
|
2021-04-08 17:20:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ObservatoryConfig struct {
|
2021-06-30 14:33:37 -04:00
|
|
|
SubjectSelector []string `json:"subjectSelector"`
|
|
|
|
ProbeURL string `json:"probeURL"`
|
2021-08-19 01:40:47 -04:00
|
|
|
ProbeInterval duration.Duration `json:"probeInterval"`
|
2021-04-08 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
2021-05-01 10:28:23 -04:00
|
|
|
func (o *ObservatoryConfig) Build() (proto.Message, error) {
|
2021-06-30 14:33:37 -04:00
|
|
|
return &observatory.Config{SubjectSelector: o.SubjectSelector, ProbeUrl: o.ProbeURL, ProbeInterval: int64(o.ProbeInterval)}, nil
|
2021-04-08 17:20:30 -04:00
|
|
|
}
|
2021-06-18 19:29:50 -04:00
|
|
|
|
|
|
|
type BurstObservatoryConfig struct {
|
|
|
|
SubjectSelector []string `json:"subjectSelector"`
|
|
|
|
// health check settings
|
2021-06-19 08:28:20 -04:00
|
|
|
HealthCheck *router.HealthCheckSettings `json:"pingConfig,omitempty"`
|
2021-06-18 19:29:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BurstObservatoryConfig) Build() (proto.Message, error) {
|
2021-06-19 07:19:21 -04:00
|
|
|
if result, err := b.HealthCheck.Build(); err == nil {
|
2021-06-18 19:29:50 -04:00
|
|
|
return &burst.Config{SubjectSelector: b.SubjectSelector, PingConfig: result.(*burst.HealthPingConfig)}, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-06-19 06:15:48 -04:00
|
|
|
|
|
|
|
type MultiObservatoryItem struct {
|
|
|
|
MemberType string `json:"type"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Value json.RawMessage `json:"settings"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiObservatoryConfig struct {
|
|
|
|
Observers []MultiObservatoryItem `json:"observers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MultiObservatoryConfig) Build() (proto.Message, error) {
|
2021-06-19 09:36:54 -04:00
|
|
|
ret := &multiObservatory.Config{Holders: &taggedfeatures.Config{Features: make(map[string]*anypb.Any)}}
|
2021-06-19 06:15:48 -04:00
|
|
|
for _, v := range o.Observers {
|
|
|
|
switch v.MemberType {
|
|
|
|
case "burst":
|
|
|
|
var burstObservatoryConfig BurstObservatoryConfig
|
|
|
|
err := json.Unmarshal(v.Value, &burstObservatoryConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
burstObservatoryConfigPb, err := burstObservatoryConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.Holders.Features[v.Tag] = serial.ToTypedMessage(burstObservatoryConfigPb)
|
|
|
|
break
|
|
|
|
case "default":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
var observatoryConfig ObservatoryConfig
|
|
|
|
err := json.Unmarshal(v.Value, &observatoryConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
observatoryConfigPb, err := observatoryConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.Holders.Features[v.Tag] = serial.ToTypedMessage(observatoryConfigPb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|