multi.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package multiobservatory
  2. import (
  3. "context"
  4. "github.com/golang/protobuf/jsonpb"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/v2fly/v2ray-core/v4/common"
  7. "github.com/v2fly/v2ray-core/v4/common/taggedfeatures"
  8. "github.com/v2fly/v2ray-core/v4/features"
  9. "github.com/v2fly/v2ray-core/v4/features/extension"
  10. )
  11. type Observer struct {
  12. features.TaggedFeatures
  13. config *Config
  14. ctx context.Context
  15. }
  16. func (o Observer) GetObservation(ctx context.Context) (proto.Message, error) {
  17. return common.Must2(o.GetFeaturesByTag("")).(extension.Observatory).GetObservation(ctx)
  18. }
  19. func (o Observer) Type() interface{} {
  20. return extension.ObservatoryType()
  21. }
  22. func New(ctx context.Context, config *Config) (*Observer, error) {
  23. holder, err := taggedfeatures.NewHolderFromConfig(ctx, config.Holders, extension.ObservatoryType())
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &Observer{config: config, ctx: ctx, TaggedFeatures: holder}, nil
  28. }
  29. func (x *Config) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
  30. var err error
  31. x.Holders, err = taggedfeatures.LoadJSONConfig(context.TODO(), "service", "background", bytes)
  32. return err
  33. }
  34. func init() {
  35. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  36. return New(ctx, config.(*Config))
  37. }))
  38. }