github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/v4/observatory.go (about)

     1  package v4
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  	"google.golang.org/protobuf/types/known/anypb"
     8  
     9  	"github.com/v2fly/v2ray-core/v5/app/observatory"
    10  	"github.com/v2fly/v2ray-core/v5/app/observatory/burst"
    11  	"github.com/v2fly/v2ray-core/v5/app/observatory/multiobservatory"
    12  	"github.com/v2fly/v2ray-core/v5/common/serial"
    13  	"github.com/v2fly/v2ray-core/v5/common/taggedfeatures"
    14  	"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/duration"
    15  	"github.com/v2fly/v2ray-core/v5/infra/conf/synthetic/router"
    16  )
    17  
    18  type ObservatoryConfig struct {
    19  	SubjectSelector []string          `json:"subjectSelector"`
    20  	ProbeURL        string            `json:"probeURL"`
    21  	ProbeInterval   duration.Duration `json:"probeInterval"`
    22  }
    23  
    24  func (o *ObservatoryConfig) Build() (proto.Message, error) {
    25  	return &observatory.Config{SubjectSelector: o.SubjectSelector, ProbeUrl: o.ProbeURL, ProbeInterval: int64(o.ProbeInterval)}, nil
    26  }
    27  
    28  type BurstObservatoryConfig struct {
    29  	SubjectSelector []string `json:"subjectSelector"`
    30  	// health check settings
    31  	HealthCheck *router.HealthCheckSettings `json:"pingConfig,omitempty"`
    32  }
    33  
    34  func (b BurstObservatoryConfig) Build() (proto.Message, error) {
    35  	result, err := b.HealthCheck.Build()
    36  	if err == nil {
    37  		return &burst.Config{SubjectSelector: b.SubjectSelector, PingConfig: result.(*burst.HealthPingConfig)}, nil
    38  	}
    39  	return nil, err
    40  }
    41  
    42  type MultiObservatoryItem struct {
    43  	MemberType string          `json:"type"`
    44  	Tag        string          `json:"tag"`
    45  	Value      json.RawMessage `json:"settings"`
    46  }
    47  
    48  type MultiObservatoryConfig struct {
    49  	Observers []MultiObservatoryItem `json:"observers"`
    50  }
    51  
    52  func (o *MultiObservatoryConfig) Build() (proto.Message, error) {
    53  	ret := &multiobservatory.Config{Holders: &taggedfeatures.Config{Features: make(map[string]*anypb.Any)}}
    54  	for _, v := range o.Observers {
    55  		switch v.MemberType {
    56  		case "burst":
    57  			var burstObservatoryConfig BurstObservatoryConfig
    58  			err := json.Unmarshal(v.Value, &burstObservatoryConfig)
    59  			if err != nil {
    60  				return nil, err
    61  			}
    62  			burstObservatoryConfigPb, err := burstObservatoryConfig.Build()
    63  			if err != nil {
    64  				return nil, err
    65  			}
    66  			ret.Holders.Features[v.Tag] = serial.ToTypedMessage(burstObservatoryConfigPb)
    67  		case "default":
    68  			fallthrough
    69  		default:
    70  			var observatoryConfig ObservatoryConfig
    71  			err := json.Unmarshal(v.Value, &observatoryConfig)
    72  			if err != nil {
    73  				return nil, err
    74  			}
    75  			observatoryConfigPb, err := observatoryConfig.Build()
    76  			if err != nil {
    77  				return nil, err
    78  			}
    79  			ret.Holders.Features[v.Tag] = serial.ToTypedMessage(observatoryConfigPb)
    80  		}
    81  	}
    82  	return ret, nil
    83  }