github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/observatory/command/command.go (about)

     1  //go:build !confonly
     2  // +build !confonly
     3  
     4  package command
     5  
     6  //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/golang/protobuf/proto"
    12  	"google.golang.org/grpc"
    13  
    14  	core "github.com/v2fly/v2ray-core/v5"
    15  	"github.com/v2fly/v2ray-core/v5/app/observatory"
    16  	"github.com/v2fly/v2ray-core/v5/common"
    17  	"github.com/v2fly/v2ray-core/v5/features"
    18  	"github.com/v2fly/v2ray-core/v5/features/extension"
    19  )
    20  
    21  type service struct {
    22  	UnimplementedObservatoryServiceServer
    23  	v *core.Instance
    24  
    25  	observatory extension.Observatory
    26  }
    27  
    28  func (s *service) GetOutboundStatus(ctx context.Context, request *GetOutboundStatusRequest) (*GetOutboundStatusResponse, error) {
    29  	var result proto.Message
    30  	if request.Tag == "" {
    31  		observeResult, err := s.observatory.GetObservation(ctx)
    32  		if err != nil {
    33  			return nil, newError("cannot get observation").Base(err)
    34  		}
    35  		result = observeResult
    36  	} else {
    37  		fet, err := s.observatory.(features.TaggedFeatures).GetFeaturesByTag(request.Tag)
    38  		if err != nil {
    39  			return nil, newError("cannot get tagged observatory").Base(err)
    40  		}
    41  		observeResult, err := fet.(extension.Observatory).GetObservation(ctx)
    42  		if err != nil {
    43  			return nil, newError("cannot get observation").Base(err)
    44  		}
    45  		result = observeResult
    46  	}
    47  	retdata := result.(*observatory.ObservationResult)
    48  	return &GetOutboundStatusResponse{
    49  		Status: retdata,
    50  	}, nil
    51  }
    52  
    53  func (s *service) Register(server *grpc.Server) {
    54  	RegisterObservatoryServiceServer(server, s)
    55  }
    56  
    57  func init() {
    58  	common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
    59  		s := core.MustFromContext(ctx)
    60  		sv := &service{v: s}
    61  		err := s.RequireFeatures(func(Observatory extension.Observatory) {
    62  			sv.observatory = Observatory
    63  		})
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		return sv, nil
    68  	}))
    69  }